/** * Creates a new and reusable instance of the JWTVerifier with the configuration already provided. * ONLY FOR TEST PURPOSES. * * @param clock the instance that will handle the current time. * @return a new JWTVerifier instance with a custom Clock. */ public JWTVerifier build(Clock clock) { addLeewayToDateClaims(); return new JWTVerifier(algorithm, claims, clock); }
@Test public void shouldThrowOnInvalidCustomClaimValue() throws Exception { exception.expect(InvalidClaimException.class); exception.expectMessage("The Claim 'name' value doesn't match the required one."); String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjpbInNvbWV0aGluZyJdfQ.3ENLez6tU_fG0SVFrGmISltZPiXLSHaz_dyn-XFTEGQ"; Map<String, Object> map = new HashMap<>(); map.put("name", new Object()); JWTVerifier verifier = new JWTVerifier(Algorithm.HMAC256("secret"), map, new ClockImpl()); verifier.verify(token); }
public JWTAuthenticationFilter(final String jwtSecret, final JWTCookieGenerator jwtCookieGenerator) { this.jwtVerifier = new JWTVerifier(jwtSecret); this.jwtCookieGenerator = jwtCookieGenerator; }
public JwtServerInterceptor(String secret) { this.secret = secret; this.verifier = new JWTVerifier(secret); }
public JWTAuthenticationFilter(final String jwtSecret, final JWTCookieGenerator jwtCookieGenerator) { this.jwtVerifier = new JWTVerifier(jwtSecret); this.jwtCookieGenerator = jwtCookieGenerator; }
/** * Creates a new and reusable instance of the JWTVerifier with the configuration already provided. * ONLY FOR TEST PURPOSES. * * @param clock the instance that will handle the current time. * @return a new JWTVerifier instance with a custom Clock. */ public JWTVerifier build(Clock clock) { addLeewayToDateClaims(); return new JWTVerifier(algorithm, claims, clock); }
final String secret = "{{secret used for signing}}"; try { final JWTVerifier verifier = new JWTVerifier(secret); final Map<String, Object> claims= verifier.verify(jwt); } catch (JWTVerifyException e) { // Invalid Token }
private static String superdupersecret = RandomStringUtils.randomAlphanumeric(24); private static JWTSigner signer = new JWTSigner(superdupersecret); private static JWTVerifier verifier = new JWTVerifier(superdupersecret);
/** * get the object of jwt if not expired * * @param jwt * @return POJO object */ public static <T> T unsign(String jwt, Class<T> classT) { final JWTVerifier verifier = new JWTVerifier(SIGN); try { final Map<String, Object> claims = verifier.verify(jwt); if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) { long exp = (Long) claims.get(EXP); long currentTimeMillis = System.currentTimeMillis(); if (exp > currentTimeMillis) { String json = (String) claims.get(PAYLOAD); return JSONObject.parseObject(json, classT); } } return null; } catch (Exception e) { return null; } }
public static LoginUserInfo unsign(String jwt) { final JWTVerifier verifier = new JWTVerifier(SECRET); try { final Map<String,Object> claims= verifier.verify(jwt); if (claims.containsKey(PAYLOAD)&&claims.containsKey(USERID)) { String json = (String)claims.get(PAYLOAD); String userId = claims.get(USERID).toString(); LoginUserInfo user = JsonUtils.getMapper().readValue(json,LoginUserInfo.class); if (userId.equals(user.getId().toString())){ return user; } } return null; } catch (Exception e) { return null; } }
/** * * @param request * @param response * @return */ public Object validateJwt(Request request, Response response) { try { String jwt = request.getHeader(Constants.Url.JWT, "No JWT supplied"); String alg = getSignatureAlgorithm(jwt); String apiKey = getApiKey(jwt); String secret = getSecret(apiKey); Map<String, Object> decodedPayload = new JWTVerifier(secret).verify(jwt); if (!alg.equals("HS256") // prevent hack using 'none' || Long.parseLong(decodedPayload.get("exp").toString()) <= System.currentTimeMillis() / 1000) { return false; } } catch (RuntimeException | NoSuchAlgorithmException | InvalidKeyException | IOException | SignatureException | JWTVerifyException e) { LOG.error("validateJwt() failed: " + ExceptionUtils.getRootCauseMessage(e)); LOG.debug("validateJwt() failed: " + ExceptionUtils.getStackTrace(e)); return false; } return true; }
jwtVerifier = new JWTVerifier(base64.decode(clientSecret), clientId, issuer); } else { jwtVerifier = new JWTVerifier(clientSecret, clientId, issuer); final PublicKey publicKey = readPublicKey(publicKeyRealPath); Validate.notNull(publicKey); jwtVerifier = new JWTVerifier(publicKey, clientId, issuer); return; } catch (Exception e) {
@Test public void testConstructToken() throws Exception { String constructedToken = auth.constructToken(1234, "1111111"); byte[] keyBytes = testUtils.loadKey("test/keys/application_public_key.der"); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey key = kf.generatePublic(spec); final JWTVerifier verifier = new JWTVerifier(key); final Map<String, Object> claims = verifier.verify(constructedToken); assertEquals(1234, claims.get("iat")); assertEquals("1111111", claims.get("jti")); assertEquals("application-id", claims.get("application_id")); }
final Map<String, Object> claims = new JWTVerifier(jwtSecret).verify(registerUserEntity.getToken()); String username = claims.get(Claims.SUBJECT).toString();
final Map<String, Object> claims = new JWTVerifier(jwtSecret).verify(registerUserEntity.getToken()); String username = claims.get(Claims.SUBJECT).toString();
@Test public void testGenerateJwt() throws Exception { byte[] privateKeyBytes = testUtils.loadKey("test/keys/application_key"); NexmoClient client = new NexmoClient.Builder() .applicationId("application-id") .privateKeyContents(privateKeyBytes) .build(); String constructedToken = client.generateJwt(); byte[] publicKeyBytes = testUtils.loadKey("test/keys/application_public_key.der"); X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey key = kf.generatePublic(spec); final JWTVerifier verifier = new JWTVerifier(key); final Map<String, Object> claims = verifier.verify(constructedToken); assertEquals("application-id", claims.get("application_id")); }