Skip to content

Commit 09de5a6

Browse files
committed
Backport OAuth2 examples from main branch
See #689 The doc uses deprecated API Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 1f3554a commit 09de5a6

File tree

1 file changed

+60
-66
lines changed

1 file changed

+60
-66
lines changed

vertx-auth-oauth2/src/main/java/examples/AuthOAuth2Examples.java

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,25 @@
2020
import io.vertx.core.http.HttpServerResponse;
2121
import io.vertx.core.json.JsonObject;
2222
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;
2326
import io.vertx.ext.auth.authorization.AuthorizationProvider;
2427
import io.vertx.ext.auth.authorization.PermissionBasedAuthorization;
2528
import io.vertx.ext.auth.authorization.RoleBasedAuthorization;
2629
import io.vertx.ext.auth.oauth2.*;
2730
import io.vertx.ext.auth.oauth2.authorization.KeycloakAuthorization;
2831
import io.vertx.ext.auth.oauth2.providers.*;
2932

30-
import java.util.Arrays;
31-
3233
/**
3334
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
3435
*/
36+
@SuppressWarnings("unused")
3537
public class AuthOAuth2Examples {
3638

3739
public void example1(Vertx vertx) {
3840

3941
OAuth2Auth oauth2 = OAuth2Auth.create(vertx, new OAuth2Options()
40-
.setFlow(OAuth2FlowType.AUTH_CODE)
4142
.setClientId("YOUR_CLIENT_ID")
4243
.setClientSecret("YOUR_CLIENT_SECRET")
4344
.setSite("https://github.com/login")
@@ -51,7 +52,7 @@ public void example1(Vertx vertx) {
5152

5253
String authorization_uri = oauth2.authorizeURL(new OAuth2AuthorizationURL()
5354
.setRedirectUri("http://localhost:8080/callback")
54-
.setScopes(Arrays.asList("notifications"))
55+
.addScope("notifications")
5556
.setState("3(#0/!~"));
5657

5758
// when working with web application use the above string as a redirect url
@@ -63,9 +64,9 @@ public void example1(Vertx vertx) {
6364
String code = "xxxxxxxxxxxxxxxxxxxxxxxx";
6465

6566
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"))
6970
.onSuccess(user -> {
7071
// save the token and continue...
7172
})
@@ -78,7 +79,6 @@ public void example2(Vertx vertx, HttpServerResponse response) {
7879

7980
// Set the client credentials and the OAuth2 server
8081
OAuth2Options credentials = new OAuth2Options()
81-
.setFlow(OAuth2FlowType.AUTH_CODE)
8282
.setClientId("<client-id>")
8383
.setClientSecret("<client-secret>")
8484
.setSite("https://api.oauth.com");
@@ -90,17 +90,17 @@ public void example2(Vertx vertx, HttpServerResponse response) {
9090
// Authorization oauth2 URI
9191
String authorization_uri = oauth2.authorizeURL(new OAuth2AuthorizationURL()
9292
.setRedirectUri("http://localhost:8080/callback")
93-
.setScopes(Arrays.asList("<scope>"))
93+
.addScope("<scope>")
9494
.setState("<state>"));
9595

9696
// Redirect example using Vert.x
9797
response.putHeader("Location", authorization_uri)
9898
.setStatusCode(302)
9999
.end();
100100

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");
104104

105105
// Callbacks
106106
// Save the access token
@@ -117,14 +117,10 @@ public void example2(Vertx vertx, HttpServerResponse response) {
117117
public void example3(Vertx vertx) {
118118

119119
// Initialize the OAuth2 Library
120-
OAuth2Auth oauth2 = OAuth2Auth.create(
121-
vertx,
122-
new OAuth2Options()
123-
.setFlow(OAuth2FlowType.PASSWORD));
120+
OAuth2Auth oauth2 = OAuth2Auth.create(vertx);
124121

125-
JsonObject tokenConfig = new JsonObject()
126-
.put("username", "username")
127-
.put("password", "password");
122+
Credentials tokenConfig = new UsernamePasswordCredentials(
123+
"username", "password");
128124

129125
oauth2.authenticate(tokenConfig)
130126
.onSuccess(user -> {
@@ -146,7 +142,6 @@ public void example4(Vertx vertx) {
146142

147143
// Set the client credentials and the OAuth2 server
148144
OAuth2Options credentials = new OAuth2Options()
149-
.setFlow(OAuth2FlowType.CLIENT)
150145
.setClientId("<client-id>")
151146
.setClientSecret("<client-secret>")
152147
.setSite("https://api.oauth.com");
@@ -155,7 +150,7 @@ public void example4(Vertx vertx) {
155150
// Initialize the OAuth2 Library
156151
OAuth2Auth oauth2 = OAuth2Auth.create(vertx, credentials);
157152

158-
JsonObject tokenConfig = new JsonObject();
153+
Credentials tokenConfig = new TokenCredentials("<token>");
159154

160155
oauth2.authenticate(tokenConfig)
161156
.onSuccess(user -> {
@@ -211,9 +206,7 @@ public void example13(Vertx vertx) {
211206

212207
// first get a token (authenticate)
213208
oauth2.authenticate(
214-
new JsonObject()
215-
.put("username", "user")
216-
.put("password", "secret"))
209+
new UsernamePasswordCredentials("user", "secret"))
217210
.onSuccess(user -> {
218211
// now check for permissions
219212
AuthorizationProvider authz = KeycloakAuthorization.create();
@@ -242,21 +235,21 @@ public void example14(User user) {
242235

243236
public void example15(OAuth2Auth oauth2, User user) {
244237
// OAuth2Auth level
245-
oauth2.authenticate(new JsonObject().put("access_token", "opaque string"))
238+
oauth2.authenticate(new TokenCredentials("opaque string"))
246239
.onSuccess(theUser -> {
247240
// token is valid!
248241
});
249242

250243
// User level
251-
oauth2.authenticate(user.principal())
244+
oauth2.authenticate(new TokenCredentials(user.<String>get("access_token")))
252245
.onSuccess(authenticatedUser -> {
253246
// Token is valid!
254247
});
255248
}
256249

257250
public void example16(OAuth2Auth oauth2) {
258251
// OAuth2Auth level
259-
oauth2.authenticate(new JsonObject().put("access_token", "jwt-token"))
252+
oauth2.authenticate(new TokenCredentials("jwt-token"))
260253
.onSuccess(theUser -> {
261254
// token is valid!
262255
});
@@ -327,7 +320,7 @@ public void example23(OAuth2Auth oauth2, User user) {
327320
}
328321

329322
public void example24(OAuth2Auth oauth2, User user) {
330-
oauth2.authenticate(user.principal())
323+
oauth2.authenticate(new TokenCredentials(user.<String>get("access_token")))
331324
.onSuccess(validUser -> {
332325
// the introspection call succeeded
333326
})
@@ -341,11 +334,11 @@ public void example24(OAuth2Auth oauth2, User user) {
341334
public void example25(Vertx vertx) {
342335

343336
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"))
349342
.onSuccess(oauth2 -> {
350343
// the setup call succeeded.
351344
// at this moment your auth is ready to use and
@@ -359,55 +352,55 @@ public void example25(Vertx vertx) {
359352
public void example25b(Vertx vertx) {
360353
// keycloak example
361354
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"))
368361
.onSuccess(oauth2 -> {
369362
// ...
370363
});
371364

372365
// Google example
373366
GoogleAuth.discover(
374-
vertx,
375-
new OAuth2Options()
376-
.setClientId("clientId")
377-
.setClientSecret("clientSecret"))
367+
vertx,
368+
new OAuth2Options()
369+
.setClientId("clientId")
370+
.setClientSecret("clientSecret"))
378371
.onSuccess(oauth2 -> {
379372
// ...
380373
});
381374

382375
// Salesforce example
383376
SalesforceAuth.discover(
384-
vertx,
385-
new OAuth2Options()
386-
.setClientId("clientId")
387-
.setClientSecret("clientSecret"))
377+
vertx,
378+
new OAuth2Options()
379+
.setClientId("clientId")
380+
.setClientSecret("clientSecret"))
388381
.onSuccess(oauth2 -> {
389382
// ...
390383
});
391384

392385
// Azure AD example
393386
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"))
399392
.onSuccess(oauth2 -> {
400393
// ...
401394
});
402395

403396
// IBM Cloud example
404397
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"))
411404
.onSuccess(oauth2 -> {
412405
// ...
413406
});
@@ -416,11 +409,11 @@ public void example25b(Vertx vertx) {
416409
public void example26(Vertx vertx) {
417410

418411
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}"))
424417
.onSuccess(oauth2 -> {
425418
// the setup call succeeded.
426419
// at this moment your auth is ready to use
@@ -444,9 +437,10 @@ public void example22(OAuth2Auth oauth2) {
444437
// 1. we can inspect the key id, does it make sense?
445438
if (keyId.equals("the-new-id")) {
446439
// 2. refresh the keys
447-
oauth2.jWKSet(res -> {
448-
// ...
449-
});
440+
oauth2.jWKSet()
441+
.onSuccess(v -> {
442+
// ...
443+
});
450444
}
451445
});
452446
}

0 commit comments

Comments
 (0)