Added support for using oauth token for Kafka
[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         logger.error("Token: {}", token);
60         return new OAuthBearerTokenJwt(token, tokenRaw);
61     }
62
63     private OAuthBearerTokenJwt(JwtTokenBody jwtTokenBody, String accessToken) {
64         super();
65         this.jwtTokenRaw = accessToken;
66         this.tokenBody = jwtTokenBody;
67     }
68
69     @Override
70     public String value() {
71         return jwtTokenRaw;
72     }
73
74     @Override
75     public Set<String> scope() {
76         Set<String> res = new HashSet<>();
77         if (!this.tokenBody.scope.isEmpty()) {
78             res.add(this.tokenBody.scope);
79         }
80         return res;
81     }
82
83     @Override
84     public long lifetimeMs() {
85         if (this.tokenBody.exp == 0) {
86             return Long.MAX_VALUE;
87         }
88         return this.tokenBody.exp * 1000;
89     }
90
91     @Override
92     public String principalName() {
93         return this.tokenBody.sub;
94     }
95
96     @Override
97     public Long startTimeMs() {
98         return this.tokenBody.iat;
99     }
100
101 }