3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
7 * * ================================================================================
8 * * Licensed under the Apache License, Version 2.0 (the "License");
9 * * you may not use this file except in compliance with the License.
10 * * You may obtain a copy of the License at
12 * * http://www.apache.org/licenses/LICENSE-2.0
14 * * Unless required by applicable law or agreed to in writing, software
15 * * distributed under the License is distributed on an "AS IS" BASIS,
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * * See the License for the specific language governing permissions and
18 * * limitations under the License.
19 * * ============LICENSE_END=========================================================
23 package org.onap.dcaegen2.platform.mod.security.jwt;
25 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsImpl;
26 import io.jsonwebtoken.Jwts;
27 import io.jsonwebtoken.SignatureAlgorithm;
28 import io.jsonwebtoken.SignatureException;
29 import io.jsonwebtoken.MalformedJwtException;
30 import io.jsonwebtoken.ExpiredJwtException;
31 import io.jsonwebtoken.UnsupportedJwtException;
32 import lombok.extern.slf4j.Slf4j;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.security.core.Authentication;
35 import org.springframework.stereotype.Component;
37 import java.util.Date;
46 public class JwtUtils {
48 @Value("${mod-portal.jwt.secret}")
49 private String jwtSecret;
51 @Value("${mod-portal.jwt.jwtExpirationMs}")
52 private int jwtExpirationMs;
54 public String generateJwtToken(Authentication authentication) {
56 UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal();
59 .setSubject((userPrincipal.getUsername()))
60 .claim("roles", userPrincipal.getAuthoritiesAsList())
61 .claim("fullName", userPrincipal.getFullName())
62 .setIssuedAt(new Date())
63 .setExpiration(new Date((new Date()).getTime() + jwtExpirationMs))
64 .signWith(SignatureAlgorithm.HS512, jwtSecret)
68 public String getUserNameFromJwtToken(String token) {
69 return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject();
72 public boolean validateJwtToken(String authToken) {
74 Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken);
76 } catch (SignatureException e) {
77 log.error("Invalid JWT signature: {}", e.getMessage());
78 } catch (MalformedJwtException e) {
79 log.error("Invalid JWT token: {}", e.getMessage());
80 } catch (ExpiredJwtException e) {
81 log.error("JWT token is expired: {}", e.getMessage());
82 } catch (UnsupportedJwtException e) {
83 log.error("JWT token is unsupported: {}", e.getMessage());
84 } catch (IllegalArgumentException e) {
85 log.error("JWT claims string is empty: {}", e.getMessage());