Search in sources :

Example 1 with OAuth2AuthProviderImpl

use of io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl in project api-framework by vinscom.

the class LoadUserFromSessionRouteBuillder method handle.

public void handle(RoutingContext pRoutingContext) {
    Session session = pRoutingContext.session();
    if (session != null && getOAuth2Auth() != null) {
        JsonObject principal = session.get(FrameworkConstants.Session.PRINCIPAL);
        if (principal != null) {
            OAuth2AuthProviderImpl provider = (OAuth2AuthProviderImpl) getOAuth2Auth().getDelegate();
            try {
                OAuth2TokenImpl token = new OAuth2TokenImpl(provider, principal);
                pRoutingContext.setUser(new AccessToken(token));
            } catch (RuntimeException e) {
                getLog().error(e);
                pRoutingContext.fail(401);
                return;
            }
        }
    }
    pRoutingContext.next();
}
Also used : AccessToken(io.vertx.reactivex.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) OAuth2AuthProviderImpl(io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl) Session(io.vertx.reactivex.ext.web.Session)

Example 2 with OAuth2AuthProviderImpl

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

the class OAuth2UserInfoTest method getUserInfo.

@Test
public void getUserInfo() {
    final AccessToken accessToken = new OAuth2TokenImpl((OAuth2AuthProviderImpl) oauth2, new JsonObject("{\"access_token\":\"eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdXRob3JpemF0aW9uIjp7InBlcm1pc3Npb25zIjpbeyJyZXNvdXJjZV9zZXRfaWQiOiJkMmZlOTg0My02NDYyLTRiZmMtYmFiYS1iNTc4N2JiNmUwZTciLCJyZXNvdXJjZV9zZXRfbmFtZSI6IkhlbGxvIFdvcmxkIFJlc291cmNlIn1dfSwianRpIjoiZDYxMDlhMDktNzhmZC00OTk4LWJmODktOTU3MzBkZmQwODkyLTE0NjQ5MDY2Nzk0MDUiLCJleHAiOjk5OTk5OTk5OTksIm5iZiI6MCwiaWF0IjoxNDY0OTA2NjcxLCJzdWIiOiJmMTg4OGY0ZC01MTcyLTQzNTktYmUwYy1hZjMzODUwNWQ4NmMiLCJ0eXAiOiJrY19ldHQiLCJhenAiOiJoZWxsby13b3JsZC1hdXRoei1zZXJ2aWNlIn0\",\"active\":true,\"scope\":\"scopeA scopeB\",\"client_id\":\"client-id\",\"username\":\"username\",\"token_type\":\"bearer\",\"expires_at\":99999999999000}"));
    accessToken.userInfo(userInfo -> {
        if (userInfo.failed()) {
            fail(userInfo.cause().getMessage());
        } else {
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl) Test(org.junit.Test)

Example 3 with OAuth2AuthProviderImpl

use of io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl 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 4 with OAuth2AuthProviderImpl

use of io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl in project api-framework by vinscom.

the class LoadUserFromAccessTokenRouteBuillder method handle.

public void handle(RoutingContext pRoutingContext) {
    if (pRoutingContext.user() == null) {
        String access_token = pRoutingContext.request().getHeader(HttpHeaders.AUTHORIZATION);
        if (!Strings.isNullOrEmpty(access_token)) {
            OAuth2AuthProviderImpl provider = (OAuth2AuthProviderImpl) getOAuth2Auth().getDelegate();
            JsonObject accessToken = new JsonObject().put("access_token", access_token.split(" ")[1]);
            try {
                OAuth2TokenImpl token = new OAuth2TokenImpl(provider, accessToken);
                pRoutingContext.setUser(new AccessToken(token));
            } catch (RuntimeException e) {
                getLog().error(e);
                pRoutingContext.fail(401);
                return;
            }
        }
    }
    pRoutingContext.next();
}
Also used : AccessToken(io.vertx.reactivex.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) OAuth2AuthProviderImpl(io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)

Example 5 with OAuth2AuthProviderImpl

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

the class OAuth2UserInfoTest method getUserInfoWithParams.

@Test
public void getUserInfoWithParams() {
    final AccessToken accessToken = new OAuth2TokenImpl((OAuth2AuthProviderImpl) oauth2, new JsonObject("{\"access_token\":\"eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdXRob3JpemF0aW9uIjp7InBlcm1pc3Npb25zIjpbeyJyZXNvdXJjZV9zZXRfaWQiOiJkMmZlOTg0My02NDYyLTRiZmMtYmFiYS1iNTc4N2JiNmUwZTciLCJyZXNvdXJjZV9zZXRfbmFtZSI6IkhlbGxvIFdvcmxkIFJlc291cmNlIn1dfSwianRpIjoiZDYxMDlhMDktNzhmZC00OTk4LWJmODktOTU3MzBkZmQwODkyLTE0NjQ5MDY2Nzk0MDUiLCJleHAiOjk5OTk5OTk5OTksIm5iZiI6MCwiaWF0IjoxNDY0OTA2NjcxLCJzdWIiOiJmMTg4OGY0ZC01MTcyLTQzNTktYmUwYy1hZjMzODUwNWQ4NmMiLCJ0eXAiOiJrY19ldHQiLCJhenAiOiJoZWxsby13b3JsZC1hdXRoei1zZXJ2aWNlIn0\",\"active\":true,\"scope\":\"scopeA scopeB\",\"client_id\":\"client-id\",\"username\":\"username\",\"token_type\":\"bearer\",\"expires_at\":99999999999000}"));
    accessToken.userInfo(userInfo -> {
        if (userInfo.failed()) {
            fail(userInfo.cause().getMessage());
        } else {
            assertEquals(fixture, userInfo.result());
            testComplete();
        }
    });
    await();
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) JsonObject(io.vertx.core.json.JsonObject) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl) Test(org.junit.Test)

Aggregations

OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)6 JsonObject (io.vertx.core.json.JsonObject)5 Test (org.junit.Test)4 OAuth2AuthProviderImpl (io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl)3 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)2 AccessToken (io.vertx.reactivex.ext.auth.oauth2.AccessToken)2 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)1 Session (io.vertx.reactivex.ext.web.Session)1