public static TokenType getById(Integer id) { return Arrays.stream(values()) .filter(tokenType -> tokenType.getId().equals(id)) .findFirst() .orElseThrow(() -> new HiveException("No such a token type")); } }
@Override public Response refreshTokenRequest(JwtRefreshTokenVO requestTokenVO) { hiveValidator.validate(requestTokenVO); JwtPayload payload; try { payload = tokenService.getPayload(requestTokenVO.getRefreshToken()); } catch (JwtException e) { logger.error(e.getMessage()); return ResponseFactory.response(UNAUTHORIZED); } if (!payload.getTokenType().equals(TokenType.REFRESH.getId())) { logger.warn("JwtToken: refresh token is not valid"); return ResponseFactory.response(UNAUTHORIZED, new ErrorResponse(UNAUTHORIZED.getStatusCode(), INVALID_TOKEN_TYPE)); } if (payload.getExpiration().before(timestampService.getDate())) { logger.warn("JwtToken: refresh token has expired"); return ResponseFactory.response(UNAUTHORIZED, new ErrorResponse(UNAUTHORIZED.getStatusCode(), EXPIRED_TOKEN)); } return payload.isUserPayload() ? getRefreshResponse((JwtUserPayload) payload) : getRefreshResponse((JwtPluginPayload) payload); }
jwtPluginPayload.getTokenType().equals(TokenType.REFRESH.getId())) { logger.warn(INVALID_TOKEN_TYPE); return ResponseFactory.response(UNAUTHORIZED,
/** * 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(); }
/** * 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(); }
jwtPayload.getTokenType().equals(TokenType.REFRESH.getId())) { throw new BadCredentialsException("Unauthorized");