@Test(expected = JwtException.class) public void testShouldNotGenerateTokenWithNullIdentity() throws Exception { final int EXPIRATION_MILLIS = 60000; LoginAuthenticationToken nullIdentityLoginAuthenticationToken = new LoginAuthenticationToken(null, EXPIRATION_MILLIS, "MockIdentityProvider"); logger.debug("Generating token for " + nullIdentityLoginAuthenticationToken); jwtService.generateSignedToken(nullIdentityLoginAuthenticationToken); }
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);  }