Search in sources :

Example 36 with Claim

use of com.auth0.jwt.Claim in project balcaovirtual by trf2-jus-br.

the class CertidaoObterTokenGet method jwt.

public static String jwt(String certidao, String requisitante, String cpfcnpj) throws Exception {
    final String issuer = Utils.getJwtIssuer();
    // issued at claim
    final long iat = System.currentTimeMillis() / 1000L;
    // token expires in 12h
    final long exp = iat + 12 * 60 * 60L;
    final JWTSigner signer = new JWTSigner(Utils.getApiPassword());
    final HashMap<String, Object> claims = new HashMap<String, Object>();
    if (issuer != null)
        claims.put("iss", issuer);
    claims.put("exp", exp);
    claims.put("iat", iat);
    claims.put("certidao", certidao);
    claims.put("requisitante", requisitante);
    claims.put("cpfcnpj", cpfcnpj);
    claims.put("typ", "consulta-certidao");
    final String jwt = signer.sign(claims);
    return jwt;
}
Also used : JWTSigner(com.auth0.jwt.JWTSigner) HashMap(java.util.HashMap)

Example 37 with Claim

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

the class JWTAuthenticator method extractClaimFromBearerToken.

Claim extractClaimFromBearerToken(final String bearerToken, final String claimName) {
    final DecodedJWT jwt;
    try {
        jwt = verifier.verify(bearerToken);
    } catch (JWTVerificationException e) {
        throw new AuthenticationException("Unable to decode or verify JWT bearer token", e);
    }
    final Claim claim = jwt.getClaim(claimName);
    if (claim.isNull()) {
        throw new AuthenticationException("Unable to extract account id claim from JWT bearer token");
    }
    return claim;
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim)

Example 38 with Claim

use of com.auth0.jwt.Claim 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)

Example 39 with Claim

use of com.auth0.jwt.Claim in project yyl_example by Relucent.

the class JwtDemo method main.

public static void main(String[] args) throws Exception {
    long currentMillis = System.currentTimeMillis();
    // JWT 生存时间(5秒)
    long ttl = 5000;
    // 生成JWT的时间
    Date iat = new Date(currentMillis);
    // 生成JWT失效时间
    Date exp = new Date(currentMillis + ttl);
    // 签名秘钥
    String secret = "key";
    // 签发人
    String issuer = "root";
    // 算法
    Algorithm algorithm = Algorithm.HMAC256(secret);
    // 本地的密码解码
    JWTCreator.Builder builder = JWT.create();
    // 签发时间
    builder.withIssuedAt(iat);
    // 签发人
    builder.withIssuer(issuer);
    // 过期时间
    builder.withExpiresAt(exp);
    // 主题
    builder.withClaim("subject", "MySubject");
    String token = builder.sign(algorithm);
    System.out.println(token);
    // 解密
    JWTVerifier verifier = JWT.require(algorithm).withIssuer(issuer).build();
    DecodedJWT jwt = verifier.verify(token);
    Map<String, Claim> claims = jwt.getClaims();
    NullClaim nullClaim = new NullClaim();
    System.out.println(claims.getOrDefault("subject", nullClaim).asString());
    // 等待5秒
    System.out.println("Wait 5 seconds!");
    Thread.sleep(5000);
    try {
        // 这时候Token已经超时了,会抛出异常
        verifier.verify(token);
    } catch (JWTVerificationException e) {
        System.err.println(e);
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JWTCreator(com.auth0.jwt.JWTCreator) NullClaim(com.auth0.jwt.impl.NullClaim) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Date(java.util.Date) NullClaim(com.auth0.jwt.impl.NullClaim) Claim(com.auth0.jwt.interfaces.Claim)

Example 40 with Claim

use of com.auth0.jwt.Claim in project cryptography by norkator.

the class JWT method createECDSA256Jwt.

/**
 * Create elliptic curve based JWT
 *
 * @param privatePem of EC keypair
 * @param issuer     party name
 * @return json web token
 * @throws JWTCreationException if jwt creation fails
 */
public static String createECDSA256Jwt(String privatePem, String issuer) throws InvalidKeySpecException, NoSuchAlgorithmException {
    ECKey privateKey = (ECKey) PEMToKey.getPemPrivateKey(privatePem, "ECDSA");
    Algorithm algorithm = Algorithm.ECDSA256(privateKey);
    return com.auth0.jwt.JWT.create().withIssuer(issuer).withClaim("test claim", "test claim value").sign(algorithm);
}
Also used : ECKey(java.security.interfaces.ECKey) Algorithm(com.auth0.jwt.algorithms.Algorithm)

Aggregations

Claim (com.auth0.jwt.interfaces.Claim)110 Test (org.junit.Test)67 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)62 JsonNode (com.fasterxml.jackson.databind.JsonNode)42 Algorithm (com.auth0.jwt.algorithms.Algorithm)24 Date (java.util.Date)24 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)21 RSAPublicKey (java.security.interfaces.RSAPublicKey)21 Test (org.junit.jupiter.api.Test)18 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)17 JWTVerifier (com.auth0.jwt.JWTVerifier)15 JwksTestKeySource (org.sdase.commons.server.auth.service.testsources.JwksTestKeySource)14 JsonObject (com.google.gson.JsonObject)10 HashMap (java.util.HashMap)9 UserPojo (com.auth0.jwt.UserPojo)8 IOException (java.io.IOException)8 Map (java.util.Map)8 TestingProcessManager (io.supertokens.test.TestingProcessManager)7 NullClaim (com.auth0.jwt.impl.NullClaim)5 JWT (com.auth0.jwt.JWT)4