RSASSASigner signer = new RSASSASigner((RSAKey) jwk); signers.put(id, signer);
private static String getSignedJwt(ServiceAccountCredentials credentials, String iapClientId) throws Exception { Instant now = Instant.now(clock); long expirationTime = now.getEpochSecond() + EXPIRATION_TIME_IN_SECONDS; // generate jwt signed by service account // header must contain algorithm ("alg") and key ID ("kid") JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(credentials.getPrivateKeyId()).build(); // set required claims JWTClaimsSet claims = new JWTClaimsSet.Builder() .audience(OAUTH_TOKEN_URI) .issuer(credentials.getClientEmail()) .subject(credentials.getClientEmail()) .issueTime(Date.from(now)) .expirationTime(Date.from(Instant.ofEpochSecond(expirationTime))) .claim("target_audience", iapClientId) .build(); // sign using service account private key JWSSigner signer = new RSASSASigner(credentials.getPrivateKey()); SignedJWT signedJwt = new SignedJWT(jwsHeader, claims); signedJwt.sign(signer); return signedJwt.serialize(); }
@VisibleForTesting protected JWSSigner createRSASSASignerForKey(RSAPrivateKey privateKey) { return new RSASSASigner(privateKey); }
public static JWSSigner findSigner(KeyAndJwk randomKey) throws JOSEException{ if(randomKey.jwk instanceof RSAKey){ return new RSASSASigner((RSAKey)randomKey.jwk); } else if (randomKey.jwk instanceof ECKey){ return new ECDSASigner((ECKey)randomKey.jwk); } else if (randomKey.jwk instanceof OctetSequenceKey){ return new MACSigner((OctetSequenceKey)randomKey.jwk); } else { throw new IllegalStateException("Unknown key type: " + randomKey.jwk.getClass().getName()); } } }
@Nonnull @Override public JwtWriter signingWriter(@Nonnull AsymmetricSigningInfo signingInfo) { return factoryHelper.makeRsJwtWriter(signingInfo.getSigningAlgorithm(), new RSASSASigner(signingInfo.getPrivateKey())); }
/** * Sign with given RSA Algorithm * * @param signedJWT * @param jwsAlgorithm * @param tenantDomain * @param tenantId * @return * @throws IdentityOAuth2Exception */ protected SignedJWT signJWTWithRSA(SignedJWT signedJWT, JWSAlgorithm jwsAlgorithm, String tenantDomain, int tenantId) throws IdentityOAuth2Exception { try { Key privateKey = getPrivateKey(tenantDomain, tenantId); JWSSigner signer = new RSASSASigner((RSAPrivateKey) privateKey); signedJWT.sign(signer); return signedJWT; } catch (JOSEException e) { log.error("Error in obtaining tenant's keystore", e); throw new IdentityOAuth2Exception("Error in obtaining tenant's keystore", e); } catch (Exception e) { log.error("Error in obtaining tenant's keystore", e); throw new IdentityOAuth2Exception("Error in obtaining tenant's keystore", e); } }
private JWT generateAuthenticationJwt() { // Create RSA-signer with the private key JWSSigner signer = new RSASSASigner(this.rsaPrivateKey); // Prepare JWT with claims set JWTClaimsSet claimsSet = new JWTClaimsSet(); claimsSet.setIssuer(issuer); claimsSet.setSubject(subject); claimsSet.setAudience(audience); claimsSet.setIssueTime(new Date()); claimsSet.setExpirationTime(new Date(new Date().getTime() + durationSeconds * 1000)); claimsSet.setJWTID(tokenReference); SignedJWT signedJWT = new SignedJWT(new com.nimbusds.jose.JWSHeader(JWSAlgorithm.RS256), claimsSet); try { signedJWT.sign(signer); } catch (JOSEException jose_ex) { throw new RuntimeException("Error signing JSON Web Token.", jose_ex); } return signedJWT; } }
private JWT generateAuthenticationJwt() { // Create RSA-signer with the private key JWSSigner signer = new RSASSASigner(this.rsaPrivateKey); // Prepare JWT with claims set JWTClaimsSet claimsSet = new JWTClaimsSet(); claimsSet.setIssuer(issuer); claimsSet.setSubject(subject); claimsSet.setAudience(audience); claimsSet.setIssueTime(new Date()); claimsSet.setExpirationTime(new Date(new Date().getTime() + durationSeconds * 1000)); claimsSet.setJWTID(tokenReference); SignedJWT signedJWT = new SignedJWT(new com.nimbusds.jose.JWSHeader(JWSAlgorithm.RS256), claimsSet); try { signedJWT.sign(signer); } catch (JOSEException jose_ex) { throw new RuntimeException("Error signing JSON Web Token.", jose_ex); } return signedJWT; } }
/** * {@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(); }
/** * {@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(); }
private static String signJWT(String uid, PrivateKey privateKey) { final JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).type(JOSEObjectType.JWT).build(); final JWTClaimsSet payload = new JWTClaimsSet.Builder().claim("uid", uid).build(); final SignedJWT signedJWT = new SignedJWT(header, payload); try { signedJWT.sign(new RSASSASigner(privateKey)); return signedJWT.serialize(); } catch (JOSEException e) { throw new RuntimeException(e); } }
private static String signJWT(String uid, PrivateKey privateKey) { final JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).type(JOSEObjectType.JWT).build(); final JWTClaimsSet payload = new JWTClaimsSet.Builder().claim("uid", uid).build(); final SignedJWT signedJWT = new SignedJWT(header, payload); try { signedJWT.sign(new RSASSASigner(privateKey)); return signedJWT.serialize(); } catch (JOSEException e) { throw new RuntimeException(e); } }
@Override public SignedJWT sign(JWTClaimsSet claims) { init(); CommonHelper.assertNotNull("privateKey", privateKey); try { final JWSSigner signer = new RSASSASigner(this.privateKey); final SignedJWT signedJWT = new SignedJWT(new JWSHeader(algorithm), claims); signedJWT.sign(signer); return signedJWT; } catch (final JOSEException e) { throw new TechnicalException(e); } }
protected String generateJWT(User user) throws Exception { RSAPrivateKey privateKey = getPrivateKey(keyStore, keyStorePassword, alias); // Create RSA-signer with the private key JWSSigner signer = new RSASSASigner(privateKey); // Prepare JWT with claims set JWTClaimsSet claimsSet = new JWTClaimsSet(); claimsSet.setSubject(user.getName()); claimsSet.setClaim("email", user.getEmail()); claimsSet.setClaim("roles", user.getRoles()); claimsSet.setIssuer("wso2.org/products/msf4j"); claimsSet.setExpirationTime(new Date(new Date().getTime() + 60 * 60 * 1000)); //60 min SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), claimsSet); // Compute the RSA signature signedJWT.sign(signer); // To serialize to compact form, produces something like // eyJhbGciOiJSUzI1NiJ9.SW4gUlNBIHdlIHRydXN0IQ.IRMQENi4nJyp4er2L // mZq3ivwoAjqa1uUkSBKFIX7ATndFF5ivnt-m8uApHO4kfIFOrW7w2Ezmlg3Qd // maXlS9DhN0nUk_hGI3amEjkKd0BWYCB8vfUbUv0XGjQip78AI4z1PrFRNidm7 // -jPDm5Iq0SZnjKjCNS5Q15fokXZc8u0A return signedJWT.serialize(); }
/** Get the private key for signing * * @return * @throws JOSEException */ public SignedJWT sign(JWTClaimsSet claims) throws JOSEException{ JWSSigner signer = new RSASSASigner(privateJWK); JWSHeader.Builder head = new JWSHeader.Builder(defaultAlg); head.keyID(getDefaultKeyID()); SignedJWT signedJWT = new SignedJWT(head.build(), claims); signedJWT.sign(signer); return signedJWT; /* For HMAC we could do the following. This may be useful for the implicit flow: ClientDetailsEntity clientEntity = clientDetailsEntityCacheManager.retrieve(authentication.getOAuth2Request().getClientId()); JWSSigner signer = new MACSigner(StringUtils.rightPad(clientEntity.getDecryptedClientSecret(), 32, "#").getBytes()); signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claims.build()); signedJWT.sign(signer); */ }
protected SignedJWT getJWT(String sub, Date expires, RSAPrivateKey privateKey) throws Exception { JWTClaimsSet claimsSet = new JWTClaimsSet(); claimsSet.setSubject(sub); claimsSet.setIssueTime(new Date(new Date().getTime())); claimsSet.setIssuer("https://c2id.com"); claimsSet.setCustomClaim("scope", "openid"); claimsSet.setExpirationTime(expires); List<String> aud = new ArrayList<String>(); aud.add("bar"); claimsSet.setAudience("bar"); JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).build(); SignedJWT signedJWT = new SignedJWT(header, claimsSet); JWSSigner signer = new RSASSASigner(privateKey); signedJWT.sign(signer); return signedJWT; }
protected SignedJWT getJWT(String sub, Date expires, RSAPrivateKey privateKey) throws Exception { JWTClaimsSet claimsSet = new JWTClaimsSet(); claimsSet.setSubject(sub); claimsSet.setIssueTime(new Date(new Date().getTime())); claimsSet.setIssuer("https://c2id.com"); claimsSet.setCustomClaim("scope", "openid"); claimsSet.setExpirationTime(expires); List<String> aud = new ArrayList<String>(); aud.add("bar"); claimsSet.setAudience("bar"); JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).build(); SignedJWT signedJWT = new SignedJWT(header, claimsSet); JWSSigner signer = new RSASSASigner(privateKey); signedJWT.sign(signer); return signedJWT; }
protected SignedJWT getJWT(String sub, Date expires, RSAPrivateKey privateKey) throws Exception { JWTClaimsSet claimsSet = new JWTClaimsSet.Builder() .subject(sub) .issueTime(new Date(new Date().getTime())) .issuer("https://c2id.com") .claim("scope", "openid") .audience("bar") .expirationTime(expires) .build(); List<String> aud = new ArrayList<String>(); aud.add("bar"); JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).build(); SignedJWT signedJWT = new SignedJWT(header, claimsSet); JWSSigner signer = new RSASSASigner(privateKey); signedJWT.sign(signer); return signedJWT; } }
private String signJwt(JWTClaimsSet jwtClaimsSet) throws AuthException { if (JWSAlgorithm.RS256.equals(signatureAlgorithm) || JWSAlgorithm.RS384.equals(signatureAlgorithm) || JWSAlgorithm.RS512.equals(signatureAlgorithm)) { try { JWSSigner signer = new RSASSASigner(ServiceReferenceHolder.getInstance().getPrivateKey()); JWSAlgorithm jwsAlgorithm; if (signatureAlgorithm instanceof JWSAlgorithm) { jwsAlgorithm = (JWSAlgorithm) signatureAlgorithm; } else { throw new AuthException("Signature Algorithm couldn't convert to JWSAlgorithm"); } JWSHeader.Builder headerBuilder = new JWSHeader.Builder(jwsAlgorithm); String certThumbPrint = getThumbPrint(ServiceReferenceHolder.getInstance().getPublicKey()); headerBuilder.keyID(certThumbPrint); headerBuilder.x509CertThumbprint(new Base64URL(certThumbPrint)); SignedJWT signedJWT = new SignedJWT(headerBuilder.build(), jwtClaimsSet); signedJWT.sign(signer); return signedJWT.serialize(); } catch (NoSuchAlgorithmException | CertificateEncodingException | JOSEException e) { throw new AuthException("Invalid signature algorithm provided. " + signatureAlgorithm); } } else { throw new AuthException("Invalid signature algorithm provided. " + signatureAlgorithm); } }
@Test public void nestedJWT() throws Exception { RSAKey senderJWK = (RSAKey) JWK.parse(senderJwkJson); RSAKey recipientPublicJWK = (RSAKey) (JWK.parse(recipientJwkJson)); // Create JWT SignedJWT signedJWT = new SignedJWT( new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(senderJWK.getKeyID()).build(), new JWTClaimsSet.Builder() .subject("testi") .issuer("https:devgluu.saminet.local") .build()); signedJWT.sign(new RSASSASigner(senderJWK)); JWEObject jweObject = new JWEObject( new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM) .contentType("JWT") // required to indicate nested JWT .build(), new Payload(signedJWT)); // Encrypt with the recipient's public key RSAEncrypter encrypter = new RSAEncrypter(recipientPublicJWK); jweObject.encrypt(encrypter); final String jweString = jweObject.serialize(); decryptAndValidateSignatureWithGluu(jweString); }