Search in sources :

Example 21 with JWT

use of com.nimbusds.jwt.JWT in project ddf by codice.

the class OidcTokenValidatorTest method testValidateUserInfoIdTokenInvalidSignature.

@Test(expected = OidcValidationException.class)
public void testValidateUserInfoIdTokenInvalidSignature() throws Exception {
    String stringJwt = getIdTokenBuilder().sign(invalidAlgorithm);
    JWT jwt = SignedJWT.parse(stringJwt);
    OidcTokenValidator.validateUserInfoIdToken(jwt, resourceRetriever, oidcProviderMetadata);
}
Also used : PlainJWT(com.nimbusds.jwt.PlainJWT) JWT(com.nimbusds.jwt.JWT) SignedJWT(com.nimbusds.jwt.SignedJWT) Test(org.junit.Test)

Example 22 with JWT

use of com.nimbusds.jwt.JWT in project ddf by codice.

the class OidcTokenValidatorTest method testValidateIdTokensExpiredToken.

@Test(expected = OidcValidationException.class)
public void testValidateIdTokensExpiredToken() throws Exception {
    WebContext context = getWebContext();
    String stringJwt = getIdTokenBuilder().withClaim("nonce", "myNonce").withExpiresAt(new Date(Instant.now().minus(Duration.ofDays(3)).toEpochMilli())).sign(invalidAlgorithm);
    JWT jwt = SignedJWT.parse(stringJwt);
    OidcTokenValidator.validateIdTokens(jwt, context, configuration, oidcClient);
}
Also used : WebContext(org.pac4j.core.context.WebContext) PlainJWT(com.nimbusds.jwt.PlainJWT) JWT(com.nimbusds.jwt.JWT) SignedJWT(com.nimbusds.jwt.SignedJWT) Date(java.util.Date) Test(org.junit.Test)

Example 23 with JWT

use of com.nimbusds.jwt.JWT in project ddf by codice.

the class OidcTokenValidatorTest method testValidateIdTokensInvalidSignature.

@Test(expected = OidcValidationException.class)
public void testValidateIdTokensInvalidSignature() throws Exception {
    WebContext context = getWebContext();
    String stringJwt = getIdTokenBuilder().withClaim("nonce", "myNonce").sign(invalidAlgorithm);
    JWT jwt = SignedJWT.parse(stringJwt);
    OidcTokenValidator.validateIdTokens(jwt, context, configuration, oidcClient);
}
Also used : WebContext(org.pac4j.core.context.WebContext) PlainJWT(com.nimbusds.jwt.PlainJWT) JWT(com.nimbusds.jwt.JWT) SignedJWT(com.nimbusds.jwt.SignedJWT) Test(org.junit.Test)

Example 24 with JWT

use of com.nimbusds.jwt.JWT in project ddf by codice.

the class OidcTokenValidatorTest method testValidateAccessToken.

@Test
public void testValidateAccessToken() throws Exception {
    String accessTokenString = getAccessTokenBuilder().sign(validAlgorithm);
    AccessToken accessToken = new BearerAccessToken(accessTokenString);
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    messageDigest.update(accessTokenString.getBytes(Charset.forName("US-ASCII")));
    byte[] hash = messageDigest.digest();
    byte[] firstHalf = Arrays.copyOf(hash, hash.length / 2);
    String idToken = getIdTokenBuilder().withClaim("nonce", "myNonce").withClaim("at_hash", Base64URL.encode(firstHalf).toString()).sign(validAlgorithm);
    JWT jwt = SignedJWT.parse(idToken);
    OidcTokenValidator.validateAccessToken(accessToken, jwt, resourceRetriever, oidcProviderMetadata, configuration);
}
Also used : AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) PlainJWT(com.nimbusds.jwt.PlainJWT) JWT(com.nimbusds.jwt.JWT) SignedJWT(com.nimbusds.jwt.SignedJWT) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Example 25 with JWT

use of com.nimbusds.jwt.JWT in project ddf by codice.

the class OidcTokenValidatorTest method testValidateAccessTokenInvalidAtHash.

@Test(expected = OidcValidationException.class)
public void testValidateAccessTokenInvalidAtHash() throws Exception {
    String accessTokenString = getAccessTokenBuilder().sign(validAlgorithm);
    AccessToken accessToken = new BearerAccessToken(accessTokenString);
    String idToken = getIdTokenBuilder().withClaim("nonce", "myNonce").withClaim("at_hash", "WRONG").sign(validAlgorithm);
    JWT jwt = SignedJWT.parse(idToken);
    OidcTokenValidator.validateAccessToken(accessToken, jwt, resourceRetriever, oidcProviderMetadata, configuration);
}
Also used : AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) PlainJWT(com.nimbusds.jwt.PlainJWT) JWT(com.nimbusds.jwt.JWT) SignedJWT(com.nimbusds.jwt.SignedJWT) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) Test(org.junit.Test)

Aggregations

JWT (com.nimbusds.jwt.JWT)28 SignedJWT (com.nimbusds.jwt.SignedJWT)17 PlainJWT (com.nimbusds.jwt.PlainJWT)16 Test (org.junit.Test)14 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)9 BearerAccessToken (com.nimbusds.oauth2.sdk.token.BearerAccessToken)9 WebContext (org.pac4j.core.context.WebContext)7 TechnicalException (org.pac4j.core.exception.TechnicalException)6 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)5 JOSEException (com.nimbusds.jose.JOSEException)4 ParseException (com.nimbusds.oauth2.sdk.ParseException)4 IOException (java.io.IOException)4 ParseException (java.text.ParseException)4 AuthorizationCode (com.nimbusds.oauth2.sdk.AuthorizationCode)3 HTTPRequest (com.nimbusds.oauth2.sdk.http.HTTPRequest)3 URI (java.net.URI)3 Date (java.util.Date)3 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)2 BadJOSEException (com.nimbusds.jose.proc.BadJOSEException)2 Resource (com.nimbusds.jose.util.Resource)2