JwtUserPayload accessPayload = JwtUserPayload.newBuilder() .withUserId(user.getId()) .withActions(actions) .buildPayload(); JwtUserPayload refreshPayload = JwtUserPayload.newBuilder().withPayload(accessPayload) .buildPayload();
@Override public Response tokenRequest(JwtUserPayloadView payloadView) { JwtUserPayload payload = payloadView.convertTo(); hiveValidator.validate(payload); JwtTokenVO responseTokenVO = new JwtTokenVO(); UserVO user = userService.findById(payload.getUserId()); if (user == null) { logger.warn(String.format(USER_NOT_FOUND, payload.getUserId())); return ResponseFactory.response(NOT_FOUND, new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(USER_NOT_FOUND, payload.getUserId()))); } if (!user.getStatus().equals(UserStatus.ACTIVE)) { logger.warn("JwtToken: User with specified id {} is not active", payload.getUserId()); return ResponseFactory.response(FORBIDDEN, new ErrorResponse(FORBIDDEN.getStatusCode(), USER_NOT_ACTIVE)); } logger.debug("JwtToken: generate access and refresh token"); JwtUserPayload.JwtUserPayloadBuilder refreshPayload = JwtUserPayload.newBuilder().withPayload(payload); if (payloadView.getRefreshExpiration() != null) { refreshPayload.withExpirationDate(payloadView.getRefreshExpiration()); } responseTokenVO.setAccessToken(tokenService.generateJwtAccessToken(payload, true)); responseTokenVO.setRefreshToken(tokenService.generateJwtRefreshToken(refreshPayload.buildPayload(), true)); return ResponseFactory.response(CREATED, responseTokenVO, JsonPolicyDef.Policy.JWT_REFRESH_TOKEN_SUBMITTED); }
/** * Generates a JWT 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 token */ public String generateToken(JwtUserPayload 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); JwtUserPayload generatedPayload = JwtUserPayload.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(); }