Search in sources :

Example 81 with Algorithm

use of com.auth0.jwt.Algorithm in project castlemock by castlemock.

the class JWTEncoderDecoder method createToken.

public String createToken(Map<String, String> claims) {
    final Date expiresAt = getExpirationDate();
    final JWTCreator.Builder builder = JWT.create().withIssuer(ISSUER).withExpiresAt(expiresAt);
    claims.forEach(builder::withClaim);
    return builder.sign(algorithm);
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) Date(java.util.Date)

Example 82 with Algorithm

use of com.auth0.jwt.Algorithm in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class JwtUtil method getDecodedJWT.

public static DecodedJWT getDecodedJWT(String authorizationHeader) {
    String token = authorizationHeader.substring("Bearer ".length());
    Algorithm algorithm = Algorithm.HMAC256(SecurityConfig.getSecretKey().getBytes());
    JWTVerifier verifier = JWT.require(algorithm).build();
    return verifier.verify(token);
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier)

Example 83 with Algorithm

use of com.auth0.jwt.Algorithm in project brapi-Java-TestServer by plantbreeding.

the class BrapiTestServerJWTAuthFilter method validateOAuthToken.

private String validateOAuthToken(HttpServletRequest request) {
    try {
        String token = request.getHeader("Authorization");
        if (token != null) {
            token = token.replaceFirst("Bearer ", "");
            RSAPublicKey pubKey = getPublicKey(oidcDiscoveryUrl);
            Algorithm algorithm = Algorithm.RSA256(pubKey, null);
            JWTVerifier verifier = JWT.require(algorithm).withIssuer("https://auth.brapi.org/auth/realms/brapi").build();
            DecodedJWT jwt = verifier.verify(token);
            return jwt.getClaim("email").asString();
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) ServletException(javax.servlet.ServletException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException)

Example 84 with Algorithm

use of com.auth0.jwt.Algorithm in project bank-of-sirius by nginxinc.

the class JWTVerifierGenerator method generateJWTVerifier.

@Bean(name = "verifier")
@ConditionalOnProperty(value = "jwt.account.authentication.enabled", matchIfMissing = true, havingValue = "true")
public JWTVerifier generateJWTVerifier(@Value("${PUB_KEY_PATH}") final String publicKeyPath) throws IOException {
    // load public key from file
    final Path publicKeyFile = Paths.get(publicKeyPath);
    final String keyContents = new String(Files.readAllBytes(publicKeyFile));
    try {
        final String keyStr = keyContents.replaceFirst("-----BEGIN PUBLIC KEY-----", "").replaceFirst("-----END PUBLIC KEY-----", "").replaceAll("\\s", "");
        final byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        final KeyFactory kf = KeyFactory.getInstance("RSA");
        final X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(keyBytes);
        final RSAPublicKey publicKey = (RSAPublicKey) kf.generatePublic(keySpecX509);
        // Initialize JWT verifier.
        final Algorithm algorithm = Algorithm.RSA256(publicKey, null);
        final JWTVerifier verifier = JWT.require(algorithm).build();
        LOGGER.debug("Generated JWT token verifier [algorithm={},publicKeyPath={}]", algorithm.getName(), publicKeyFile);
        return verifier;
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        final String msg = String.format("Cannot generate JWT key [path=%s]", publicKeyFile);
        throw new GenerateKeyException(msg, e);
    }
}
Also used : Path(java.nio.file.Path) RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) KeyFactory(java.security.KeyFactory) Bean(org.springframework.context.annotation.Bean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 85 with Algorithm

use of com.auth0.jwt.Algorithm in project kikaha.rocker by jmilagroso.

the class Security method valid.

/**
 * Validates token
 * @param token The token string parameter.
 * @param id The id string/integer parameter.
 * @param claim The claim string parameter.
 * @param issuer The issuer string parameter.
 * @return boolean Is valid.
 */
public boolean valid(String token, String id, String claim, String issuer) {
    boolean valid = false;
    try {
        verifier = JWT.require(algorithm).withJWTId(id).withClaim("name", claim).withIssuer(issuer).build();
        DecodedJWT jwt = verifier.verify(token);
        valid = true;
    } catch (Exception e) {
        throw new IllegalStateException(e.getLocalizedMessage());
    }
    return valid;
}
Also used : DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Aggregations

Algorithm (com.auth0.jwt.algorithms.Algorithm)206 Test (org.junit.Test)160 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)90 JWTVerifier (com.auth0.jwt.JWTVerifier)79 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)79 ECDSAAlgorithmTest (com.auth0.jwt.algorithms.ECDSAAlgorithmTest)61 Date (java.util.Date)57 ECDSAKeyProvider (com.auth0.jwt.interfaces.ECDSAKeyProvider)51 RSAPublicKey (java.security.interfaces.RSAPublicKey)36 ECPublicKey (java.security.interfaces.ECPublicKey)34 RSAKeyProvider (com.auth0.jwt.interfaces.RSAKeyProvider)31 IOException (java.io.IOException)30 JWTCreator (com.auth0.jwt.JWTCreator)28 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 HashMap (java.util.HashMap)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 JsonObject (com.google.gson.JsonObject)15