private JwtPayload getJwtPayload(JwtPayload.JwtPayloadBuilder jwtPayloadBuilder, LinkedHashMap<String, Object> payloadMap) { Optional<Long> expiration = Optional.ofNullable((Long)payloadMap.get(EXPIRATION)); Optional<Integer> tokenType = Optional.ofNullable((Integer) payloadMap.get(TOKEN_TYPE)); if (!tokenType.isPresent() && !expiration.isPresent()) { throw new MalformedJwtException("Token type and expiration date should be provided in the token"); } else { if (tokenType.isPresent()) jwtPayloadBuilder.withTokenType(tokenType.get()); else throw new MalformedJwtException("Token type should be provided in the token"); if (expiration.isPresent()) jwtPayloadBuilder.withExpirationDate(new Date(expiration.get())); else throw new MalformedJwtException("Expiration date should be provided in the token"); return jwtPayloadBuilder.buildPayload(); } }
/** * Generates a JWT plugin token containing all needed claims. These properties are taken from the specified * JwtUserPayload object. * * @param payload the payload entity with which the token will be generated * @return the JWT plugin token */ public String generateToken(JwtPluginPayload payload, TokenType tokenType, boolean useExpiration) { long maxAge = tokenType.equals(TokenType.ACCESS) ? accessTokenMaxAge : refreshTokenMaxAge; Date expiration = useExpiration && payload.getExpiration() != null ? payload.getExpiration() : timestampService.getDate(System.currentTimeMillis() + maxAge); JwtPluginPayload generatedPayload = (JwtPluginPayload) JwtPluginPayload.newBuilder() .withPayload(payload) .withExpirationDate(expiration) .withTokenType(tokenType.getId()) .buildPayload(); Map<String, Object> jwtMap = new HashMap<>(); jwtMap.put(JwtUserPayload.JWT_CLAIM_KEY, generatedPayload); Claims claims = Jwts.claims(jwtMap); return Jwts.builder() .setClaims(claims) .signWith(SignatureAlgorithm.HS256, jwtSecretService.getJwtSecret()) .compact(); }