use of io.vertx.ext.auth.oauth2.OAuth2Response 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)));
});
}
Aggregations