Skip to content

Commit

Permalink
#13 Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaLisov committed Jan 22, 2024
1 parent c73b34b commit 3492158
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
63 changes: 60 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ JWT tokens in your Java applications.
* [How to use](#how-to-use)
* [Instantiate a service](#instantiate-a-service)
* [Persist tokens](#persist-tokens)
* [Invalidate token](#invalidate-jwt-token)
* [Create token](#create-jwt-token)
* [If token is expired](#check-if-jwt-token-is-expired)
* [If token has claim](#check-if-jwt-token-has-claim)
Expand All @@ -33,7 +34,7 @@ With Maven add dependency to your `pom.xml`.
<dependency>
<groupId>io.github.ilyalisov</groupId>
<artifactId>jwt</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
</dependency>
```

Expand All @@ -59,7 +60,7 @@ After, you can call available methods and use library.

### Persist tokens

Library supports `PersistentTokenServiceImpl` implementation with saving
Library supports `PersistentTokenService` implementation with saving
tokens to `TokenStorage`.

With such approach you can store tokens in Redis or in-memory Map and create new
Expand Down Expand Up @@ -97,7 +98,7 @@ public class Main {
port
);

TokenService tokenService = new PersistentTokenServiceImpl(
PersistentTokenService tokenService = new PersistentTokenServiceImpl(
secret,
tokenStorage
);
Expand All @@ -110,6 +111,43 @@ JWT token. Just pass it as argument in `RedisTokenStorageImpl` constructor.

By default, library uses key `"tokens:" + subject + ":" + type`.

### Invalidate JWT token

With `PersistentTokenService` you can invalidate token by token itself or by
subject and token type. If first option is chosen, all keys with such token
values will be deleted.

If token will be deleted from storage you receive `true`.

```java
public class Main {
public static void main(String[] args) {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";

boolean deleted = persistentTokenService.invalidate(token);

System.out.println(deleted);
}
}
```

```java
public class Main {
public static void main(String[] args) {
boolean deleted = persistentTokenService.invalidate(
TokenParameters.builder(
"user@example.com",
"access",
Duration.of(1, ChronoUnit.HOURS)
)
.build()
);

System.out.println(deleted);
}
}
```

### Create JWT token

To create token call method `create(TokenParameters params)` on `TokenService`
Expand All @@ -121,6 +159,7 @@ public class Main {
String token = tokenService.create(
TokenParameters.builder(
"user@example.com",
"access",
Duration.of(1, ChronoUnit.HOURS)
)
.build()
Expand Down Expand Up @@ -158,6 +197,24 @@ class Main {
}
```

You can also check expiration with any other date.

```java
class Main {
public static void main(String[] args) {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
Date date = new Date(1705911211182);

boolean expired = tokenService.isExpired(
token,
date
);

System.out.println(expired);
}
}
```

### Check if JWT token has claim

To check if JWT token has claim in payload call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ void shouldInvalidateByToken() {
)
.build();
String newToken = tokenService.create(newParams);
System.out.println(token);
System.out.println(newToken);
assertNotEquals(token, newToken);
}

Expand Down

0 comments on commit 3492158

Please sign in to comment.