forked from SAP/cloud-security-services-integration-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityContext.java
More file actions
65 lines (55 loc) · 1.57 KB
/
SecurityContext.java
File metadata and controls
65 lines (55 loc) · 1.57 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
package com.sap.cloud.security.token;
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<>();
/**
* 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();
}
}
}