Search in sources :

Example 1 with OAuth2API.stringify

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

the class OAuth2TokenImpl method userInfo.

@Override
public AccessToken userInfo(Handler<AsyncResult<JsonObject>> callback) {
    final JsonObject headers = new JsonObject();
    final JsonObject extraParams = provider.getConfig().getUserInfoParameters();
    String path = provider.getConfig().getUserInfoPath();
    if (extraParams != null) {
        path += "?" + OAuth2API.stringify(extraParams);
    }
    headers.put("Authorization", "Bearer " + token.getString("access_token"));
    // specify preferred accepted accessToken type
    headers.put("Accept", "application/json,application/x-www-form-urlencoded;q=0.9");
    OAuth2API.fetch(provider, HttpMethod.GET, path, headers, null, fetch -> {
        if (fetch.failed()) {
            callback.handle(Future.failedFuture(fetch.cause()));
            return;
        }
        final OAuth2Response reply = fetch.result();
        // userInfo is expected to be an object
        JsonObject userInfo;
        if (reply.is("application/json")) {
            try {
                // userInfo is expected to be an object
                userInfo = reply.jsonObject();
            } catch (RuntimeException e) {
                callback.handle(Future.failedFuture(e));
                return;
            }
        } else if (reply.is("application/x-www-form-urlencoded") || reply.is("text/plain")) {
            try {
                // attempt to convert url encoded string to json
                userInfo = OAuth2API.queryToJSON(reply.body().toString());
            } catch (RuntimeException | UnsupportedEncodingException e) {
                callback.handle(Future.failedFuture(e));
                return;
            }
        } else {
            callback.handle(Future.failedFuture("Cannot handle Content-Type: " + reply.headers().get("Content-Type")));
            return;
        }
        OAuth2API.processNonStandardHeaders(token, reply, provider.getConfig().getScopeSeparator());
        // re-init to reparse the authorities
        init();
        callback.handle(Future.succeededFuture(userInfo));
    });
    return this;
}
Also used : OAuth2Response(io.vertx.ext.auth.oauth2.OAuth2Response) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with OAuth2API.stringify

use of io.vertx.ext.auth.oauth2.impl.OAuth2API.stringify 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

JsonObject (io.vertx.core.json.JsonObject)2 OAuth2Response (io.vertx.ext.auth.oauth2.OAuth2Response)2 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)1