New Auth Service in Mod2
[dcaegen2/platform.git] / mod2 / auth-service / src / main / java / org / onap / dcaegen2 / platform / mod / security / jwt / JwtUtils.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
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
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
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=========================================================
20  *
21  */
22
23 package org.onap.dcaegen2.platform.mod.security.jwt;
24
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;
36
37 import java.util.Date;
38
39 /**
40  * @author
41  * @date 09/08/2020
42  * JWT Utils
43  */
44 @Slf4j
45 @Component
46 public class JwtUtils {
47
48     @Value("${mod-portal.jwt.secret}")
49     private String jwtSecret;
50
51     @Value("${mod-portal.jwt.jwtExpirationMs}")
52     private int jwtExpirationMs;
53
54     public String generateJwtToken(Authentication authentication) {
55
56         UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal();
57
58         return Jwts.builder()
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)
65                 .compact();
66     }
67
68     public String getUserNameFromJwtToken(String token) {
69         return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject();
70     }
71
72     public boolean validateJwtToken(String authToken) {
73         try {
74             Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken);
75             return true;
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());
86         }
87
88         return false;
89     }
90 }