Search in sources :

Example 11 with JWSSigner

use of com.nimbusds.jose.JWSSigner in project knox by apache.

the class JWKSResourceTest method getTestToken.

private JWT getTestToken(final String algorithm) {
    String[] claimArray = new String[6];
    claimArray[0] = "KNOXSSO";
    claimArray[1] = "joe@example.com";
    claimArray[2] = null;
    claimArray[3] = null;
    claimArray[4] = "E0LDZulQ0XE_otJ5aoQtQu-RnXv8hU-M9U4dD7vDioA";
    claimArray[5] = null;
    final JWT token = new JWTToken(algorithm, claimArray, Collections.singletonList("aud"), false);
    final JWSSigner signer = new RSASSASigner(privateKey, true);
    token.sign(signer);
    return token;
}
Also used : JWT(org.apache.knox.gateway.services.security.token.impl.JWT) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) JWSSigner(com.nimbusds.jose.JWSSigner)

Example 12 with JWSSigner

use of com.nimbusds.jose.JWSSigner in project knox by apache.

the class JWTTokenTest method testTokenSignatureRS512.

@Test
public void testTokenSignatureRS512() throws Exception {
    String[] claims = new String[6];
    claims[0] = "KNOXSSO";
    claims[1] = "john.doe@example.com";
    claims[2] = "https://login.example.com";
    claims[3] = Long.toString((System.currentTimeMillis() / 1000) + 300);
    claims[4] = "E0LDZulQ0XE_otJ5aoQtQu-RnXv8hU-M9U4dD7vDioA";
    claims[5] = null;
    JWT token = new JWTToken(JWSAlgorithm.RS512.getName(), claims);
    assertEquals("KNOXSSO", token.getIssuer());
    assertEquals("john.doe@example.com", token.getSubject());
    assertEquals("https://login.example.com", token.getAudience());
    assertTrue(token.getHeader().contains(JWSAlgorithm.RS512.getName()));
    // Sign the token
    JWSSigner signer = new RSASSASigner(privateKey);
    token.sign(signer);
    assertTrue(token.getSignaturePayload().length > 0);
    // Verify the signature
    JWSVerifier verifier = new RSASSAVerifier(publicKey);
    assertTrue(token.verify(verifier));
}
Also used : RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) JWSSigner(com.nimbusds.jose.JWSSigner) Test(org.junit.Test)

Example 13 with JWSSigner

use of com.nimbusds.jose.JWSSigner in project ratauth by alfa-laboratory.

the class HS256TokenProcessor method createToken.

@Override
@SneakyThrows
public String createToken(String clientId, String secret, String identifier, Date created, Date expiresIn, Set<String> audience, Set<String> scopes, Collection<String> authContext, String userId, Map<String, Object> userInfo) {
    final JWSSigner signer = new MACSigner(Base64.getDecoder().decode(secret));
    final List<String> aud = new ArrayList<>(audience);
    aud.add(clientId);
    // Prepare JWT with claims set
    JWTClaimsSet.Builder jwtBuilder = new JWTClaimsSet.Builder().issuer(issuer).subject(userId).expirationTime(expiresIn).audience(aud).claim(SCOPE, scopes).claim(CLIENT_ID, clientId).claim(ACR_VALUES, authContext).jwtID(identifier).issueTime(created);
    userInfo.forEach(jwtBuilder::claim);
    SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), jwtBuilder.build());
    // Apply the HMAC protection
    signedJWT.sign(signer);
    // eyJhbGciOiJIUzI1NiJ9.SGVsbG8sIHdvcmxkIQ.onO9Ihudz3WkiauDO2Uhyuz0Y18UASXlSc1eS0NkWyA
    return signedJWT.serialize();
}
Also used : MACSigner(com.nimbusds.jose.crypto.MACSigner) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SignedJWT(com.nimbusds.jwt.SignedJWT) JWSSigner(com.nimbusds.jose.JWSSigner) JWSHeader(com.nimbusds.jose.JWSHeader) SneakyThrows(lombok.SneakyThrows)

Example 14 with JWSSigner

use of com.nimbusds.jose.JWSSigner in project perry by ca-cwds.

the class JwtService method sign.

private SignedJWT sign(JWTClaimsSet claimsSet) throws JwtException {
    try {
        JWSSigner signer = new RSASSASigner(keyProvider.getSigningKey());
        SignedJWT signedJWT = new SignedJWT(jwsHeader(), claimsSet);
        signedJWT.sign(signer);
        return signedJWT;
    } catch (Exception e) {
        throw new JwtException(e);
    }
}
Also used : RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) SignedJWT(com.nimbusds.jwt.SignedJWT) JWSSigner(com.nimbusds.jose.JWSSigner) GeneralSecurityException(java.security.GeneralSecurityException)

Example 15 with JWSSigner

use of com.nimbusds.jose.JWSSigner in project carbon-apimgt by wso2.

the class JWTWithRSASignatureImpl method rsaSignAndSerialize.

/**
 * {@inheritDoc}
 */
@Override
public String rsaSignAndSerialize(RSAPrivateKey rsaPrivateKey, JWTClaimsSet claimsSet) throws APIManagementException {
    if (rsaPrivateKey == null) {
        throw new IllegalArgumentException("The private key must not be null");
    }
    if (claimsSet == null) {
        throw new IllegalArgumentException("The JWTClaimsSet must not be null");
    }
    JWSSigner signer = new RSASSASigner(rsaPrivateKey);
    SignedJWT jwt = new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), claimsSet);
    try {
        jwt.sign(signer);
    } catch (JOSEException e) {
        throw new APIManagementException("Error signing JWT ", e);
    }
    return jwt.serialize();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) SignedJWT(com.nimbusds.jwt.SignedJWT) JWSSigner(com.nimbusds.jose.JWSSigner) JOSEException(com.nimbusds.jose.JOSEException) JWSHeader(com.nimbusds.jose.JWSHeader)

Aggregations

JWSSigner (com.nimbusds.jose.JWSSigner)29 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)21 JWSHeader (com.nimbusds.jose.JWSHeader)18 SignedJWT (com.nimbusds.jwt.SignedJWT)18 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)13 JOSEException (com.nimbusds.jose.JOSEException)5 MACSigner (com.nimbusds.jose.crypto.MACSigner)5 PrivateKey (java.security.PrivateKey)5 Date (java.util.Date)4 JOSEObjectType (com.nimbusds.jose.JOSEObjectType)3 JWSObject (com.nimbusds.jose.JWSObject)3 Payload (com.nimbusds.jose.Payload)3 JWK (com.nimbusds.jose.jwk.JWK)3 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)3 JsonArrayBuilder (javax.json.JsonArrayBuilder)3 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 TokenServiceException (org.apache.knox.gateway.services.security.token.TokenServiceException)3 JWT (org.apache.knox.gateway.services.security.token.impl.JWT)3 JWTToken (org.apache.knox.gateway.services.security.token.impl.JWTToken)3 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)2