Search in sources :

Example 6 with OAuth2TokenImpl

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

the class OAuth2UserSerializationTest method loadUser.

@Test
public void loadUser() {
    OAuth2Auth provider = KeycloakAuth.create(Vertx.vertx(), OAuth2FlowType.AUTH_CODE, keycloakConfig);
    OAuth2TokenImpl user = new OAuth2TokenImpl();
    System.out.println(keycloakToken.length());
    user.readFromBuffer(0, Buffer.buffer().appendInt(0).appendInt(keycloakToken.length()).appendString(keycloakToken));
    user.setAuthProvider(provider);
}
Also used : OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl) Test(org.junit.Test)

Example 7 with OAuth2TokenImpl

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

the class OAuth2TokenImpl method refresh.

/**
 * Refresh the access token
 *
 * @param handler - The callback function returning the results.
 */
@Override
public OAuth2TokenImpl refresh(Handler<AsyncResult<Void>> handler) {
    final JsonObject headers = new JsonObject();
    JsonObject tmp = provider.getConfig().getHeaders();
    if (tmp != null) {
        headers.mergeIn(tmp);
    }
    final JsonObject form = new JsonObject();
    form.put("grant_type", "refresh_token").put("refresh_token", opaqueRefreshToken()).put("client_id", provider.getConfig().getClientID());
    if (provider.getConfig().getClientSecretParameterName() != null) {
        form.put(provider.getConfig().getClientSecretParameterName(), provider.getConfig().getClientSecret());
    }
    headers.put("Content-Type", "application/x-www-form-urlencoded");
    final Buffer payload = Buffer.buffer(stringify(form));
    // specify preferred accepted accessToken type
    headers.put("Accept", "application/json,application/x-www-form-urlencoded;q=0.9");
    OAuth2API.fetch(provider, HttpMethod.POST, provider.getConfig().getTokenPath(), headers, payload, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
            return;
        }
        final OAuth2Response reply = res.result();
        if (reply.body() == null || reply.body().length() == 0) {
            handler.handle(Future.failedFuture("No Body"));
            return;
        }
        JsonObject json;
        if (reply.is("application/json")) {
            try {
                json = reply.jsonObject();
            } catch (RuntimeException e) {
                handler.handle(Future.failedFuture(e));
                return;
            }
        } else if (reply.is("application/x-www-form-urlencoded") || reply.is("text/plain")) {
            try {
                json = queryToJSON(reply.body().toString());
            } catch (UnsupportedEncodingException | RuntimeException e) {
                handler.handle(Future.failedFuture(e));
                return;
            }
        } else {
            handler.handle(Future.failedFuture("Cannot handle accessToken type: " + reply.headers().get("Content-Type")));
            return;
        }
        try {
            if (json.containsKey("error")) {
                String description;
                Object error = json.getValue("error");
                if (error instanceof JsonObject) {
                    description = ((JsonObject) error).getString("message");
                } else {
                    // attempt to handle the error as a string
                    try {
                        description = json.getString("error_description", json.getString("error"));
                    } catch (RuntimeException e) {
                        description = error.toString();
                    }
                }
                handler.handle(Future.failedFuture(description));
            } else {
                OAuth2API.processNonStandardHeaders(json, reply, provider.getConfig().getScopeSeparator());
                token = json;
                init();
                handler.handle(Future.succeededFuture());
            }
        } catch (RuntimeException e) {
            handler.handle(Future.failedFuture(e));
        }
    });
    return this;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) OAuth2Response(io.vertx.ext.auth.oauth2.OAuth2Response) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Example 8 with OAuth2TokenImpl

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

the class OAuth2TokenImpl method revoke.

/**
 * Revoke access or refresh token
 *
 * @param token_type - A String containing the type of token to revoke. Should be either "access_token" or "refresh_token".
 * @param handler    - The callback function returning the results.
 */
@Override
public OAuth2TokenImpl revoke(String token_type, Handler<AsyncResult<Void>> handler) {
    final String tokenValue = token.getString(token_type);
    if (tokenValue != null) {
        final JsonObject headers = new JsonObject();
        JsonObject tmp = provider.getConfig().getHeaders();
        if (tmp != null) {
            headers.mergeIn(tmp);
        }
        final JsonObject form = new JsonObject();
        form.put("token", tokenValue).put("token_type_hint", token_type);
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        final Buffer payload = Buffer.buffer(stringify(form));
        // specify preferred accepted accessToken type
        headers.put("Accept", "application/json,application/x-www-form-urlencoded;q=0.9");
        OAuth2API.fetch(provider, HttpMethod.POST, provider.getConfig().getRevocationPath(), headers, payload, res -> {
            if (res.failed()) {
                handler.handle(Future.failedFuture(res.cause()));
                return;
            }
            final OAuth2Response reply = res.result();
            if (reply.body() == null) {
                handler.handle(Future.failedFuture("No Body"));
                return;
            }
            // invalidate ourselves
            token.remove(token_type);
            if ("access_token".equals(token_type)) {
                accessToken = null;
            }
            handler.handle(Future.succeededFuture());
        });
    } else {
        handler.handle(Future.failedFuture("Invalid token: " + token_type));
    }
    return this;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) OAuth2Response(io.vertx.ext.auth.oauth2.OAuth2Response) JsonObject(io.vertx.core.json.JsonObject)

Example 9 with OAuth2TokenImpl

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

the class AuthCodeImpl method getToken.

/**
 * Returns the Access Token object.
 *
 * @param params  - code:        Authorization code (from previous step).
 *                redirectURI: A String that represents the callback uri.
 * @param handler - The handler returning the results.
 */
@Override
public void getToken(JsonObject params, Handler<AsyncResult<AccessToken>> handler) {
    getToken("authorization_code", params, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
            return;
        }
        AccessToken token;
        try {
            token = new OAuth2TokenImpl(provider, res.result());
        } catch (RuntimeException e) {
            handler.handle(Future.failedFuture(e));
            return;
        }
        handler.handle(Future.succeededFuture(token));
    });
}
Also used : AccessToken(io.vertx.ext.auth.oauth2.AccessToken) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)

Example 10 with OAuth2TokenImpl

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

the class AuthJWTImpl method getToken.

/**
 * Returns the Access Token object.
 *
 * @param params - jwt: a JWT to be traded for a token
 * @param callback- The handler returning the results.
 */
@Override
public void getToken(JsonObject params, Handler<AsyncResult<AccessToken>> callback) {
    final JsonObject body = new JsonObject().put("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer").put("assertion", provider.getJWT().sign(params, provider.getConfig().getJWTOptions()));
    fetch(provider, HttpMethod.POST, provider.getConfig().getTokenPath(), new JsonObject().put("Content-Type", "application/x-www-form-urlencoded"), Buffer.buffer(OAuth2API.stringify(body)), fetch -> {
        if (fetch.failed()) {
            callback.handle(Future.failedFuture(fetch.cause()));
            return;
        }
        final OAuth2Response res = fetch.result();
        // token is expected to be an object
        JsonObject token;
        if (res.is("application/json")) {
            try {
                // userInfo is expected to be an object
                token = res.jsonObject();
            } catch (RuntimeException e) {
                callback.handle(Future.failedFuture(e));
                return;
            }
        } else if (res.is("application/x-www-form-urlencoded") || res.is("text/plain")) {
            try {
                // attempt to convert url encoded string to json
                token = OAuth2API.queryToJSON(res.body().toString());
            } catch (RuntimeException | UnsupportedEncodingException e) {
                callback.handle(Future.failedFuture(e));
                return;
            }
        } else {
            callback.handle(Future.failedFuture("Cannot handle Content-Type: " + res.headers().get("Content-Type")));
            return;
        }
        callback.handle(Future.succeededFuture(new OAuth2TokenImpl(provider, token)));
    });
}
Also used : OAuth2Response(io.vertx.ext.auth.oauth2.OAuth2Response) JsonObject(io.vertx.core.json.JsonObject) OAuth2TokenImpl(io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)

Aggregations

OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)11 JsonObject (io.vertx.core.json.JsonObject)8 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)5 Test (org.junit.Test)5 OAuth2Response (io.vertx.ext.auth.oauth2.OAuth2Response)3 Buffer (io.vertx.core.buffer.Buffer)2 OAuth2AuthProviderImpl (io.vertx.ext.auth.oauth2.impl.OAuth2AuthProviderImpl)2 AccessToken (io.vertx.reactivex.ext.auth.oauth2.AccessToken)2 OAuth2Auth (io.vertx.ext.auth.oauth2.OAuth2Auth)1 Session (io.vertx.reactivex.ext.web.Session)1