Search in sources :

Example 26 with SignedJWT

use of com.nimbusds.jwt.SignedJWT 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)

Example 27 with SignedJWT

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

the class SecretSignatureConfigurationTests method testSignVerifyBase64.

@Test
public void testSignVerifyBase64() throws JOSEException {
    SecretSignatureConfiguration config = new SecretSignatureConfiguration();
    config.setSecretBase64(BASE64_512_BIT_SIG_SECRET);
    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 28 with SignedJWT

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

the class SecretSignatureConfigurationTests method testSignVerify.

@Test
public void testSignVerify() throws JOSEException {
    final SecretSignatureConfiguration config = new SecretSignatureConfiguration(MAC_SECRET);
    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 29 with SignedJWT

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

the class AbstractEncryptionConfiguration method encrypt.

@Override
public String encrypt(final JWT jwt) {
    init();
    if (jwt instanceof SignedJWT) {
        // Create JWE object with signed JWT as payload
        final JWEObject jweObject = new JWEObject(new JWEHeader.Builder(this.algorithm, this.method).contentType("JWT").build(), new Payload((SignedJWT) jwt));
        try {
            // Perform encryption
            jweObject.encrypt(buildEncrypter());
        } catch (final JOSEException e) {
            throw new TechnicalException(e);
        }
        // Serialise to JWE compact form
        return jweObject.serialize();
    } else {
        // create header
        final JWEHeader header = new JWEHeader(this.algorithm, this.method);
        try {
            // encrypted jwt
            EncryptedJWT encryptedJwt = new EncryptedJWT(header, jwt.getJWTClaimsSet());
            // Perform encryption
            encryptedJwt.encrypt(buildEncrypter());
            // serialize
            return encryptedJwt.serialize();
        } catch (final JOSEException | ParseException e) {
            throw new TechnicalException(e);
        }
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) SignedJWT(com.nimbusds.jwt.SignedJWT) ParseException(java.text.ParseException) EncryptedJWT(com.nimbusds.jwt.EncryptedJWT)

Example 30 with SignedJWT

use of com.nimbusds.jwt.SignedJWT in project connect-android-sdk by telenordigital.

the class IdTokenValidatorTest method authorizedPartyNotEqualClientThrows.

@Test(expected = ConnectException.class)
public void authorizedPartyNotEqualClientThrows() throws Exception {
    BDDMockito.given(ConnectSdk.getConnectApiUrl()).willReturn(HttpUrl.parse("https://connect.telenordigital.com"));
    BDDMockito.given(ConnectSdk.getClientId()).willReturn("connect-tests");
    BDDMockito.given(ConnectSdk.getExpectedIssuer()).willReturn("https://connect.telenordigital.com/oauth");
    JWTClaimsSet claimsSet = new JWTClaimsSet();
    claimsSet.setIssuer("https://connect.telenordigital.com/oauth");
    claimsSet.setAudience("connect-tests");
    claimsSet.setExpirationTime(oneHourIntoFuture);
    claimsSet.setIssueTime(now);
    claimsSet.setCustomClaim("azp", "NOT connect-tests");
    SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.ES256), claimsSet);
    signedJWT.sign(new ECDSASigner(new BigInteger("123")));
    IdToken idToken = new IdToken(signedJWT.serialize());
    IdTokenValidator.validate(idToken, null);
}
Also used : IdToken(com.telenor.connect.id.IdToken) ECDSASigner(com.nimbusds.jose.crypto.ECDSASigner) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) BigInteger(java.math.BigInteger) SignedJWT(com.nimbusds.jwt.SignedJWT) JWSHeader(com.nimbusds.jose.JWSHeader) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SignedJWT (com.nimbusds.jwt.SignedJWT)137 Date (java.util.Date)51 Test (org.junit.Test)50 HttpServletRequest (javax.servlet.http.HttpServletRequest)47 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)45 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 Properties (java.util.Properties)39 ServletException (javax.servlet.ServletException)39 JWSHeader (com.nimbusds.jose.JWSHeader)30 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)24 Cookie (javax.servlet.http.Cookie)21 ParseException (java.text.ParseException)20 JOSEException (com.nimbusds.jose.JOSEException)19 JWSSigner (com.nimbusds.jose.JWSSigner)14 Test (org.junit.jupiter.api.Test)12 AuthenticationException (com.hortonworks.registries.auth.client.AuthenticationException)10 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)10 AuthenticationException (org.apache.hadoop.security.authentication.client.AuthenticationException)10 PrimaryPrincipal (org.apache.knox.gateway.security.PrimaryPrincipal)10 JWSVerifier (com.nimbusds.jose.JWSVerifier)9