17
17
import java .lang .invoke .MethodHandles ;
18
18
import java .net .URLEncoder ;
19
19
import java .nio .charset .StandardCharsets ;
20
+ import java .time .LocalDateTime ;
20
21
import java .util .AbstractMap ;
21
22
import java .util .HashMap ;
22
23
import java .util .Map ;
@@ -26,65 +27,23 @@ public class SelfManagedAuthentication extends JwtAuthentication {
26
27
27
28
private static final Logger LOG = LoggerFactory .getLogger (MethodHandles .lookup ().lookupClass ());
28
29
29
- private String authUrl ;
30
+ private final String authUrl ;
31
+ private final JsonMapper jsonMapper ;
30
32
31
- // TODO: Check with Identity about upcoming IDPs to abstract this
32
- private String keycloakRealm = "camunda-platform" ;
33
- private String keycloakUrl ;
34
- private String keycloakTokenUrl ;
35
- private JwtConfig jwtConfig ;
36
- private Map <Product , String > tokens ;
37
-
38
- // TODO: have a single object mapper to be used all throughout the SDK, i.e.bean injection
39
- private JsonMapper jsonMapper = new SdkObjectMapper ();
40
-
41
- public SelfManagedAuthentication () {
42
- tokens = new HashMap <>();
33
+ public SelfManagedAuthentication (JwtConfig jwtConfig , String authUrl , JsonMapper jsonMapper ) {
34
+ super (jwtConfig );
35
+ this .authUrl = authUrl ;
36
+ this .jsonMapper = jsonMapper ;
43
37
}
44
38
45
39
public static SelfManagedAuthenticationBuilder builder () {
46
40
return new SelfManagedAuthenticationBuilder ();
47
41
}
48
42
49
- public void setKeycloakRealm (String keycloakRealm ) {
50
- this .keycloakRealm = keycloakRealm ;
51
- }
52
-
53
- public void setKeycloakUrl (String keycloakUrl ) {
54
- this .keycloakUrl = keycloakUrl ;
55
- }
56
-
57
- public void setKeycloakTokenUrl (String keycloakTokenUrl ) {
58
- this .keycloakTokenUrl = keycloakTokenUrl ;
59
- }
60
-
61
- public JwtConfig getJwtConfig () {
62
- return jwtConfig ;
63
- }
64
-
65
- public void setJwtConfig (JwtConfig jwtConfig ) {
66
- this .jwtConfig = jwtConfig ;
67
- }
68
-
69
- @ Override
70
- public Authentication build () {
71
- if (keycloakTokenUrl != null ) {
72
- authUrl = keycloakTokenUrl ;
73
- } else {
74
- authUrl = keycloakUrl +"/auth/realms/" +keycloakRealm +"/protocol/openid-connect/token" ;
75
- }
76
- return this ;
77
- }
78
-
79
- @ Override
80
- public void resetToken (Product product ) {
81
- tokens .remove (product );
82
- }
83
-
84
- private String retrieveToken (Product product , JwtCredential jwtCredential ) {
43
+ private TokenResponse retrieveToken (Product product , JwtCredential jwtCredential ) {
85
44
try (CloseableHttpClient client = HttpClients .createDefault ()) {
86
45
HttpPost request = buildRequest (jwtCredential );
87
- TokenResponse tokenResponse =
46
+ return
88
47
client .execute (
89
48
request ,
90
49
response -> {
@@ -99,12 +58,10 @@ private String retrieveToken(Product product, JwtCredential jwtCredential) {
99
58
+ EntityUtils .toString (response .getEntity ()));
100
59
}
101
60
});
102
- tokens .put (product , tokenResponse .getAccessToken ());
103
61
} catch (Exception e ) {
104
62
LOG .error ("Authenticating for " + product + " failed due to " + e );
105
63
throw new SdkException ("Unable to authenticate" , e );
106
64
}
107
- return tokens .get (product );
108
65
}
109
66
110
67
private HttpPost buildRequest (JwtCredential jwtCredential ) {
@@ -132,15 +89,13 @@ private HttpPost buildRequest(JwtCredential jwtCredential) {
132
89
return httpPost ;
133
90
}
134
91
92
+
93
+
135
94
@ Override
136
- public Map .Entry <String , String > getTokenHeader (Product product ) {
137
- String token ;
138
- if (tokens .containsKey (product )) {
139
- token = tokens .get (product );
140
- } else {
141
- JwtCredential jwtCredential = jwtConfig .getProduct (product );
142
- token = retrieveToken (product , jwtCredential );
143
- }
144
- return new AbstractMap .SimpleEntry <>("Authorization" , "Bearer " + token );
95
+ protected JwtToken generateToken (Product product , JwtCredential credential ) {
96
+ TokenResponse tokenResponse = retrieveToken (product , credential );
97
+ return new JwtToken (
98
+ tokenResponse .getAccessToken (),
99
+ LocalDateTime .now ().plusSeconds (tokenResponse .getExpiresIn ()));
145
100
}
146
101
}
0 commit comments