Search in sources :

Example 6 with OAuth2ClientOptions

use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.

the class OAuth2AuthCodeTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:8080"));
    final CountDownLatch latch = new CountDownLatch(1);
    server = vertx.createHttpServer().requestHandler(req -> {
        if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> {
                try {
                    assertEquals(config, queryToJSON(buffer.toString()));
                } catch (UnsupportedEncodingException e) {
                    fail(e);
                }
                req.response().putHeader("Content-Type", "application/json").end(fixture.encode());
            });
        } else {
            req.response().setStatusCode(400).end();
        }
    }).listen(8080, ready -> {
        if (ready.failed()) {
            throw new RuntimeException(ready.cause());
        }
        // ready
        latch.countDown();
    });
    latch.await();
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test) OAuth2API.stringify(io.vertx.ext.auth.oauth2.impl.OAuth2API.stringify) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) VertxTestBase(io.vertx.test.core.VertxTestBase) CountDownLatch(java.util.concurrent.CountDownLatch) URLEncoder(java.net.URLEncoder) User(io.vertx.ext.auth.User) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) AccessToken(io.vertx.ext.auth.oauth2.AccessToken) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) OAuth2API.queryToJSON(io.vertx.ext.auth.oauth2.impl.OAuth2API.queryToJSON) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 7 with OAuth2ClientOptions

use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.

the class OAuth2Auth method createKeycloak.

/**
 * @deprecated You should use the provider helper {@link io.vertx.ext.auth.oauth2.providers.KeycloakAuth} instead.
 *
 * Create a OAuth2 auth provider
 *
 * @param vertx the Vertx instance
 * @param config  the config as exported from the admin console
 * @return the auth provider
 */
@Deprecated
static OAuth2Auth createKeycloak(Vertx vertx, OAuth2FlowType flow, JsonObject config) {
    final OAuth2ClientOptions options = new OAuth2ClientOptions();
    // keycloak conversion to oauth2 options
    if (config.containsKey("auth-server-url")) {
        options.setSite(config.getString("auth-server-url"));
    }
    if (config.containsKey("resource")) {
        options.setClientID(config.getString("resource"));
    }
    if (config.containsKey("credentials") && config.getJsonObject("credentials").containsKey("secret")) {
        options.setClientSecret(config.getJsonObject("credentials").getString("secret"));
    }
    if (config.containsKey("public-client") && config.getBoolean("public-client", false)) {
        options.setUseBasicAuthorizationHeader(true);
    }
    if (config.containsKey("realm")) {
        final String realm = config.getString("realm");
        options.setAuthorizationPath("/realms/" + realm + "/protocol/openid-connect/auth");
        options.setTokenPath("/realms/" + realm + "/protocol/openid-connect/token");
        options.setRevocationPath(null);
        options.setLogoutPath("/realms/" + realm + "/protocol/openid-connect/logout");
        options.setUserInfoPath("/realms/" + realm + "/protocol/openid-connect/userinfo");
    }
    if (config.containsKey("realm-public-key")) {
        options.addPubSecKey(new PubSecKeyOptions().setAlgorithm("RS256").setPublicKey(config.getString("realm-public-key")));
    }
    return new OAuth2AuthProviderImpl(vertx, flow, options);
}
Also used : PubSecKeyOptions(io.vertx.ext.auth.PubSecKeyOptions) OAuth2AuthProviderImpl(io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl)

Example 8 with OAuth2ClientOptions

use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.

the class KeycloakAuth method create.

/**
 * Create a OAuth2Auth provider for Keycloak
 *
 * @param flow              the oauth2 flow to use
 * @param config            the json config file exported from Keycloak admin console
 * @param httpClientOptions custom http client options
 */
static OAuth2Auth create(Vertx vertx, OAuth2FlowType flow, JsonObject config, HttpClientOptions httpClientOptions) {
    final OAuth2ClientOptions options = new OAuth2ClientOptions(httpClientOptions);
    // keycloak conversion to oauth2 options
    if (config.containsKey("auth-server-url")) {
        options.setSite(config.getString("auth-server-url"));
    }
    if (config.containsKey("resource")) {
        options.setClientID(config.getString("resource"));
    }
    if (config.containsKey("credentials") && config.getJsonObject("credentials").containsKey("secret")) {
        options.setClientSecret(config.getJsonObject("credentials").getString("secret"));
    }
    if (config.containsKey("public-client") && config.getBoolean("public-client", false)) {
        options.setUseBasicAuthorizationHeader(true);
    }
    if (config.containsKey("realm")) {
        final String realm = config.getString("realm");
        options.setAuthorizationPath("/realms/" + realm + "/protocol/openid-connect/auth");
        options.setTokenPath("/realms/" + realm + "/protocol/openid-connect/token");
        options.setRevocationPath(null);
        options.setLogoutPath("/realms/" + realm + "/protocol/openid-connect/logout");
        options.setUserInfoPath("/realms/" + realm + "/protocol/openid-connect/userinfo");
        // keycloak follows the RFC7662
        options.setIntrospectionPath("/realms/" + realm + "/protocol/openid-connect/token/introspect");
        // keycloak follows the RFC7517
        options.setJwkPath("/realms/" + realm + "/protocol/openid-connect/certs");
    }
    if (config.containsKey("realm-public-key")) {
        options.addPubSecKey(new PubSecKeyOptions().setAlgorithm("RS256").setPublicKey(config.getString("realm-public-key")));
        // we could load keys
        options.setJWTToken(true);
    }
    return OAuth2Auth.create(vertx, flow, options);
}
Also used : PubSecKeyOptions(io.vertx.ext.auth.PubSecKeyOptions) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions)

Example 9 with OAuth2ClientOptions

use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-web by vert-x3.

the class WebExamples method example59.

public void example59(Vertx vertx, Router router) {
    // create an OAuth2 provider, clientID and clientSecret should be requested to Google
    OAuth2Auth authProvider = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("CLIENT_ID").setClientSecret("CLIENT_SECRET").setSite("https://accounts.google.com").setTokenPath("https://www.googleapis.com/oauth2/v3/token").setAuthorizationPath("/o/oauth2/auth"));
    // create a oauth2 handler on our domain: "http://localhost:8080"
    OAuth2AuthHandler oauth2 = OAuth2AuthHandler.create(authProvider, "http://localhost:8080");
    // these are the scopes
    oauth2.addAuthority("profile");
    // setup the callback handler for receiving the Google callback
    oauth2.setupCallback(router.get("/callback"));
    // protect everything under /protected
    router.route("/protected/*").handler(oauth2);
    // mount some handler under the protected zone
    router.route("/protected/somepage").handler(rc -> rc.response().end("Welcome to the protected resource!"));
    // welcome page
    router.get("/").handler(ctx -> ctx.response().putHeader("content-type", "text/html").end("Hello<br><a href=\"/protected/somepage\">Protected by Google</a>"));
}
Also used : OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth)

Example 10 with OAuth2ClientOptions

use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-web by vert-x3.

the class OAuth2AuthHandlerTest method testAuthCodeFlow.

@Test
public void testAuthCodeFlow() throws Exception {
    // lets mock a oauth2 server using code auth code flow
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:10000"));
    final CountDownLatch latch = new CountDownLatch(1);
    HttpServer server = vertx.createHttpServer().requestHandler(req -> {
        if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> req.response().putHeader("Content-Type", "application/json").end(fixture.encode()));
        } else if (req.method() == HttpMethod.POST && "/oauth/revoke".equals(req.path())) {
            req.setExpectMultipart(true).bodyHandler(buffer -> req.response().end());
        } else {
            req.response().setStatusCode(400).end();
        }
    }).listen(10000, ready -> {
        if (ready.failed()) {
            throw new RuntimeException(ready.cause());
        }
        // ready
        latch.countDown();
    });
    latch.await();
    // create a oauth2 handler on our domain to the callback: "http://localhost:8080/callback"
    OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback");
    // setup the callback handler for receiving the callback
    oauth2Handler.setupCallback(router.route());
    // protect everything under /protected
    router.route("/protected/*").handler(oauth2Handler);
    // mount some handler under the protected zone
    router.route("/protected/somepage").handler(rc -> {
        assertNotNull(rc.user());
        rc.response().end("Welcome to the protected resource!");
    });
    testRequest(HttpMethod.GET, "/protected/somepage", null, resp -> {
        // in this case we should get a redirect
        redirectURL = resp.getHeader("Location");
        assertNotNull(redirectURL);
    }, 302, "Found", null);
    // fake the redirect
    testRequest(HttpMethod.GET, "/callback?state=/protected/somepage&code=1", null, resp -> {
    }, 200, "OK", "Welcome to the protected resource!");
    server.close();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) HttpMethod(io.vertx.core.http.HttpMethod) HttpServer(io.vertx.core.http.HttpServer) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) WebTestBase(io.vertx.ext.web.WebTestBase) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) HttpServer(io.vertx.core.http.HttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) Test(org.junit.Test)

Aggregations

OAuth2ClientOptions (io.vertx.ext.auth.oauth2.OAuth2ClientOptions)14 JsonObject (io.vertx.core.json.JsonObject)11 OAuth2Auth (io.vertx.ext.auth.oauth2.OAuth2Auth)11 Test (org.junit.Test)10 HttpMethod (io.vertx.core.http.HttpMethod)9 HttpServer (io.vertx.core.http.HttpServer)9 OAuth2FlowType (io.vertx.ext.auth.oauth2.OAuth2FlowType)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 VertxTestBase (io.vertx.test.core.VertxTestBase)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)5 User (io.vertx.ext.auth.User)4 OAuth2API.queryToJSON (io.vertx.ext.auth.oauth2.impl.OAuth2API.queryToJSON)4 OAuth2API.stringify (io.vertx.ext.auth.oauth2.impl.OAuth2API.stringify)4 URLEncoder (java.net.URLEncoder)3 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)2 OAuth2API (io.vertx.ext.auth.oauth2.impl.OAuth2API)2 WebTestBase (io.vertx.ext.web.WebTestBase)2 UnknownHostException (java.net.UnknownHostException)2 Base64 (java.util.Base64)2