40
40
41
41
public class OidcClientImpl implements OidcClient {
42
42
43
+ private enum Operation {
44
+ Get ,
45
+ Refresh ,
46
+ Revoke
47
+ }
48
+
43
49
private static final Logger LOG = Logger .getLogger (OidcClientImpl .class );
44
50
private static final String CLIENT_ID_ATTRIBUTE = "client-id" ;
45
51
private static final String DEFAULT_OIDC_CLIENT_ID = "Default" ;
@@ -95,7 +101,7 @@ public Uni<Tokens> getTokens(Map<String, String> additionalGrantParameters) {
95
101
throw new OidcClientException (
96
102
"Only 'refresh_token' grant is supported, please call OidcClient#refreshTokens method instead" );
97
103
}
98
- return getJsonResponse (OidcEndpoint .Type .TOKEN , tokenGrantParams , additionalGrantParameters , false );
104
+ return getJsonResponse (OidcEndpoint .Type .TOKEN , tokenGrantParams , additionalGrantParameters , Operation . Get );
99
105
}
100
106
101
107
@ Override
@@ -106,7 +112,7 @@ public Uni<Tokens> refreshTokens(String refreshToken, Map<String, String> additi
106
112
}
107
113
MultiMap refreshGrantParams = copyMultiMap (commonRefreshGrantParams );
108
114
refreshGrantParams .add (OidcConstants .REFRESH_TOKEN_VALUE , refreshToken );
109
- return getJsonResponse (OidcEndpoint .Type .TOKEN , refreshGrantParams , additionalGrantParameters , true );
115
+ return getJsonResponse (OidcEndpoint .Type .TOKEN , refreshGrantParams , additionalGrantParameters , Operation . Refresh );
110
116
}
111
117
112
118
@ Override
@@ -122,7 +128,7 @@ public Uni<Boolean> revokeAccessToken(String accessToken, Map<String, String> ad
122
128
tokenRevokeParams .set (OidcConstants .REVOCATION_TOKEN , accessToken );
123
129
return postRequest (requestProps , OidcEndpoint .Type .TOKEN_REVOCATION , client .postAbs (tokenRevokeUri ),
124
130
tokenRevokeParams ,
125
- additionalParameters , false )
131
+ additionalParameters , Operation . Revoke )
126
132
.transform (resp -> toRevokeResponse (requestProps , resp ));
127
133
} else {
128
134
LOG .debugf ("%s OidcClient can not revoke the access token because the revocation endpoint URL is not set" );
@@ -155,18 +161,18 @@ private Boolean toRevokeResponse(OidcRequestContextProperties requestProps, Http
155
161
private Uni <Tokens > getJsonResponse (
156
162
OidcEndpoint .Type endpointType , MultiMap formBody ,
157
163
Map <String , String > additionalGrantParameters ,
158
- boolean refresh ) {
164
+ Operation op ) {
159
165
//Uni needs to be lazy by default, we don't send the request unless
160
166
//something has subscribed to it. This is important for the CAS state
161
167
//management in TokensHelper
162
- String currentGrantType = refresh ? OidcConstants .REFRESH_TOKEN_GRANT : grantType ;
168
+ String currentGrantType = isRefresh ( op ) ? OidcConstants .REFRESH_TOKEN_GRANT : grantType ;
163
169
final OidcRequestContextProperties requestProps = getRequestProps (currentGrantType );
164
170
return Uni .createFrom ().deferred (new Supplier <Uni <? extends Tokens >>() {
165
171
@ Override
166
172
public Uni <Tokens > get () {
167
173
return postRequest (requestProps , endpointType , client .postAbs (tokenRequestUri ), formBody ,
168
- additionalGrantParameters , refresh )
169
- .transform (resp -> emitGrantTokens (requestProps , resp , refresh ));
174
+ additionalGrantParameters , op )
175
+ .transform (resp -> emitGrantTokens (requestProps , resp , op ));
170
176
}
171
177
});
172
178
}
@@ -176,7 +182,7 @@ private UniOnItem<HttpResponse<Buffer>> postRequest(
176
182
OidcEndpoint .Type endpointType , HttpRequest <Buffer > request ,
177
183
MultiMap formBody ,
178
184
Map <String , String > additionalGrantParameters ,
179
- boolean refresh ) {
185
+ Operation op ) {
180
186
MultiMap body = formBody ;
181
187
request .putHeader (HttpHeaders .CONTENT_TYPE .toString (),
182
188
HttpHeaders .APPLICATION_X_WWW_FORM_URLENCODED .toString ());
@@ -199,14 +205,14 @@ private UniOnItem<HttpResponse<Buffer>> postRequest(
199
205
if (clientAssertion == null ) {
200
206
String errorMessage = String .format (
201
207
"%s OidcClient can not complete the %s grant request because a JWT bearer client_assertion is missing" ,
202
- oidcConfig .id ().get (), (refresh ? OidcConstants .REFRESH_TOKEN_GRANT : grantType ));
208
+ oidcConfig .id ().get (), (isRefresh ( op ) ? OidcConstants .REFRESH_TOKEN_GRANT : grantType ));
203
209
LOG .error (errorMessage );
204
210
throw new OidcClientException (errorMessage );
205
211
}
206
212
body .set (OidcConstants .CLIENT_ASSERTION_TYPE , OidcConstants .JWT_BEARER_CLIENT_ASSERTION_TYPE );
207
213
} else if (clientJwtKey != null ) {
208
214
// if it is a refresh then a map has already been copied
209
- body = !refresh ? copyMultiMap (body ) : body ;
215
+ body = !isRefresh ( op ) ? copyMultiMap (body ) : body ;
210
216
String jwt = OidcCommonUtils .signJwtWithKey (oidcConfig , tokenRequestUri , clientJwtKey );
211
217
212
218
if (OidcCommonUtils .isClientSecretPostJwtAuthRequired (oidcConfig .credentials ())) {
@@ -227,11 +233,11 @@ private UniOnItem<HttpResponse<Buffer>> postRequest(
227
233
body .add (OidcConstants .CLIENT_ASSERTION , jwt );
228
234
}
229
235
} else if (OidcCommonUtils .isClientSecretPostAuthRequired (oidcConfig .credentials ())) {
230
- body = !refresh ? copyMultiMap (body ) : body ;
236
+ body = !isRefresh ( op ) ? copyMultiMap (body ) : body ;
231
237
body .set (OidcConstants .CLIENT_ID , oidcConfig .clientId ().get ());
232
238
body .set (OidcConstants .CLIENT_SECRET , OidcCommonUtils .clientSecret (oidcConfig .credentials ()));
233
239
} else {
234
- body = !refresh ? copyMultiMap (body ) : body ;
240
+ body = !isRefresh ( op ) ? copyMultiMap (body ) : body ;
235
241
body = copyMultiMap (body ).set (OidcConstants .CLIENT_ID , oidcConfig .clientId ().get ());
236
242
}
237
243
if (!additionalGrantParameters .isEmpty ()) {
@@ -241,7 +247,8 @@ private UniOnItem<HttpResponse<Buffer>> postRequest(
241
247
}
242
248
}
243
249
if (LOG .isDebugEnabled ()) {
244
- LOG .debugf ("Token endpoint: %s, request params: %s, headers: %s" , request .uri (), body , request .headers ());
250
+ LOG .debugf ("%s token: url : %s, headers: %s, request params: %s" , op .name (), request .uri (), request .headers (),
251
+ body );
245
252
}
246
253
// Retry up to three times with a one-second delay between the retries if the connection is closed
247
254
Buffer buffer = OidcCommonUtils .encodeForm (body );
@@ -258,10 +265,10 @@ private UniOnItem<HttpResponse<Buffer>> postRequest(
258
265
return response .onItem ();
259
266
}
260
267
261
- private Tokens emitGrantTokens (OidcRequestContextProperties requestProps , HttpResponse <Buffer > resp , boolean refresh ) {
268
+ private Tokens emitGrantTokens (OidcRequestContextProperties requestProps , HttpResponse <Buffer > resp , Operation op ) {
262
269
Buffer buffer = OidcCommonUtils .filterHttpResponse (requestProps , resp , responseFilters , OidcEndpoint .Type .TOKEN );
263
270
if (resp .statusCode () == 200 ) {
264
- LOG .debugf ("%s OidcClient has %s the tokens" , oidcConfig .id ().get (), (refresh ? "refreshed" : "acquired" ));
271
+ LOG .debugf ("%s OidcClient has %s the tokens" , oidcConfig .id ().get (), (isRefresh ( op ) ? "refreshed" : "acquired" ));
265
272
JsonObject json = buffer .toJsonObject ();
266
273
// access token
267
274
final String accessToken = json .getString (oidcConfig .grant ().accessTokenProperty ());
@@ -277,7 +284,7 @@ private Tokens emitGrantTokens(OidcRequestContextProperties requestProps, HttpRe
277
284
} else {
278
285
String errorMessage = buffer .toString ();
279
286
LOG .debugf ("%s OidcClient has failed to complete the %s grant request: status: %d, error message: %s" ,
280
- oidcConfig .id ().get (), (refresh ? OidcConstants .REFRESH_TOKEN_GRANT : grantType ), resp .statusCode (),
287
+ oidcConfig .id ().get (), (isRefresh ( op ) ? OidcConstants .REFRESH_TOKEN_GRANT : grantType ), resp .statusCode (),
281
288
errorMessage );
282
289
throw new OidcClientException (errorMessage );
283
290
}
@@ -372,4 +379,8 @@ private HttpRequest<Buffer> filterHttpRequest(
372
379
OidcClientConfig getConfig () {
373
380
return oidcConfig ;
374
381
}
382
+
383
+ static boolean isRefresh (Operation op ) {
384
+ return op == Operation .Refresh ;
385
+ }
375
386
}
0 commit comments