2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers;
24 import com.auth0.jwt.JWT;
25 import com.auth0.jwt.algorithms.Algorithm;
26 import com.auth0.jwt.exceptions.JWTDecodeException;
27 import com.auth0.jwt.exceptions.JWTVerificationException;
28 import com.auth0.jwt.interfaces.DecodedJWT;
29 import com.auth0.jwt.interfaces.JWTVerifier;
30 import java.util.Arrays;
31 import java.util.Date;
32 import javax.servlet.http.HttpServletRequest;
33 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.data.Config;
34 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.data.UserTokenPayload;
35 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.http.AuthHttpServlet;
36 import org.opendaylight.aaa.shiro.filters.backport.BearerToken;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class TokenCreator {
42 private static final Logger LOG = LoggerFactory.getLogger(AuthHttpServlet.class.getName());
43 private static final long DEFAULT_TOKEN_LIFETIME_MS = 30 * 60 * 1000;
44 private static final String TOKEN_ISSUER = Config.getProperty("${TOKEN_ISSUER}", "ONAP SDNC");
45 private static TokenCreator _instance;
46 private static final String SECRET = Config.getProperty("${TOKEN_SECRET}", "secret");
48 private static final String ROLES_CLAIM = "roles";
49 private static final String FAMILYNAME_CLAIM = "family_name";
50 private static final String NAME_CLAIM = "name";
52 public static TokenCreator getInstance() {
53 if (_instance == null) {
54 _instance = new TokenCreator();
59 private TokenCreator() {
63 public BearerToken createNewJWT(UserTokenPayload data) {
64 Algorithm algorithm = Algorithm.HMAC256(SECRET);
65 final String token = JWT.create().withIssuer(TOKEN_ISSUER).withExpiresAt(new Date(data.getExp()))
66 .withSubject(data.getPreferredUsername()).withClaim(NAME_CLAIM, data.getGivenName())
67 .withClaim(FAMILYNAME_CLAIM, data.getFamilyName())
68 .withArrayClaim(ROLES_CLAIM, data.getRoles().toArray(new String[data.getRoles().size()]))
70 return new BearerToken(token);
73 public DecodedJWT verify(String token) {
74 DecodedJWT jwt = null;
75 LOG.debug("try to verify token {}", token);
77 Algorithm algorithm = Algorithm.HMAC256(SECRET);
78 JWTVerifier verifier = JWT.require(algorithm).withIssuer(TOKEN_ISSUER).build();
79 jwt = verifier.verify(token);
81 } catch (JWTVerificationException e) {
82 LOG.warn("unable to verify token {}:", token, e);
87 public long getDefaultExp() {
88 return new Date().getTime() + DEFAULT_TOKEN_LIFETIME_MS;
91 public UserTokenPayload decode(HttpServletRequest req) throws JWTDecodeException {
92 final String authHeader = req.getHeader("Authorization");
93 if (authHeader == null || !authHeader.startsWith("Bearer")) {
96 DecodedJWT jwt = JWT.decode(authHeader.substring(7));
97 UserTokenPayload data = new UserTokenPayload();
98 data.setRoles(Arrays.asList(jwt.getClaim(ROLES_CLAIM).asArray(String.class)));
99 data.setExp(jwt.getExpiresAt().getTime());
100 data.setFamilyName(jwt.getClaim(FAMILYNAME_CLAIM).asString());
101 data.setGivenName(jwt.getClaim(NAME_CLAIM).asString());
102 data.setPreferredUsername(jwt.getClaim(NAME_CLAIM).asString());