Search in sources :

Example 11 with JWTClaimsSet

use of com.nimbusds.jwt.JWTClaimsSet in project carbon-apimgt by wso2.

the class JWTWithRSASignatureImplTestCase method testRSASignAndSerializeWithNullRSAPrivateKey.

@Test(description = "Test RSA sign and serialize when RSA Private key is null", expectedExceptions = IllegalArgumentException.class)
public void testRSASignAndSerializeWithNullRSAPrivateKey() throws APIManagementException {
    JWTWithRSASignatureImpl jwtWithRSASignature = new JWTWithRSASignatureImpl();
    JWTClaimsSet jwtClaimsSet = Mockito.mock(JWTClaimsSet.class);
    jwtWithRSASignature.rsaSignAndSerialize(null, jwtClaimsSet);
}
Also used : JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) Test(org.testng.annotations.Test)

Example 12 with JWTClaimsSet

use of com.nimbusds.jwt.JWTClaimsSet in project pac4j by pac4j.

the class JwtAuthenticator method createJwtProfile.

@SuppressWarnings("unchecked")
protected void createJwtProfile(final TokenCredentials credentials, final JWT jwt) throws ParseException {
    final JWTClaimsSet claimSet = jwt.getJWTClaimsSet();
    String subject = claimSet.getSubject();
    if (subject == null) {
        throw new TechnicalException("JWT must contain a subject ('sub' claim)");
    }
    final Date expirationTime = claimSet.getExpirationTime();
    if (expirationTime != null) {
        final Date now = new Date();
        if (expirationTime.before(now)) {
            logger.error("The JWT is expired: no profile is built");
            return;
        }
    }
    final Map<String, Object> attributes = new HashMap<>(claimSet.getClaims());
    attributes.remove(JwtClaims.SUBJECT);
    final List<String> roles = (List<String>) attributes.get(JwtGenerator.INTERNAL_ROLES);
    attributes.remove(JwtGenerator.INTERNAL_ROLES);
    final List<String> permissions = (List<String>) attributes.get(JwtGenerator.INTERNAL_PERMISSIONS);
    attributes.remove(JwtGenerator.INTERNAL_PERMISSIONS);
    final CommonProfile profile = ProfileHelper.restoreOrBuildProfile(getProfileDefinition(), subject, attributes, null);
    if (roles != null) {
        profile.addRoles(roles);
    }
    if (permissions != null) {
        profile.addPermissions(permissions);
    }
    credentials.setUserProfile(profile);
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) HashMap(java.util.HashMap) CommonProfile(org.pac4j.core.profile.CommonProfile) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date)

Example 13 with JWTClaimsSet

use of com.nimbusds.jwt.JWTClaimsSet in project pac4j by pac4j.

the class JwtGenerator method buildJwtClaimsSet.

protected JWTClaimsSet buildJwtClaimsSet(final U profile) {
    // claims builder with subject and issue time
    final JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder().subject(profile.getTypedId()).issueTime(new Date());
    // add attributes
    final Map<String, Object> attributes = profile.getAttributes();
    for (final Map.Entry<String, Object> entry : attributes.entrySet()) {
        builder.claim(entry.getKey(), entry.getValue());
    }
    builder.claim(INTERNAL_ROLES, profile.getRoles());
    builder.claim(INTERNAL_PERMISSIONS, profile.getPermissions());
    // claims
    return builder.build();
}
Also used : JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) Map(java.util.Map) Date(java.util.Date)

Example 14 with JWTClaimsSet

use of com.nimbusds.jwt.JWTClaimsSet in project pac4j by pac4j.

the class ECSignatureConfigurationTests method testSignVerify.

@Test
public void testSignVerify() throws JOSEException {
    final ECSignatureConfiguration config = new ECSignatureConfiguration(buildKeyPair());
    final JWTClaimsSet claims = new JWTClaimsSet.Builder().subject(VALUE).build();
    final SignedJWT signedJwt = config.sign(claims);
    assertTrue(config.verify(signedJwt));
}
Also used : JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SignedJWT(com.nimbusds.jwt.SignedJWT) Test(org.junit.Test)

Example 15 with JWTClaimsSet

use of com.nimbusds.jwt.JWTClaimsSet in project pac4j by pac4j.

the class RSASignatureConfigurationTests method testSignVerify.

@Test
public void testSignVerify() throws JOSEException {
    final RSASignatureConfiguration config = new RSASignatureConfiguration(buildKeyPair());
    final JWTClaimsSet claims = new JWTClaimsSet.Builder().subject(VALUE).build();
    final SignedJWT signedJwt = config.sign(claims);
    assertTrue(config.verify(signedJwt));
}
Also used : JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SignedJWT(com.nimbusds.jwt.SignedJWT) Test(org.junit.Test)

Aggregations

JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)69 SignedJWT (com.nimbusds.jwt.SignedJWT)44 JWSHeader (com.nimbusds.jose.JWSHeader)23 Date (java.util.Date)19 Test (org.junit.Test)16 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)14 Test (org.junit.jupiter.api.Test)11 JOSEException (com.nimbusds.jose.JOSEException)9 ParseException (java.text.ParseException)9 SecretKey (javax.crypto.SecretKey)8 JWSSigner (com.nimbusds.jose.JWSSigner)7 MacAlgorithm (org.springframework.security.oauth2.jose.jws.MacAlgorithm)7 Instant (java.time.Instant)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)5 MACSigner (com.nimbusds.jose.crypto.MACSigner)5 BadJOSEException (com.nimbusds.jose.proc.BadJOSEException)5 JWT (com.nimbusds.jwt.JWT)5