1
1
package io .camunda .common .auth ;
2
2
3
3
import io .camunda .common .json .JsonMapper ;
4
- import io .camunda .common .json .SdkObjectMapper ;
5
4
import java .lang .invoke .MethodHandles ;
6
- import java .util .AbstractMap ;
7
- import java .util .HashMap ;
8
- import java .util .Map ;
5
+ import java .time .LocalDateTime ;
9
6
import org .apache .hc .client5 .http .classic .methods .HttpPost ;
10
7
import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
11
8
import org .apache .hc .client5 .http .impl .classic .HttpClients ;
17
14
public class SaaSAuthentication extends JwtAuthentication {
18
15
19
16
private static final Logger LOG = LoggerFactory .getLogger (MethodHandles .lookup ().lookupClass ());
20
- private JwtConfig jwtConfig ;
21
- private Map <Product , String > tokens ;
22
17
23
- // TODO: have a single object mapper to be used all throughout the SDK, i.e.bean injection
24
- private JsonMapper jsonMapper = new SdkObjectMapper ();
18
+ private final JsonMapper jsonMapper ;
25
19
26
- public SaaSAuthentication () {
27
- tokens = new HashMap <>();
20
+ public SaaSAuthentication (JwtConfig jwtConfig , JsonMapper jsonMapper ) {
21
+ super (jwtConfig );
22
+ this .jsonMapper = jsonMapper ;
28
23
}
29
24
30
25
public static SaaSAuthenticationBuilder builder () {
31
26
return new SaaSAuthenticationBuilder ();
32
27
}
33
28
34
- public JwtConfig getJwtConfig () {
35
- return jwtConfig ;
36
- }
37
-
38
- public void setJwtConfig (JwtConfig jwtConfig ) {
39
- this .jwtConfig = jwtConfig ;
40
- }
41
-
42
- @ Override
43
- public Authentication build () {
44
- return this ;
45
- }
46
-
47
- @ Override
48
- public void resetToken (Product product ) {
49
- tokens .remove (product );
50
- }
51
-
52
- private String retrieveToken (Product product , JwtCredential jwtCredential ) {
29
+ private TokenResponse retrieveToken (Product product , JwtCredential jwtCredential ) {
53
30
try (CloseableHttpClient client = HttpClients .createDefault ()) {
54
31
HttpPost request = buildRequest (jwtCredential );
55
- TokenResponse tokenResponse =
56
- client .execute (
57
- request ,
58
- response ->
59
- jsonMapper .fromJson (
60
- EntityUtils .toString (response .getEntity ()), TokenResponse .class ));
61
- tokens .put (product , tokenResponse .getAccessToken ());
32
+ return client .execute (
33
+ request ,
34
+ response ->
35
+ jsonMapper .fromJson (EntityUtils .toString (response .getEntity ()), TokenResponse .class ));
62
36
} catch (Exception e ) {
63
37
LOG .error ("Authenticating for " + product + " failed due to " + e );
64
38
throw new RuntimeException ("Unable to authenticate" , e );
65
39
}
66
- return tokens .get (product );
67
40
}
68
41
69
42
private HttpPost buildRequest (JwtCredential jwtCredential ) {
@@ -79,14 +52,10 @@ private HttpPost buildRequest(JwtCredential jwtCredential) {
79
52
}
80
53
81
54
@ Override
82
- public Map .Entry <String , String > getTokenHeader (Product product ) {
83
- String token ;
84
- if (tokens .containsKey (product )) {
85
- token = tokens .get (product );
86
- } else {
87
- JwtCredential jwtCredential = jwtConfig .getProduct (product );
88
- token = retrieveToken (product , jwtCredential );
89
- }
90
- return new AbstractMap .SimpleEntry <>("Authorization" , "Bearer " + token );
55
+ protected JwtToken generateToken (Product product , JwtCredential credential ) {
56
+ TokenResponse tokenResponse = retrieveToken (product , credential );
57
+ return new JwtToken (
58
+ tokenResponse .getAccessToken (),
59
+ LocalDateTime .now ().plusSeconds (tokenResponse .getExpiresIn ()));
91
60
}
92
61
}
0 commit comments