Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.3.0 #29

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "maven"
directory: "/"
schedule:
interval: "weekly"
2 changes: 2 additions & 0 deletions .rultor.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
architect:
- javacoded78
assets:
settings.xml: javacoded78/secrets#assets/settings.xml
secring.gpg: javacoded78/secrets#assets/Andrei Soroka_0x872A42FB_SECRET.gpg
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This repository is an open-source Java library for fast and convenient using of
* [Get subject from token](#get-subject-from-jwt-token)
* [Get type from token](#get-type-from-jwt-token)
* [Get claims from token](#get-claims-from-jwt-token)
* [Get claim from token](#get-claim-from-jwt-token)
* [How to contribute](#how-to-contribute)

## How to use
Expand Down Expand Up @@ -244,6 +245,21 @@ public class Main {

```

### Get claim from JWT token

To get claim by its name from JWT token payload call method `claim(String token, String key)`on `TokenService` object.

```java
public class Main {
public static void main(String[] args) {
String token="eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbmRyb3Nvcjk5QGdtYWlsLmNvbSIsImlkIjozLCJyb2xlcyI6WyJST0xFX1VTRVIiXSwiZXhwIjoxNzE3MTQ1MDIxfQ.w8ZFLFsKf7Qs9_dNb0WzdoyAIpWtfeEyqLfNI_G16_6NHbGwCRbeVVm_a_DzckytsyGYHTWRlZdi_gWK-HjrXg";
String claim = (String) tokenService.claim(token, "subject");
System.out.println(claim);
}
}

```

## How to contribute

See active issues at [issues page](https://github.com/JavaCoDED78/JWT-humble/issues)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<jjwt.version>0.12.5</jjwt.version>
<lombok.version>1.18.32</lombok.version>
<jedis.version>5.1.3</jedis.version>
<checkstyle.version>3.2.2</checkstyle.version>
<checkstyle.version>3.3.1</checkstyle.version>
<junit-version>5.10.2</junit-version>
<testcontainers.version>1.19.8</testcontainers.version>
<jacoco.version>0.8.11</jacoco.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ public Map<String, Object> claims(final String token) {
return new HashMap<>(claims.getPayload());
}

@Override
public Object claim(final String token,
final String key) {
Jws<Claims> claims = Jwts
.parser()
.verifyWith(this.key)
.build()
.parseSignedClaims(token);
return claims.getPayload()
.get(key);
}

@Override
public boolean invalidate(final String token) {
return tokenStorage.remove(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ boolean isExpired(String token,
*/
Map<String, Object> claims(String token);

/**
* Returns claim of JWT token by its key.
*
* @param token JWT token
* @param key key of claim
* @return value of claim or null if there is no value
*/
Object claim(String token,
String key);

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,16 @@ public Map<String, Object> claims(final String token) {
return new HashMap<>(claims.getPayload());
}

@Override
public Object claim(final String token,
final String key) {
Jws<Claims> claims = Jwts
.parser()
.verifyWith(this.key)
.build()
.parseSignedClaims(token);
return claims.getPayload()
.get(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PersistentTokenServiceImplTests {
Expand Down Expand Up @@ -168,6 +169,51 @@ void claims_ReturnsCorrectClaims() {
assertEquals(123, claims.get("key2"));
}

@Test
void claim_ExistingKey_ReturnsCorrectClaimValue() {
Map<String, Object> customClaims = new HashMap<>();
customClaims.put("key1", "value1");
customClaims.put("key2", 123);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.claims(customClaims)
.build();

String token = tokenService.create(params);
Object claim = tokenService.claim(
token,
"key1"
);
assertNotNull(claim);
assertEquals("value1", claim);
}

@Test
void claim_NotExistingKey_ReturnsNull() {
Map<String, Object> customClaims = new HashMap<>();
customClaims.put("key1", "value1");
customClaims.put("key2", 123);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.claims(customClaims)
.build();

String token = tokenService.create(params);
Object claim = tokenService.claim(
token,
"notExistingKey"
);
assertNull(claim);
}

@Test
void invalidate_Token() {
TokenParameters params = TokenParameters.builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class TokenServiceImplTest {
Expand Down Expand Up @@ -154,4 +155,49 @@ void claims_ReturnsCorrectClaims() {
assertEquals(123, claims.get("key2"));
}

@Test
void claim_ExistingKey_ReturnsCorrectClaimValue() {
Map<String, Object> customClaims = new HashMap<>();
customClaims.put("key1", "value1");
customClaims.put("key2", 123);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.claims(customClaims)
.build();

String token = tokenService.create(params);
Object claim = tokenService.claim(
token,
"key1"
);
assertNotNull(claim);
assertEquals("value1", claim);
}

@Test
void claim_NotExistingKey_ReturnsNull() {
Map<String, Object> customClaims = new HashMap<>();
customClaims.put("key1", "value1");
customClaims.put("key2", 123);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.claims(customClaims)
.build();

String token = tokenService.create(params);
Object claim = tokenService.claim(
token,
"notExistingKey"
);
assertNull(claim);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,6 @@ void invalidate_SubjectAndType() {
String existingToken = tokenStorage.get(params);
assertNull(existingToken);
}

}