SignedJWT jwt = new SignedJWT(new JWSHeader(alg), claims.build());
JWSHeader header = new JWSHeader(signingAlg, null, null, null, null, null, null, null, null, null, jwtService.getDefaultSignerKeyId(), null, null);
JWSHeader header = new JWSHeader(signingAlg, null, null, null, null, null, null, null, null, null, jwtService.getDefaultSignerKeyId(), null, null);
claimsSet.notBeforeTime(now); JWSHeader header = new JWSHeader(alg, null, null, null, null, null, null, null, null, null, signer.getDefaultSignerKeyId(), null, null);
JWSHeader header = new JWSHeader(signingAlgorithm, null, null, null, null, null, null, null, null, null, jwtService.getDefaultSignerKeyId(), null, null);
JWSHeader header = new JWSHeader(signingAlg, null, null, null, null, null, null, null, null, null, jwtService.getDefaultSignerKeyId(), null, null);
|| signingAlg.equals(JWSAlgorithm.HS512)) { JWSHeader header = new JWSHeader(signingAlg, null, null, null, null, null, null, null, null, null, jwtService.getDefaultSignerKeyId(), null, null); idClaims.claim("kid", jwtService.getDefaultSignerKeyId()); JWSHeader header = new JWSHeader(signingAlg, null, null, null, null, null, null, null, null, null, jwtService.getDefaultSignerKeyId(), null, null);
/** * Builds a new JWS header. * * @return The JWS header. */ public JWSHeader build() { return new JWSHeader( alg, typ, cty, crit, jku, jwk, x5u, x5t, x5t256, x5c, kid, customParams, parsedBase64URL); } }
JSONArray taxes= new JSONArray(); JSONArray vat = new JSONArray(); JSONObject a = new JSONObject(); a.put("TaxRate", "A"); a.put("Amount", 100); vat.add(a); JSONObject b = new JSONObject(); b.put("TaxRate", "B"); b.put("Amount", 300); vat.add(b); JSONObject vatObject = new JSONObject(); vatObject.put("VAT", vat); taxes.add(vatObject); JSONObject taxesObject = new JSONObject(); taxesObject.put("Taxes", taxes); // generate string: System.out.println(taxesObject.toJSONString()); // or create JWT: new JWSObject(new JWSHeader(...), new Payload(taxesObject))
@Override public String createToken(String aud, String subject, Long expirationMillis, Map<String, Object> claimMap) { Payload payload = createPayload(aud, subject, expirationMillis, claimMap); // Prepare JWS object JWSObject jwsObject = new JWSObject(new JWSHeader(JWSAlgorithm.HS256), payload); try { // Apply the HMAC jwsObject.sign(signer); } catch (JOSEException e) { throw new RuntimeException(e); } // To serialize to compact form, produces something like // eyJhbGciOiJIUzI1NiJ9.SGVsbG8sIHdvcmxkIQ.onO9Ihudz3WkiauDO2Uhyuz0Y18UASXlSc1eS0NkWyA return jwsObject.serialize(); }
@Override public SignedJWT sign(final JWTClaimsSet claims) { init(); try { final JWSSigner signer = new MACSigner(this.secret); final SignedJWT signedJWT = new SignedJWT(new JWSHeader(algorithm), claims); signedJWT.sign(signer); return signedJWT; } catch (final JOSEException e) { throw new TechnicalException(e); } }
public static JWSObject newJWSObject(JWTUser user) throws JOSEException { JWSSigner signer = new MACSigner(JWT.SHARED_SECRET); JWSObject jwsObject = new JWSObject(new JWSHeader(JWSAlgorithm.HS256), new Payload(user)); jwsObject.sign(signer); return jwsObject; }
@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); } }
@Override public SignedJWT sign(JWTClaimsSet claims) { init(); CommonHelper.assertNotNull("privateKey", privateKey); try { final JWSSigner signer = new ECDSASigner(this.privateKey); final SignedJWT signedJWT = new SignedJWT(new JWSHeader(algorithm), claims); signedJWT.sign(signer); return signedJWT; } catch (final JOSEException e) { throw new TechnicalException(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; } }
public JWTToken(String alg, String[] claimsArray, List<String> audiences) { JWSHeader header = new JWSHeader(new JWSAlgorithm(alg)); if (claimsArray[2] != null) { if (audiences == null) { audiences = new ArrayList<>(); } audiences.add(claimsArray[2]); } JWTClaimsSet claims = null; JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder() .issuer(claimsArray[0]) .subject(claimsArray[1]) .audience(audiences); if(claimsArray[3] != null) { builder = builder.expirationTime(new Date(Long.parseLong(claimsArray[3]))); } claims = builder.build(); jwt = new SignedJWT(header, claims); }
public JWTToken(String alg, String[] claimsArray, List<String> audiences) { JWSHeader header = new JWSHeader(new JWSAlgorithm(alg)); if (claimsArray[2] != null) { if (audiences == null) { audiences = new ArrayList<>(); } audiences.add(claimsArray[2]); } JWTClaimsSet claims; JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder() .issuer(claimsArray[0]) .subject(claimsArray[1]) .audience(audiences); if(claimsArray[3] != null) { builder = builder.expirationTime(new Date(Long.parseLong(claimsArray[3]))); } claims = builder.build(); jwt = new SignedJWT(header, claims); }
@Test(groups = TCKConstants.TEST_GROUP_DEBUG, description = "Validate how to use the HS256 signature alg") public void testHS256() throws Exception { JWTClaimsSet claimsSet = JWTClaimsSet.parse("{\"sub\":\"jdoe\"}"); SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet); SecureRandom random = new SecureRandom(); BigInteger secret = BigInteger.probablePrime(256, random); JWSSigner signer = new MACSigner(secret.toByteArray()); signedJWT.sign(signer); }
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(); }