forked from Decathlon/tzatziki
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2ClientCredentialsStore.java
More file actions
89 lines (77 loc) · 3.26 KB
/
OAuth2ClientCredentialsStore.java
File metadata and controls
89 lines (77 loc) · 3.26 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
package com.decathlon.tzatziki.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Store for OAuth2 client credentials configurations and cached access tokens.
* <p>
* This class manages OAuth2 client credentials (clientId, clientSecret, tokenUrl) and
* caches the fetched access tokens per clientId. Tokens are fetched once when the client
* is registered and cached for the duration of the test scenario.
* </p>
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class OAuth2ClientCredentialsStore {
private static final Map<String, OAuth2ClientConfig> clientConfigs = new ConcurrentHashMap<>();
private static final Map<String, String> accessTokens = new ConcurrentHashMap<>();
/**
* Registers a new OAuth2 client and immediately fetches the access token.
* If the client is already registered with the same configuration, the cached token is reused.
*
* @param clientId the OAuth2 client ID
* @param clientSecret the OAuth2 client secret
* @param tokenUrl the OAuth2 token endpoint URL
* @throws AssertionError if token fetch fails
*/
public static void registerClient(String clientId, String clientSecret, String tokenUrl) {
OAuth2ClientConfig newConfig = new OAuth2ClientConfig(clientId, clientSecret, tokenUrl);
OAuth2ClientConfig existingConfig = clientConfigs.get(clientId);
// Skip if client is already registered with the same configuration
if (newConfig.equals(existingConfig) && accessTokens.containsKey(clientId)) {
return;
}
clientConfigs.put(clientId, newConfig);
// Fetch token immediately and cache it
String accessToken = OAuth2TokenFetcher.fetchAccessToken(clientId, clientSecret, tokenUrl);
accessTokens.put(clientId, accessToken);
}
/**
* Gets the cached access token for the given clientId.
*
* @param clientId the OAuth2 client ID
* @return the cached access token
* @throws AssertionError if no token is found for the clientId
*/
public static String getAccessToken(String clientId) {
String token = accessTokens.get(clientId);
if (token == null) {
throw new AssertionError("No OAuth2 access token found for clientId: " + clientId +
". Please setup authentication first using: Setup authentication for clientId \"" +
clientId + "\" with clientSecret \"...\" and token url \"...\"");
}
return token;
}
/**
* Checks if a client is registered.
*
* @param clientId the OAuth2 client ID
* @return true if the client is registered, false otherwise
*/
public static boolean hasClient(String clientId) {
return clientConfigs.containsKey(clientId);
}
/**
* Resets the store, clearing all cached tokens and configurations.
* Should be called between test scenarios.
*/
public static void reset() {
clientConfigs.clear();
accessTokens.clear();
}
/**
* Internal configuration holder for OAuth2 client credentials.
*/
public record OAuth2ClientConfig(String clientId, String clientSecret, String tokenUrl) {
}
}