Skip to content

Commit

Permalink
#34 Implement getting claim by its name
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaLisov committed Apr 11, 2024
1 parent 49cb91b commit d281336
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ public Map<String, Object> claims(
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
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/github/ilyalisov/jwt/service/TokenService.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,16 @@ 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 @@ -138,4 +138,18 @@ public Map<String, Object> claims(
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 @@ -14,6 +14,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 @@ -202,6 +203,58 @@ void shouldReturnCorrectClaims() {
assertEquals(123, claims.get("key2"));
}


@Test
void shouldReturnCorrectClaimValue() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);
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 shouldReturnNull() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);
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 shouldInvalidateByToken() {
String subject = "testSubject";
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 TokenServiceImplTests {
Expand Down Expand Up @@ -176,4 +177,55 @@ void shouldReturnCorrectClaims() {
assertEquals(123, claims.get("key2"));
}

@Test
void shouldReturnCorrectClaimValue() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);
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 shouldReturnNull() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);
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);
}

}

0 comments on commit d281336

Please sign in to comment.