-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathSecurityContext.java
More file actions
114 lines (100 loc) · 2.88 KB
/
SecurityContext.java
File metadata and controls
114 lines (100 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* SPDX-FileCopyrightText: 2018-2022 SAP SE or an SAP affiliate company and Cloud Security Client Java contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
package com.sap.cloud.security.token;
import com.sap.cloud.security.x509.Certificate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
/**
* Thread wide {@link Token} storage.
*/
public class SecurityContext {
private static final Logger LOGGER = LoggerFactory.getLogger(SecurityContext.class);
private SecurityContext() {
}
private static final ThreadLocal<Token> tokenStorage = new ThreadLocal<>();
private static final ThreadLocal<Certificate> certificateStorage = new ThreadLocal<>();
/**
* Returns the certificate that is saved in thread wide storage.
*
*
* @return the certificate or null if the storage is empty.
*/
@Nullable
public static Certificate getClientCertificate() {
return certificateStorage.get();
}
/**
* Saves the certificate thread wide.
*
* @param certificate
* certificate to be saved.
*/
public static void setClientCertificate(Certificate certificate) {
LOGGER.info("Sets certificate to SecurityContext (thread-locally). {}",
certificate);
certificateStorage.set(certificate);
}
/**
* Clears the current Certificate from thread wide storage.
*/
private static void clearCertificate() {
final Certificate certificate = certificateStorage.get();
if (certificate != null) {
LOGGER.debug("Certificate removed from SecurityContext (thread-locally).");
certificateStorage.remove();
}
}
/**
* Saves the validated (!) token thread wide.
*
* @param token
* token to be saved.
*/
public static void setToken(Token token) {
LOGGER.info("Sets token of service {} to SecurityContext (thread-locally).",
token != null ? token.getService() : "null");
tokenStorage.set(token);
}
/**
* Returns the token that is saved in thread wide storage.
*
*
* @return the token or null if the storage is empty.
*/
@Nullable
public static Token getToken() {
return tokenStorage.get();
}
/**
* Returns the token that is saved in thread wide storage.
*
*
* @return the token or null if the storage is empty or the token does not
* implement the {@code AccessToken} interface.
*/
@Nullable
public static AccessToken getAccessToken() {
return tokenStorage.get() instanceof AccessToken ? (AccessToken) tokenStorage.get() : null;
}
/**
* Clears the current Token from thread wide storage.
*/
public static void clearToken() {
final Token token = tokenStorage.get();
if (token != null) {
LOGGER.debug("Token of service {} removed from SecurityContext (thread-locally).", token.getService());
tokenStorage.remove();
}
}
/**
* Clears the current token and certificate from thread wide storage.
*/
public static void clear() {
clearCertificate();
clearToken();
}
}