|
19 | 19 | public class SaaSAuthentication extends JwtAuthentication {
|
20 | 20 |
|
21 | 21 | private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
|
22 |
| - private JwtConfig jwtConfig; |
23 |
| - private Map<Product, String> tokens; |
24 | 22 |
|
25 |
| - // TODO: have a single object mapper to be used all throughout the SDK, i.e.bean injection |
26 |
| - private JsonMapper jsonMapper = new SdkObjectMapper(); |
| 23 | + private final JsonMapper jsonMapper; |
27 | 24 |
|
28 |
| - public SaaSAuthentication() { |
29 |
| - tokens = new HashMap<>(); |
| 25 | + public SaaSAuthentication(JwtConfig jwtConfig, JsonMapper jsonMapper) { |
| 26 | + super(jwtConfig); |
| 27 | + this.jsonMapper = jsonMapper; |
30 | 28 | }
|
31 | 29 |
|
32 | 30 | public static SaaSAuthenticationBuilder builder() {
|
33 | 31 | return new SaaSAuthenticationBuilder();
|
34 | 32 | }
|
35 | 33 |
|
36 |
| - public JwtConfig getJwtConfig() { |
37 |
| - return jwtConfig; |
38 |
| - } |
39 |
| - |
40 |
| - public void setJwtConfig(JwtConfig jwtConfig) { |
41 |
| - this.jwtConfig = jwtConfig; |
42 |
| - } |
43 |
| - |
44 |
| - @Override |
45 |
| - public Authentication build() { |
46 |
| - return this; |
47 |
| - } |
48 | 34 |
|
49 |
| - @Override |
50 |
| - public void resetToken(Product product) { |
51 |
| - tokens.remove(product); |
52 |
| - } |
53 |
| - |
54 |
| - private String retrieveToken(Product product, JwtCredential jwtCredential) { |
55 |
| - try(CloseableHttpClient client = HttpClients.createDefault()){ |
56 |
| - HttpPost request = buildRequest(jwtCredential); |
57 |
| - TokenResponse tokenResponse = client.execute(request, response -> |
58 |
| - jsonMapper.fromJson(EntityUtils.toString(response.getEntity()), TokenResponse.class) |
59 |
| - ); |
60 |
| - tokens.put(product, tokenResponse.getAccessToken()); |
61 |
| - } catch (Exception e) { |
| 35 | + private TokenResponse retrieveToken(Product product, JwtCredential jwtCredential) { |
| 36 | + try (CloseableHttpClient client = HttpClients.createDefault()) { |
| 37 | + HttpPost request = buildRequest(jwtCredential); |
| 38 | + return client.execute( |
| 39 | + request, |
| 40 | + response -> |
| 41 | + jsonMapper.fromJson(EntityUtils.toString(response.getEntity()), TokenResponse.class)); |
| 42 | + } catch (Exception e) { |
62 | 43 | LOG.error("Authenticating for " + product + " failed due to " + e);
|
63 | 44 | throw new RuntimeException("Unable to authenticate", e);
|
64 | 45 | }
|
65 |
| - return tokens.get(product); |
66 | 46 | }
|
67 | 47 |
|
68 | 48 | private HttpPost buildRequest(JwtCredential jwtCredential) {
|
69 | 49 | HttpPost httpPost = new HttpPost(jwtCredential.getAuthUrl());
|
70 | 50 | httpPost.addHeader("Content-Type", "application/json");
|
71 |
| - TokenRequest tokenRequest = new TokenRequest(jwtCredential.getAudience(), jwtCredential.getClientId(), jwtCredential.getClientSecret()); |
| 51 | + TokenRequest tokenRequest = |
| 52 | + new TokenRequest( |
| 53 | + jwtCredential.getAudience(), |
| 54 | + jwtCredential.getClientId(), |
| 55 | + jwtCredential.getClientSecret()); |
72 | 56 | httpPost.setEntity(new StringEntity(jsonMapper.toJson(tokenRequest)));
|
73 | 57 | return httpPost;
|
74 | 58 | }
|
75 | 59 |
|
| 60 | + |
| 61 | + |
76 | 62 | @Override
|
77 |
| - public Map.Entry<String, String> getTokenHeader(Product product) { |
78 |
| - String token; |
79 |
| - if (tokens.containsKey(product)) { |
80 |
| - token = tokens.get(product); |
81 |
| - } else { |
82 |
| - JwtCredential jwtCredential = jwtConfig.getProduct(product); |
83 |
| - token = retrieveToken(product, jwtCredential); |
84 |
| - } |
85 |
| - return new AbstractMap.SimpleEntry<>("Authorization", "Bearer " + token); |
| 63 | + protected JwtToken generateToken(Product product, JwtCredential credential) { |
| 64 | + TokenResponse tokenResponse = retrieveToken(product, credential); |
| 65 | + return new JwtToken( |
| 66 | + tokenResponse.getAccessToken(), |
| 67 | + LocalDateTime.now().plusSeconds(tokenResponse.getExpiresIn())); |
86 | 68 | }
|
87 | 69 | }
|
0 commit comments