Bugfix, configuration-filepath missing in application.yaml
[nonrtric/plt/ranpm.git] / influxlogger / src / main / java / org / oran / pmlog / oauth2 / SecurityContext.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.pmlog.oauth2;
22
23 import java.lang.invoke.MethodHandles;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26
27 import lombok.Getter;
28 import lombok.Setter;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.boot.context.properties.ConfigurationProperties;
34 import org.springframework.boot.context.properties.EnableConfigurationProperties;
35 import org.springframework.stereotype.Component;
36
37 @EnableConfigurationProperties
38 @ConfigurationProperties()
39 @Component
40 public class SecurityContext {
41
42     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
43
44     private long tokenTimestamp = 0;
45
46     private String authToken = "";
47
48     @Getter
49     private static SecurityContext instance;
50
51     @Setter
52     private Path authTokenFilePath;
53
54     public SecurityContext(@Value("${app.auth-token-file:}") String authTokenFilename) {
55         instance = this;
56         if (!authTokenFilename.isEmpty()) {
57             this.authTokenFilePath = Path.of(authTokenFilename);
58         }
59     }
60
61     public boolean isConfigured() {
62         return authTokenFilePath != null;
63     }
64
65     public synchronized String getBearerAuthToken() {
66         if (!isConfigured()) {
67             logger.warn("No configuration for auth token");
68             return "";
69         }
70         try {
71             long lastModified = authTokenFilePath.toFile().lastModified();
72             if (tokenTimestamp == 0 || lastModified != this.tokenTimestamp) {
73                 this.authToken = Files.readString(authTokenFilePath);
74                 this.authToken = this.authToken.trim();
75                 this.tokenTimestamp = lastModified;
76             }
77         } catch (Exception e) {
78             logger.warn("Could not read auth token file: {}, reason: {}", authTokenFilePath, e.getMessage());
79         }
80         return this.authToken;
81     }
82
83 }