Java Code Examples for com.nimbusds.jwt.JWTClaimsSet#parse()
The following examples show how to use
com.nimbusds.jwt.JWTClaimsSet#parse() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TokenUtil.java From peer-os with Apache License 2.0 | 6 votes |
public static String createToken( String headerJson, String claimJson, String sharedKey ) { try { JWSHeader header = JWSHeader.parse( headerJson ); JWSSigner signer = new MACSigner( sharedKey.getBytes() ); JWTClaimsSet claimsSet = JWTClaimsSet.parse( claimJson ); SignedJWT signedJWT = new SignedJWT( header, claimsSet ); signedJWT.sign( signer ); return signedJWT.serialize(); } catch ( Exception e ) { LOG.error( "Error creating token", e.getMessage() ); return ""; } }
Example 2
Source File: Tokens.java From tomee with Apache License 2.0 | 6 votes |
public static String asToken(final String claims) throws Exception { final PrivateKey pk = readPrivateKey("/testkey.pem"); try { final JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256) .type(JOSEObjectType.JWT) .build(); final JWTClaimsSet claimsSet = JWTClaimsSet.parse(claims); final SignedJWT jwt = new SignedJWT(header, claimsSet); jwt.sign(new RSASSASigner(pk)); return jwt.serialize(); } catch (Exception e) { throw new RuntimeException("Could not sign JWT"); } }
Example 3
Source File: Tokens.java From tomee with Apache License 2.0 | 6 votes |
public static String asToken(final String claims) throws Exception { final PrivateKey pk = readPrivateKey("/testkey.pem"); try { final JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256) .type(JOSEObjectType.JWT) .build(); final JWTClaimsSet claimsSet = JWTClaimsSet.parse(claims); final SignedJWT jwt = new SignedJWT(header, claimsSet); jwt.sign(new RSASSASigner(pk)); return jwt.serialize(); } catch (Exception e) { throw new RuntimeException("Could not sign JWT"); } }
Example 4
Source File: Tokens.java From tomee with Apache License 2.0 | 6 votes |
public static String asToken(final String claims) throws Exception { final PrivateKey pk = readPrivateKey("/testkey.pem"); try { final JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256) .type(JOSEObjectType.JWT) .build(); final JWTClaimsSet claimsSet = JWTClaimsSet.parse(claims); final SignedJWT jwt = new SignedJWT(header, claimsSet); jwt.sign(new RSASSASigner(pk)); return jwt.serialize(); } catch (Exception e) { throw new RuntimeException("Could not sign JWT"); } }
Example 5
Source File: Tokens.java From tomee with Apache License 2.0 | 6 votes |
public String asToken(final String claims) throws Exception { try { final JWSHeader header = new JWSHeader.Builder(new JWSAlgorithm("RS"+hashSize, Requirement.OPTIONAL)) .type(JOSEObjectType.JWT) .build(); final JWTClaimsSet claimsSet = JWTClaimsSet.parse(claims); final SignedJWT jwt = new SignedJWT(header, claimsSet); jwt.sign(new RSASSASigner(privateKey)); return jwt.serialize(); } catch (Exception e) { throw new RuntimeException("Could not sign JWT"); } }
Example 6
Source File: BookstoreTest.java From tomee with Apache License 2.0 | 5 votes |
private String token(boolean managerUser) { JSONObject claims = new JSONObject(); claims.put(Claims.iss.name(), "https://server.example.com"); claims.put(Claims.upn.name(), managerUser ? "[email protected]" : "[email protected]"); long currentTimeInSecs = System.currentTimeMillis() / 1000; claims.put(Claims.iat.name(), currentTimeInSecs); claims.put(Claims.auth_time.name(), currentTimeInSecs); claims.put(Claims.exp.name(), currentTimeInSecs + 300); claims.put(Claims.jti.name(), "a-123"); claims.put(Claims.sub.name(), "24400320"); claims.put(Claims.preferred_username.name(), managerUser ? "alice" : "bob"); claims.put(Claims.aud.name(), "s6BhdRkqt3"); List<String> groups = new ArrayList<>(); if (managerUser) { groups.add("manager"); groups.add("reader"); } else { groups.add("reader"); } claims.put(Claims.groups.name(), groups); try { PrivateKey pk = readPrivateKey("/privateKey.pem"); JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256) .keyID("/privateKey.pem") .type(JOSEObjectType.JWT) .build(); JWTClaimsSet claimsSet = JWTClaimsSet.parse(claims); SignedJWT jwt = new SignedJWT(header, claimsSet); jwt.sign(new RSASSASigner(pk)); return jwt.serialize(); } catch (Exception e) { throw new RuntimeException("Could not sign JWT"); } }
Example 7
Source File: TokenUtils.java From Hands-On-Enterprise-Java-Microservices-with-Eclipse-MicroProfile with MIT License | 4 votes |
/** * Utility method to generate a JWT string from a JSON resource file that is signed by the pk * test resource key, possibly with invalid fields. * * @param pk - the private key to sign the token with * @param kid - the kid claim to assign to the token * @param jsonResName - name of test resources file * @param timeClaims - used to return the exp, iat, auth_time claims * @return the JWT string * @throws Exception on parse failure */ public static String generateTokenString(PrivateKey pk, String kid, String jsonResName, Map<String, Long> timeClaims) throws Exception { InputStream contentIS = TokenUtils.class.getResourceAsStream(jsonResName); if (contentIS == null) { throw new IllegalStateException("Failed to find resource: " + jsonResName); } byte[] tmp = new byte[4096]; int length = contentIS.read(tmp); byte[] content = new byte[length]; System.arraycopy(tmp, 0, content, 0, length); JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE); JSONObject jwtContent = parser.parse(content, JSONObject.class); long currentTimeInSecs = currentTimeInSecs(); long exp = currentTimeInSecs + DEFAULT_DURATION; // If exp was passed in, use it if (timeClaims.containsKey(Claims.exp.name())) { exp = timeClaims.get(Claims.exp.name()); } System.out.printf("Setting exp: %d / %s\n", exp, new Date(1000*exp)); long iat = currentTimeInSecs; long authTime = currentTimeInSecs; jwtContent.put(Claims.exp.name(), exp); jwtContent.put(Claims.iat.name(), iat); jwtContent.put(Claims.auth_time.name(), authTime); // Return the token time values if requested if (timeClaims != null) { timeClaims.put(Claims.iat.name(), iat); timeClaims.put(Claims.auth_time.name(), authTime); timeClaims.put(Claims.exp.name(), exp); } // Create RSA-signer with the private key JWSSigner signer = new RSASSASigner(pk); JWTClaimsSet claimsSet = JWTClaimsSet.parse(jwtContent); for (String claim : claimsSet.getClaims().keySet()) { Object claimValue = claimsSet.getClaim(claim); System.out.printf("\tAdded claim: %s, value: %s\n", claim, claimValue); } JWSAlgorithm alg = JWSAlgorithm.RS256; JWSHeader jwtHeader = new JWSHeader.Builder(alg) .keyID(kid) .type(JOSEObjectType.JWT) .build(); SignedJWT signedJWT = new SignedJWT(jwtHeader, claimsSet); signedJWT.sign(signer); return signedJWT.serialize(); }