Search in sources :

Example 26 with JWSSigner

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

the class AbstractJWTFilterTest method getJWT.

protected SignedJWT getJWT(final String issuer, final String sub, final String aud, final Date expires, final Date nbf, final RSAPrivateKey privateKey, final String signatureAlgorithm, final String knoxId) throws Exception {
    JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
    builder.issuer(issuer).subject(sub).audience(aud).expirationTime(expires).notBeforeTime(nbf).claim("scope", "openid").claim(PASSCODE_CLAIM, UUID.randomUUID().toString());
    if (knoxId != null) {
        builder.claim(JWTToken.KNOX_ID_CLAIM, knoxId);
    }
    JWTClaimsSet claims = builder.build();
    JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.parse(signatureAlgorithm)).build();
    SignedJWT signedJWT = new SignedJWT(header, claims);
    JWSSigner signer = new RSASSASigner(privateKey);
    signedJWT.sign(signer);
    return signedJWT;
}
Also used : JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) SignedJWT(com.nimbusds.jwt.SignedJWT) JWSSigner(com.nimbusds.jose.JWSSigner) JWSHeader(com.nimbusds.jose.JWSHeader)

Example 27 with JWSSigner

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

the class DefaultTokenAuthorityService method signTokenWithHMAC.

private void signTokenWithHMAC(final JWT token) throws TokenServiceException {
    try {
        final JWSSigner signer = new MACSigner(getHmacSecret());
        token.sign(signer);
    } catch (KeyLengthException e) {
        throw new TokenServiceException(e);
    }
}
Also used : MACSigner(com.nimbusds.jose.crypto.MACSigner) JWSSigner(com.nimbusds.jose.JWSSigner) KeyLengthException(com.nimbusds.jose.KeyLengthException) TokenServiceException(org.apache.knox.gateway.services.security.token.TokenServiceException)

Example 28 with JWSSigner

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

the class DefaultTokenStateServiceTest method getJWTToken.

/* create a test JWT token */
protected JWT getJWTToken(final long expiry) {
    String[] claims = new String[6];
    claims[0] = "KNOXSSO";
    claims[1] = "john.doe@example.com";
    claims[2] = "https://login.example.com";
    if (expiry > 0) {
        claims[3] = Long.toString(expiry);
    }
    claims[4] = "E0LDZulQ0XE_otJ5aoQtQu-RnXv8hU-M9U4dD7vDioA";
    claims[5] = null;
    JWT token = new JWTToken("RS256", claims);
    // Sign the token
    JWSSigner signer = new RSASSASigner(privateKey);
    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 29 with JWSSigner

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

the class JWTTokenTest method testTokenSignature.

@Test
public void testTokenSignature() 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("RS256", claims);
    assertEquals("KNOXSSO", token.getIssuer());
    assertEquals("john.doe@example.com", token.getSubject());
    assertEquals("https://login.example.com", token.getAudience());
    // 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)

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