Java Code Examples for com.auth0.jwt.algorithms.Algorithm#HMAC384

The following examples show how to use com.auth0.jwt.algorithms.Algorithm#HMAC384 . 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: AlgorithmLinker.java    From JWT4B with GNU General Public License v3.0 5 votes vote down vote up
private static Algorithm getAlgorithm(String algo, String key, boolean IsKeyASignerKey)
		throws IllegalArgumentException, UnsupportedEncodingException {
	if (algo.equals(HS256.getAlgorithm())) {
		return Algorithm.HMAC256(key);
	}
	if (algo.equals(HS384.getAlgorithm())) {
		return Algorithm.HMAC384(key);
	}
	if (algo.equals(HS512.getAlgorithm())) {
		return Algorithm.HMAC512(key);
	}
	if (algo.equals(ES256.getAlgorithm())) {
		return Algorithm.ECDSA256((ECKey) getKeyInstance(key, "EC", IsKeyASignerKey));
	}
	if (algo.equals(ES384.getAlgorithm())) {
		return Algorithm.ECDSA384((ECKey) getKeyInstance(key, "EC", IsKeyASignerKey));
	}
	if (algo.equals(ES512.getAlgorithm())) {
		return Algorithm.ECDSA512((ECKey) getKeyInstance(key, "EC",IsKeyASignerKey));
	}
	if (algo.equals(RS256.getAlgorithm())) {
		return Algorithm.RSA256((RSAKey) getKeyInstance(key, "RSA", IsKeyASignerKey));
	}
	if (algo.equals(RS384.getAlgorithm())) {
		return Algorithm.RSA384((RSAKey) getKeyInstance(key, "RSA", IsKeyASignerKey));
	}
	if (algo.equals(RS512.getAlgorithm())) {
		return Algorithm.RSA512((RSAKey) getKeyInstance(key, "RSA", IsKeyASignerKey));
	}

	return Algorithm.none();
}
 
Example 2
Source File: ConcurrentVerifyTest.java    From java-jwt with MIT License 5 votes vote down vote up
@Test
public void shouldPassHMAC384Verification() throws Exception {
    String token = "eyJhbGciOiJIUzM4NCIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.uztpK_wUMYJhrRv8SV-1LU4aPnwl-EM1q-wJnqgyb5DHoDteP6lN_gE1xnZJH5vw";
    Algorithm algorithm = Algorithm.HMAC384("secret");
    JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build();

    concurrentVerify(verifier, token);
}
 
Example 3
Source File: JwtCreate.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
@Override
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
	// Prep variables
	Builder tokenBuilder = JWT.create();
	String token = "";
	Algorithm algo;

	// Grab the parameters
	String secret			= getNamedStringParam(argStruct, "secret", "" );
	String issuer			= getNamedStringParam(argStruct, "issuer", "" );
	String subject			= getNamedStringParam(argStruct, "subject", "" );
	String audience			= getNamedStringParam(argStruct, "audience", "" );
	Integer expiration		= getNamedIntParam(argStruct, "expiration", -1 );
	String algorithm 		= getNamedStringParam(argStruct, "algorithm", "HMAC256" );
	cfData privateClaims 	= getNamedParam(argStruct, "private");
	
	if (!privateClaims.isStruct())
		throwException(_session, "Parameter isn't of type STRUCTURE");

	try {
		// Set the algorithm, default to HMAC256 if no match
		switch(algorithm) {
		case "HMAC384":
			algo = Algorithm.HMAC384(secret);
			break;
			
		case "HMAC512":
			algo = Algorithm.HMAC512(secret);
			break;
			
		default:
			algo = Algorithm.HMAC256(secret);
			break;
		}
		
	    // Set the public claims
		tokenBuilder.withIssuer(issuer);
	    
	    if (subject.length() > 0) {
			tokenBuilder.withSubject(subject);
		}
	    
	    if (audience.length() > 0) {
			tokenBuilder.withAudience(audience);
		}
	    
	    if (expiration > -1) {
	    		tokenBuilder.withExpiresAt(new Date(expiration));
		}
	    
	    // Set the private claims
	    cfStructData struct = (cfStructData) privateClaims;

	    Object[] thekeys = struct.keys();
	    for ( int i = 0; i < thekeys.length; i++ ) {
	    		String key2 = (String)thekeys[ i ];
	    		cfData val = struct.getData( key2 );
			
	    		if( val.getDataTypeName() == "boolean" ) {
	    			tokenBuilder.withClaim(key2, val.getBoolean());
	    			
	    		} else {
	    			if( cfData.isSimpleValue(val) ){
	    				tokenBuilder.withClaim(key2, val.getString());
	    			} else {
	    				// Let's turn our complex data into json
	    				StringBuilder buffer 			= new StringBuilder(5000);
	    				
	    				// Use the existing openbd json serializer
	    				serializejson jsonserializer 	= new serializejson();
	    				DateType datetype 				= DateType.LONG;
	    				CaseType caseConversion 			= CaseType.MAINTAIN;
	    				
	    				jsonserializer.encodeJSON(buffer, val, false, caseConversion, datetype);
	    				tokenBuilder.withClaim(key2, buffer.toString());
	    			}
	    		}
	    	}
		
		// Sign and stringify final token
		token = tokenBuilder.sign(algo);
	    
	} catch (Exception e) {
		throwException(_session, e.getMessage());
	}	

	return new cfStringData(token);
}
 
Example 4
Source File: JwtVerify.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
@Override
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
	String token 		= getNamedStringParam(argStruct, "token", "" );
	String secret 		= getNamedStringParam(argStruct, "secret", "" );
	String issuer 		= getNamedStringParam(argStruct, "issuer", "" );
	String algorithm 	= getNamedStringParam(argStruct, "algorithm", "HMAC256" );
	
	Algorithm algo;
	Boolean verified = false;
	
	try {
		// Set the algorithm, default to HMAC256 if no match
		switch(algorithm) {
		case "HMAC384":
				algo = Algorithm.HMAC384(secret);
			break;
			
		case "HMAC512":
			algo = Algorithm.HMAC512(secret);
			break;
			
		default:
			algo = Algorithm.HMAC256(secret);
			break;
		}
	
		
		try {
			// If this doesn't throw an error, it's verified
			Verification verifier = JWT.require(algo);
		    
		    	verifier.withIssuer(issuer);
		    
		    @SuppressWarnings("unused")
			DecodedJWT jwt = verifier.build().verify(token);
		    
		    verified = true;
		} catch (JWTVerificationException exception){
			verified = false;
		}
	} catch (Exception e) {
		throwException(_session, e.getMessage());
	}
	
	return (verified == true) ? cfBooleanData.TRUE : cfBooleanData.FALSE;
}
 
Example 5
Source File: SamlRequestIdManager.java    From armeria with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link SamlRequestIdManager} implementation based on JSON Web Tokens specification with
 * the {@link Algorithm} instance using {@code HmacSHA384}.
 *
 * @param issuer the ID of the entity who issues a token
 * @param secret the secret which is used to generate a signature
 * @param validSeconds the valid period of a token in seconds
 * @param leewaySeconds the leeway when there is a clock skew times between the signer and the verifier,
 *                      in seconds.
 */
static SamlRequestIdManager ofJwt(String issuer, String secret,
                                  int validSeconds, int leewaySeconds) throws UnsupportedEncodingException {
    final Algorithm algorithm = Algorithm.HMAC384(requireNonNull(secret, "secret"));
    return ofJwt(issuer, algorithm, validSeconds, leewaySeconds);
}