@Test public void testShouldGenerateSignedToken() throws Exception { final int EXPIRATION_MILLIS = 60000; LoginAuthenticationToken loginAuthenticationToken = new LoginAuthenticationToken("alopresto", EXPIRATION_MILLIS, "MockIdentityProvider"); logger.debug("Generating token for " + loginAuthenticationToken); final String EXPECTED_HEADER = DEFAULT_HEADER; final long TOKEN_EXPIRATION_SEC = (long) (loginAuthenticationToken.getExpiration() / 1000.0); String token = jwtService.generateSignedToken(loginAuthenticationToken); logger.debug("Generated JWT: " + token); final String DECODED_PAYLOAD = new String(Base64.decodeBase64(token.split("\\.")[1].getBytes())); final long ISSUED_AT_SEC = Long.valueOf(DECODED_PAYLOAD.substring(DECODED_PAYLOAD.lastIndexOf(":") + 1, DECODED_PAYLOAD.length() - 1)); logger.trace("Actual token was issued at " + ISSUED_AT_SEC); Map<String, Object> claims = new LinkedHashMap<>(); claims.put("sub", "alopresto"); claims.put("iss", "MockIdentityProvider"); claims.put("aud", "MockIdentityProvider"); claims.put("preferred_username", "alopresto"); claims.put("kid", 1); claims.put("exp", TOKEN_EXPIRATION_SEC); claims.put("iat", ISSUED_AT_SEC); logger.trace("JSON Object to String: " + new JSONObject(claims).toString()); final String EXPECTED_PAYLOAD = new JSONObject(claims).toString(); final String EXPECTED_TOKEN_STRING = generateHS256Token(EXPECTED_HEADER, EXPECTED_PAYLOAD, true, true); logger.debug("Expected JWT: " + EXPECTED_TOKEN_STRING); assertEquals("JWT token", EXPECTED_TOKEN_STRING, token); }
public String generateSignedToken(final LoginAuthenticationToken authenticationToken) throws JwtException { if (authenticationToken == null) { throw new IllegalArgumentException("Cannot generate a JWT for a null authentication token"); } final Calendar expiration = Calendar.getInstance(); expiration.setTimeInMillis(authenticationToken.getExpiration()); final Object principal = authenticationToken.getPrincipal(); if (principal == null || StringUtils.isEmpty(principal.toString())) { final String errorMessage = "Cannot generate a JWT for a token with an empty identity issued by " + authenticationToken.getIssuer(); logger.error(errorMessage); throw new JwtException(errorMessage); } final String identity = principal.toString(); final String username = authenticationToken.getName(); try { final Key key = keyService.getOrCreateKey(identity); final byte[] keyBytes = key.getKey().getBytes(StandardCharsets.UTF_8); logger.trace("Generating JWT for " + authenticationToken); return Jwts.builder().setSubject(identity) .setIssuer(authenticationToken.getIssuer()) .setAudience(authenticationToken.getIssuer()) .claim(USERNAME_CLAIM, username) .claim(KEY_ID_CLAIM, key.getId()) .setExpiration(expiration.getTime()) .setIssuedAt(Calendar.getInstance().getTime()) .signWith(SIGNATURE_ALGORITHM, keyBytes).compact(); } catch (NullPointerException | AdministrationException e) { final String errorMessage = "Could not retrieve the signing key for JWT for " + identity; logger.error(errorMessage, e); throw new JwtException(errorMessage, e); } }
JwtService { public String generateSignedToken(final LoginAuthenticationToken authenticationToken) throws JwtException { if (authenticationToken == null) { throw new IllegalArgumentException("Cannot generate a JWT for a null authentication token"); } final Calendar expiration = Calendar.getInstance(); expiration.setTimeInMillis(authenticationToken.getExpiration()); final Object principal = authenticationToken.getPrincipal(); if (principal == null || StringUtils.isEmpty(principal.toString())) { final String errorMessage = "Cannot generate a JWT for a token with an empty identity issued by " + authenticationToken.getIssuer(); logger.error(errorMessage); throw new JwtException(errorMessage); } final String identity = principal.toString(); final String username = authenticationToken.getName(); try { final Key key = keyService.getOrCreateKey(identity); final byte[] keyBytes = key.getKey().getBytes(StandardCharsets.UTF_8); logger.trace("Generating JWT for " + authenticationToken); return Jwts.builder().setSubject(identity) .setIssuer(authenticationToken.getIssuer()) .setAudience(authenticationToken.getIssuer()) .claim(USERNAME_CLAIM, username) .claim(KEY_ID_CLAIM, key.getId()) .setExpiration(expiration.getTime()) .setIssuedAt(Calendar.getInstance().getTime()) .signWith(SIGNATURE_ALGORITHM, keyBytes).compact(); } catch (NullPointerException | AdministrationException e) { final String errorMessage = "Could not retrieve the signing key for JWT for " + identity; logger.error(errorMessage, e); throw new JwtException(errorMessage, e); } } }
JwtService { public String generateSignedToken(final LoginAuthenticationToken authenticationToken) throws JwtException { if (authenticationToken == null) { throw new IllegalArgumentException("Cannot generate a JWT for a null authentication token"); } final Calendar expiration = Calendar.getInstance(); expiration.setTimeInMillis(authenticationToken.getExpiration()); final Object principal = authenticationToken.getPrincipal(); if (principal == null || StringUtils.isEmpty(principal.toString())) { final String errorMessage = "Cannot generate a JWT for a token with an empty identity issued by " + authenticationToken.getIssuer(); logger.error(errorMessage); throw new JwtException(errorMessage); } final String identity = principal.toString(); final String username = authenticationToken.getName(); try { final Key key = keyService.getOrCreateKey(identity); final byte[] keyBytes = key.getKey().getBytes(StandardCharsets.UTF_8); logger.trace("Generating JWT for " + authenticationToken); return Jwts.builder().setSubject(identity) .setIssuer(authenticationToken.getIssuer()) .setAudience(authenticationToken.getIssuer()) .claim(USERNAME_CLAIM, username) .claim(KEY_ID_CLAIM, key.getId()) .setExpiration(expiration.getTime()) .setIssuedAt(Calendar.getInstance().getTime()) .signWith(SIGNATURE_ALGORITHM, keyBytes).compact(); } catch (NullPointerException | AdministrationException e) { final String errorMessage = "Could not retrieve the signing key for JWT for " + identity; logger.error(errorMessage, e); throw new JwtException(errorMessage, e); } } JwtService(final KeyService keyService); }
JwtService { public String generateSignedToken(final LoginAuthenticationToken authenticationToken) throws JwtException { if (authenticationToken == null) { throw new IllegalArgumentException("Cannot generate a JWT for a null authentication token"); } final Calendar expiration = Calendar.getInstance(); expiration.setTimeInMillis(authenticationToken.getExpiration()); final Object principal = authenticationToken.getPrincipal(); if (principal == null || StringUtils.isEmpty(principal.toString())) { final String errorMessage = "Cannot generate a JWT for a token with an empty identity issued by " + authenticationToken.getIssuer(); logger.error(errorMessage); throw new JwtException(errorMessage); } final String identity = principal.toString(); final String username = authenticationToken.getName(); try { final Key key = keyService.getOrCreateKey(identity); final byte[] keyBytes = key.getKey().getBytes(StandardCharsets.UTF_8); logger.trace("Generating JWT for " + authenticationToken); return Jwts.builder().setSubject(identity) .setIssuer(authenticationToken.getIssuer()) .setAudience(authenticationToken.getIssuer()) .claim(USERNAME_CLAIM, username) .claim(KEY_ID_CLAIM, key.getId()) .setExpiration(expiration.getTime()) .setIssuedAt(Calendar.getInstance().getTime()) .signWith(SIGNATURE_ALGORITHM, keyBytes).compact(); } catch (NullPointerException | AdministrationException e) { final String errorMessage = "Could not retrieve the signing key for JWT for " + identity; logger.error(errorMessage, e); throw new JwtException(errorMessage, e); } } JwtService(final KeyService keyService); String getAuthenticationFromToken(final String base64EncodedToken); String generateSignedToken(final LoginAuthenticationToken authenticationToken); }
JwtService { public String generateSignedToken(final LoginAuthenticationToken authenticationToken) throws JwtException { if (authenticationToken == null) { throw new IllegalArgumentException("Cannot generate a JWT for a null authentication token"); } final Calendar expiration = Calendar.getInstance(); expiration.setTimeInMillis(authenticationToken.getExpiration()); final Object principal = authenticationToken.getPrincipal(); if (principal == null || StringUtils.isEmpty(principal.toString())) { final String errorMessage = "Cannot generate a JWT for a token with an empty identity issued by " + authenticationToken.getIssuer(); logger.error(errorMessage); throw new JwtException(errorMessage); } final String identity = principal.toString(); final String username = authenticationToken.getName(); try { final Key key = keyService.getOrCreateKey(identity); final byte[] keyBytes = key.getKey().getBytes(StandardCharsets.UTF_8); logger.trace("Generating JWT for " + authenticationToken); return Jwts.builder().setSubject(identity) .setIssuer(authenticationToken.getIssuer()) .setAudience(authenticationToken.getIssuer()) .claim(USERNAME_CLAIM, username) .claim(KEY_ID_CLAIM, key.getId()) .setExpiration(expiration.getTime()) .setIssuedAt(Calendar.getInstance().getTime()) .signWith(SIGNATURE_ALGORITHM, keyBytes).compact(); } catch (NullPointerException | AdministrationException e) { final String errorMessage = "Could not retrieve the signing key for JWT for " + identity; logger.error(errorMessage, e); throw new JwtException(errorMessage, e); } } JwtService(final KeyService keyService); String getAuthenticationFromToken(final String base64EncodedToken); String generateSignedToken(final LoginAuthenticationToken authenticationToken);  }