use of io.vertx.ext.auth.oauth2.OAuth2Response 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;
}
use of io.vertx.ext.auth.oauth2.OAuth2Response in project vertx-auth by vert-x3.
the class AbstractOAuth2Flow method getToken.
void getToken(String grantType, JsonObject params, Handler<AsyncResult<JsonObject>> handler) {
final JsonObject headers = new JsonObject();
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);
}
// Enable the system to send authorization params in the body (for example github does not require to be in the header)
final JsonObject form = params.copy();
if (config.getExtraParameters() != null) {
form.mergeIn(config.getExtraParameters());
}
form.put("client_id", config.getClientID());
form.put("grant_type", grantType);
if (config.getClientSecretParameterName() != null) {
form.put(config.getClientSecretParameterName(), config.getClientSecret());
}
headers.put("Content-Type", "application/x-www-form-urlencoded");
final Buffer payload = Buffer.buffer(stringify(form));
// specify preferred accepted content type
headers.put("Accept", "application/json,application/x-www-form-urlencoded;q=0.9");
fetch(provider, HttpMethod.POST, config.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 content 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 {
handler.handle(Future.succeededFuture(json));
}
} catch (RuntimeException e) {
handler.handle(Future.failedFuture(e));
}
});
}
use of io.vertx.ext.auth.oauth2.OAuth2Response 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;
}
use of io.vertx.ext.auth.oauth2.OAuth2Response 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;
}
use of io.vertx.ext.auth.oauth2.OAuth2Response 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;
}
Aggregations