20
20
import io .vertx .core .http .HttpServerResponse ;
21
21
import io .vertx .core .json .JsonObject ;
22
22
import io .vertx .ext .auth .User ;
23
+ import io .vertx .ext .auth .authentication .Credentials ;
24
+ import io .vertx .ext .auth .authentication .TokenCredentials ;
25
+ import io .vertx .ext .auth .authentication .UsernamePasswordCredentials ;
23
26
import io .vertx .ext .auth .authorization .AuthorizationProvider ;
24
27
import io .vertx .ext .auth .authorization .PermissionBasedAuthorization ;
25
28
import io .vertx .ext .auth .authorization .RoleBasedAuthorization ;
26
29
import io .vertx .ext .auth .oauth2 .*;
27
30
import io .vertx .ext .auth .oauth2 .authorization .KeycloakAuthorization ;
28
31
import io .vertx .ext .auth .oauth2 .providers .*;
29
32
30
- import java .util .Arrays ;
31
-
32
33
/**
33
34
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
34
35
*/
36
+ @ SuppressWarnings ("unused" )
35
37
public class AuthOAuth2Examples {
36
38
37
39
public void example1 (Vertx vertx ) {
38
40
39
41
OAuth2Auth oauth2 = OAuth2Auth .create (vertx , new OAuth2Options ()
40
- .setFlow (OAuth2FlowType .AUTH_CODE )
41
42
.setClientId ("YOUR_CLIENT_ID" )
42
43
.setClientSecret ("YOUR_CLIENT_SECRET" )
43
44
.setSite ("https://github.com/login" )
@@ -51,7 +52,7 @@ public void example1(Vertx vertx) {
51
52
52
53
String authorization_uri = oauth2 .authorizeURL (new OAuth2AuthorizationURL ()
53
54
.setRedirectUri ("http://localhost:8080/callback" )
54
- .setScopes ( Arrays . asList ( "notifications" ) )
55
+ .addScope ( "notifications" )
55
56
.setState ("3(#0/!~" ));
56
57
57
58
// when working with web application use the above string as a redirect url
@@ -63,9 +64,9 @@ public void example1(Vertx vertx) {
63
64
String code = "xxxxxxxxxxxxxxxxxxxxxxxx" ;
64
65
65
66
oauth2 .authenticate (
66
- new JsonObject ()
67
- . put ( "code" , code )
68
- . put ( "redirectUri" , "http://localhost:8080/callback" ))
67
+ new Oauth2Credentials ()
68
+ . setCode ( code )
69
+ . setRedirectUri ( "http://localhost:8080/callback" ))
69
70
.onSuccess (user -> {
70
71
// save the token and continue...
71
72
})
@@ -78,7 +79,6 @@ public void example2(Vertx vertx, HttpServerResponse response) {
78
79
79
80
// Set the client credentials and the OAuth2 server
80
81
OAuth2Options credentials = new OAuth2Options ()
81
- .setFlow (OAuth2FlowType .AUTH_CODE )
82
82
.setClientId ("<client-id>" )
83
83
.setClientSecret ("<client-secret>" )
84
84
.setSite ("https://api.oauth.com" );
@@ -90,17 +90,17 @@ public void example2(Vertx vertx, HttpServerResponse response) {
90
90
// Authorization oauth2 URI
91
91
String authorization_uri = oauth2 .authorizeURL (new OAuth2AuthorizationURL ()
92
92
.setRedirectUri ("http://localhost:8080/callback" )
93
- .setScopes ( Arrays . asList ( "<scope>" ) )
93
+ .addScope ( "<scope>" )
94
94
.setState ("<state>" ));
95
95
96
96
// Redirect example using Vert.x
97
97
response .putHeader ("Location" , authorization_uri )
98
98
.setStatusCode (302 )
99
99
.end ();
100
100
101
- JsonObject tokenConfig = new JsonObject ()
102
- .put ( "code" , "<code>" )
103
- .put ( "redirectUri" , "http://localhost:3000/callback" );
101
+ Credentials tokenConfig = new Oauth2Credentials ()
102
+ .setCode ( "<code>" )
103
+ .setRedirectUri ( "http://localhost:3000/callback" );
104
104
105
105
// Callbacks
106
106
// Save the access token
@@ -117,14 +117,10 @@ public void example2(Vertx vertx, HttpServerResponse response) {
117
117
public void example3 (Vertx vertx ) {
118
118
119
119
// Initialize the OAuth2 Library
120
- OAuth2Auth oauth2 = OAuth2Auth .create (
121
- vertx ,
122
- new OAuth2Options ()
123
- .setFlow (OAuth2FlowType .PASSWORD ));
120
+ OAuth2Auth oauth2 = OAuth2Auth .create (vertx );
124
121
125
- JsonObject tokenConfig = new JsonObject ()
126
- .put ("username" , "username" )
127
- .put ("password" , "password" );
122
+ Credentials tokenConfig = new UsernamePasswordCredentials (
123
+ "username" , "password" );
128
124
129
125
oauth2 .authenticate (tokenConfig )
130
126
.onSuccess (user -> {
@@ -146,7 +142,6 @@ public void example4(Vertx vertx) {
146
142
147
143
// Set the client credentials and the OAuth2 server
148
144
OAuth2Options credentials = new OAuth2Options ()
149
- .setFlow (OAuth2FlowType .CLIENT )
150
145
.setClientId ("<client-id>" )
151
146
.setClientSecret ("<client-secret>" )
152
147
.setSite ("https://api.oauth.com" );
@@ -155,7 +150,7 @@ public void example4(Vertx vertx) {
155
150
// Initialize the OAuth2 Library
156
151
OAuth2Auth oauth2 = OAuth2Auth .create (vertx , credentials );
157
152
158
- JsonObject tokenConfig = new JsonObject ( );
153
+ Credentials tokenConfig = new TokenCredentials ( "<token>" );
159
154
160
155
oauth2 .authenticate (tokenConfig )
161
156
.onSuccess (user -> {
@@ -211,9 +206,7 @@ public void example13(Vertx vertx) {
211
206
212
207
// first get a token (authenticate)
213
208
oauth2 .authenticate (
214
- new JsonObject ()
215
- .put ("username" , "user" )
216
- .put ("password" , "secret" ))
209
+ new UsernamePasswordCredentials ("user" , "secret" ))
217
210
.onSuccess (user -> {
218
211
// now check for permissions
219
212
AuthorizationProvider authz = KeycloakAuthorization .create ();
@@ -242,21 +235,21 @@ public void example14(User user) {
242
235
243
236
public void example15 (OAuth2Auth oauth2 , User user ) {
244
237
// OAuth2Auth level
245
- oauth2 .authenticate (new JsonObject (). put ( "access_token" , "opaque string" ))
238
+ oauth2 .authenticate (new TokenCredentials ( "opaque string" ))
246
239
.onSuccess (theUser -> {
247
240
// token is valid!
248
241
});
249
242
250
243
// User level
251
- oauth2 .authenticate (user .principal ( ))
244
+ oauth2 .authenticate (new TokenCredentials ( user .< String > get ( "access_token" ) ))
252
245
.onSuccess (authenticatedUser -> {
253
246
// Token is valid!
254
247
});
255
248
}
256
249
257
250
public void example16 (OAuth2Auth oauth2 ) {
258
251
// OAuth2Auth level
259
- oauth2 .authenticate (new JsonObject (). put ( "access_token" , "jwt-token" ))
252
+ oauth2 .authenticate (new TokenCredentials ( "jwt-token" ))
260
253
.onSuccess (theUser -> {
261
254
// token is valid!
262
255
});
@@ -327,7 +320,7 @@ public void example23(OAuth2Auth oauth2, User user) {
327
320
}
328
321
329
322
public void example24 (OAuth2Auth oauth2 , User user ) {
330
- oauth2 .authenticate (user .principal ( ))
323
+ oauth2 .authenticate (new TokenCredentials ( user .< String > get ( "access_token" ) ))
331
324
.onSuccess (validUser -> {
332
325
// the introspection call succeeded
333
326
})
@@ -341,11 +334,11 @@ public void example24(OAuth2Auth oauth2, User user) {
341
334
public void example25 (Vertx vertx ) {
342
335
343
336
OpenIDConnectAuth .discover (
344
- vertx ,
345
- new OAuth2Options ()
346
- .setClientId ("clientId" )
347
- .setClientSecret ("clientSecret" )
348
- .setSite ("https://accounts.google.com" ))
337
+ vertx ,
338
+ new OAuth2Options ()
339
+ .setClientId ("clientId" )
340
+ .setClientSecret ("clientSecret" )
341
+ .setSite ("https://accounts.google.com" ))
349
342
.onSuccess (oauth2 -> {
350
343
// the setup call succeeded.
351
344
// at this moment your auth is ready to use and
@@ -359,55 +352,55 @@ public void example25(Vertx vertx) {
359
352
public void example25b (Vertx vertx ) {
360
353
// keycloak example
361
354
KeycloakAuth .discover (
362
- vertx ,
363
- new OAuth2Options ()
364
- .setClientId ("clientId" )
365
- .setClientSecret ("clientSecret" )
366
- .setSite ("https://keycloakhost:keycloakport/auth/realms/{realm}" )
367
- .setTenant ("your-realm" ))
355
+ vertx ,
356
+ new OAuth2Options ()
357
+ .setClientId ("clientId" )
358
+ .setClientSecret ("clientSecret" )
359
+ .setSite ("https://keycloakhost:keycloakport/auth/realms/{realm}" )
360
+ .setTenant ("your-realm" ))
368
361
.onSuccess (oauth2 -> {
369
362
// ...
370
363
});
371
364
372
365
// Google example
373
366
GoogleAuth .discover (
374
- vertx ,
375
- new OAuth2Options ()
376
- .setClientId ("clientId" )
377
- .setClientSecret ("clientSecret" ))
367
+ vertx ,
368
+ new OAuth2Options ()
369
+ .setClientId ("clientId" )
370
+ .setClientSecret ("clientSecret" ))
378
371
.onSuccess (oauth2 -> {
379
372
// ...
380
373
});
381
374
382
375
// Salesforce example
383
376
SalesforceAuth .discover (
384
- vertx ,
385
- new OAuth2Options ()
386
- .setClientId ("clientId" )
387
- .setClientSecret ("clientSecret" ))
377
+ vertx ,
378
+ new OAuth2Options ()
379
+ .setClientId ("clientId" )
380
+ .setClientSecret ("clientSecret" ))
388
381
.onSuccess (oauth2 -> {
389
382
// ...
390
383
});
391
384
392
385
// Azure AD example
393
386
AzureADAuth .discover (
394
- vertx ,
395
- new OAuth2Options ()
396
- .setClientId ("clientId" )
397
- .setClientSecret ("clientSecret" )
398
- .setTenant ("your-app-guid" ))
387
+ vertx ,
388
+ new OAuth2Options ()
389
+ .setClientId ("clientId" )
390
+ .setClientSecret ("clientSecret" )
391
+ .setTenant ("your-app-guid" ))
399
392
.onSuccess (oauth2 -> {
400
393
// ...
401
394
});
402
395
403
396
// IBM Cloud example
404
397
IBMCloudAuth .discover (
405
- vertx ,
406
- new OAuth2Options ()
407
- .setClientId ("clientId" )
408
- .setClientSecret ("clientSecret" )
409
- .setSite ("https://<region-id>.appid.cloud.ibm.com/oauth/v4/{tenant}" )
410
- .setTenant ("your-tenant-id" ))
398
+ vertx ,
399
+ new OAuth2Options ()
400
+ .setClientId ("clientId" )
401
+ .setClientSecret ("clientSecret" )
402
+ .setSite ("https://<region-id>.appid.cloud.ibm.com/oauth/v4/{tenant}" )
403
+ .setTenant ("your-tenant-id" ))
411
404
.onSuccess (oauth2 -> {
412
405
// ...
413
406
});
@@ -416,11 +409,11 @@ public void example25b(Vertx vertx) {
416
409
public void example26 (Vertx vertx ) {
417
410
418
411
OpenIDConnectAuth .discover (
419
- vertx ,
420
- new OAuth2Options ()
421
- .setClientId ("clientId" )
422
- .setTenant ("your_realm" )
423
- .setSite ("https://server:port/auth/realms/{tenant}" ))
412
+ vertx ,
413
+ new OAuth2Options ()
414
+ .setClientId ("clientId" )
415
+ .setTenant ("your_realm" )
416
+ .setSite ("https://server:port/auth/realms/{tenant}" ))
424
417
.onSuccess (oauth2 -> {
425
418
// the setup call succeeded.
426
419
// at this moment your auth is ready to use
@@ -444,9 +437,10 @@ public void example22(OAuth2Auth oauth2) {
444
437
// 1. we can inspect the key id, does it make sense?
445
438
if (keyId .equals ("the-new-id" )) {
446
439
// 2. refresh the keys
447
- oauth2 .jWKSet (res -> {
448
- // ...
449
- });
440
+ oauth2 .jWKSet ()
441
+ .onSuccess (v -> {
442
+ // ...
443
+ });
450
444
}
451
445
});
452
446
}
0 commit comments