use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.
the class OAuth2TokenImpl method introspect.
@Override
public AccessToken introspect(String tokenType, Handler<AsyncResult<Void>> handler) {
final JsonObject headers = new JsonObject();
final OAuth2ClientOptions config = provider.getConfig();
if (config.isUseBasicAuthorizationHeader()) {
String basic = config.getClientID() + ":" + config.getClientSecret();
headers.put("Authorization", "Basic " + Base64.getEncoder().encodeToString(basic.getBytes()));
}
JsonObject tmp = config.getHeaders();
if (tmp != null) {
headers.mergeIn(tmp);
}
final JsonObject form = new JsonObject().put("token", token.getString(tokenType)).put("token_type_hint", tokenType);
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, config.getIntrospectionPath(), 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 {
// RFC7662 dictates that there is a boolean active field (however tokeninfo implementations do not return this)
if (json.containsKey("active") && !json.getBoolean("active", false)) {
handler.handle(Future.failedFuture("Inactive Token"));
return;
}
// validate client id
if (json.containsKey("client_id") && !json.getString("client_id", "").equals(config.getClientID())) {
handler.handle(Future.failedFuture("Wrong client_id"));
return;
}
// RFC7662 dictates that there is a boolean active field (however tokeninfo implementations do not return this)
if (json.containsKey("active") && !json.getBoolean("active", false)) {
handler.handle(Future.failedFuture("Inactive Token"));
return;
}
// validate client id
if (json.containsKey("client_id") && !json.getString("client_id", "").equals(provider.getConfig().getClientID())) {
handler.handle(Future.failedFuture("Wrong client_id"));
return;
}
try {
processNonStandardHeaders(json, reply, config.getScopeSeparator());
// reset the access token
token.mergeIn(json);
init();
if (expired()) {
handler.handle(Future.failedFuture("Expired token"));
return;
}
handler.handle(Future.succeededFuture());
} catch (RuntimeException e) {
handler.handle(Future.failedFuture(e));
}
}
} catch (RuntimeException e) {
handler.handle(Future.failedFuture(e));
}
});
return this;
}
use of io.vertx.ext.auth.oauth2.AccessToken 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));
});
}
use of io.vertx.ext.auth.oauth2.AccessToken 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)));
});
}
use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.
the class PasswordImpl method getToken.
/**
* Returns the Access Token object.
*
* @param params - username: A string that represents the registered username.
* password: A string that represents the registered password.
* scope: A String that represents the application privileges.
* @param handler - The handler function returning the results.
*/
@Override
public void getToken(JsonObject params, Handler<AsyncResult<AccessToken>> handler) {
getToken("password", 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));
});
}
use of io.vertx.ext.auth.oauth2.AccessToken in project vertx-auth by vert-x3.
the class OAuth2AccessTokenTest method tokenShouldNotBeExpired.
@Test
public void tokenShouldNotBeExpired() {
config = oauthConfig;
oauth2.authenticate(tokenConfig, res -> {
if (res.failed()) {
fail(res.cause().getMessage());
} else {
AccessToken token = (AccessToken) res.result();
assertFalse(token.expired());
testComplete();
}
});
await();
}
Aggregations