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