Skip to content

Commit

Permalink
Add methods to JsonWebToken to get claims by using the Claims enum
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Eberle <andreas.eberle@arconsis.com>
  • Loading branch information
andreas-eberle committed Feb 28, 2020
1 parent 6de52aa commit 6d55506
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions api/src/main/java/org/eclipse/microprofile/jwt/JsonWebToken.java
Expand Up @@ -48,15 +48,15 @@ public interface JsonWebToken extends Principal {
* @return raw bear token string
*/
default String getRawToken() {
return getClaim(Claims.raw_token.name());
return getClaim(Claims.raw_token);
}

/**
* The iss(Issuer) claim identifies the principal that issued the JWT
* @return the iss claim.
*/
default String getIssuer() {
return getClaim(Claims.iss.name());
return getClaim(Claims.iss);
}

/**
Expand All @@ -65,7 +65,7 @@ default String getIssuer() {
* @return the aud claim or null if the claim is not present
*/
default Set<String> getAudience() {
return getClaim(Claims.aud.name());
return getClaim(Claims.aud);
}

/**
Expand All @@ -76,7 +76,7 @@ default Set<String> getAudience() {
* @return the sub claim.
*/
default String getSubject() {
return getClaim(Claims.sub.name());
return getClaim(Claims.sub);
}

/**
Expand All @@ -90,7 +90,7 @@ default String getSubject() {
* @return the jti claim.
*/
default String getTokenID() {
return getClaim(Claims.jti.name());
return getClaim(Claims.jti);
}

/**
Expand All @@ -100,7 +100,7 @@ default String getTokenID() {
* @return the exp claim.
*/
default long getExpirationTime() {
return getClaim(Claims.exp.name());
return getClaim(Claims.exp);
}

/**
Expand All @@ -109,7 +109,7 @@ default long getExpirationTime() {
* @return the iat claim
*/
default long getIssuedAtTime() {
return getClaim(Claims.iat.name());
return getClaim(Claims.iat);
}

/**
Expand All @@ -120,7 +120,7 @@ default long getIssuedAtTime() {
* @return a possibly empty set of group names.
*/
default Set<String> getGroups() {
return getClaim(Claims.groups.name());
return getClaim(Claims.groups);
}

/**
Expand All @@ -140,13 +140,24 @@ default boolean containsClaim(String claimName) {

/**
* Access the value of the indicated claim.
*
*
* @param <T> The claim type
* @param claimName - the name of the claim
* @return the value of the indicated claim if it exists, null otherwise.
*/
<T> T getClaim(String claimName);

/**
* Access the value of the indicated claim.
*
* @param <T> The claim type
* @param claim - the claim
* @return the value of the indicated claim if it exists, null otherwise.
*/
default <T> T getClaim(Claims claim) {
return getClaim(claim.name());
}

/**
* A utility method to access a claim value in an {@linkplain Optional}
* wrapper
Expand All @@ -157,4 +168,15 @@ default boolean containsClaim(String claimName) {
default <T> Optional<T> claim(String claimName) {
return Optional.ofNullable(getClaim(claimName));
}

/**
* A utility method to access a claim value in an {@linkplain Optional}
* wrapper
* @param claim - the claim
* @param <T> - the type of the claim value to return
* @return an Optional wrapper of the claim value
*/
default <T> Optional<T> claim(Claims claim) {
return claim(claim.name());
}
}

0 comments on commit 6d55506

Please sign in to comment.