Documentation updates
[nonrtric/plt/ranpm.git] / pmproducer / src / main / java / org / oran / pmproducer / oauth2 / OAuthBearerTokenJwt.java
1 //  ============LICENSE_START===============================================
2 //  Copyright (C) 2023 Nordix Foundation. All rights reserved.
3 //  ========================================================================
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 //  ============LICENSE_END=================================================
16 //
17
18 package org.oran.pmproducer.oauth2;
19
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import com.fasterxml.jackson.databind.JsonMappingException;
22
23 import java.util.Base64;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import lombok.ToString;
28
29 import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken;
30 import org.oran.pmproducer.exceptions.ServiceException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class OAuthBearerTokenJwt implements OAuthBearerToken {
35     private static final Logger logger = LoggerFactory.getLogger(OAuthBearerTokenJwt.class);
36     private static final com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
37
38     private final String jwtTokenRaw;
39     private final JwtTokenBody tokenBody;
40
41     @ToString
42     private static class JwtTokenBody {
43         String sub = ""; // principalName
44         long exp = 0; // expirationTime
45         long iat = 0; // startTime
46         String scope = "";
47     }
48
49     public static OAuthBearerTokenJwt create(String tokenRaw)
50             throws ServiceException, JsonMappingException, JsonProcessingException {
51         String[] chunks = tokenRaw.split("\\.");
52         Base64.Decoder decoder = Base64.getUrlDecoder();
53         if (chunks.length < 2) {
54             throw new ServiceException("Could not parse JWT token: " + tokenRaw);
55
56         }
57         String payloadStr = new String(decoder.decode(chunks[1]));
58         JwtTokenBody token = gson.fromJson(payloadStr, JwtTokenBody.class);
59         return new OAuthBearerTokenJwt(token, tokenRaw);
60     }
61
62     private OAuthBearerTokenJwt(JwtTokenBody jwtTokenBody, String accessToken) {
63         super();
64         this.jwtTokenRaw = accessToken;
65         this.tokenBody = jwtTokenBody;
66     }
67
68     @Override
69     public String value() {
70         return jwtTokenRaw;
71     }
72
73     @Override
74     public Set<String> scope() {
75         Set<String> res = new HashSet<>();
76         if (!this.tokenBody.scope.isEmpty()) {
77             res.add(this.tokenBody.scope);
78         }
79         return res;
80     }
81
82     @Override
83     public long lifetimeMs() {
84         if (this.tokenBody.exp == 0) {
85             return Long.MAX_VALUE;
86         }
87         return this.tokenBody.exp * 1000;
88     }
89
90     @Override
91     public String principalName() {
92         return this.tokenBody.sub;
93     }
94
95     @Override
96     public Long startTimeMs() {
97         return this.tokenBody.iat;
98     }
99
100 }