use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.
the class OAuth2ClientTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.CLIENT, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:8080"));
final CountDownLatch latch = new CountDownLatch(1);
server = vertx.createHttpServer().requestHandler(req -> {
if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> {
try {
assertEquals(config, queryToJSON(buffer.toString()));
} catch (UnsupportedEncodingException e) {
fail(e);
}
req.response().putHeader("Content-Type", "application/json").end(fixture.encode());
});
} else {
req.response().setStatusCode(400).end();
}
}).listen(8080, ready -> {
if (ready.failed()) {
throw new RuntimeException(ready.cause());
}
// ready
latch.countDown();
});
latch.await();
}
use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.
the class OAuth2ErrorsTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:8080"));
final CountDownLatch latch = new CountDownLatch(1);
server = vertx.createHttpServer().requestHandler(req -> {
if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> req.response().putHeader("Content-Type", "application/json").end(fixture.encode()));
} else {
req.response().setStatusCode(400).end();
}
}).listen(8080, ready -> {
if (ready.failed()) {
throw new RuntimeException(ready.cause());
}
// ready
latch.countDown();
});
latch.await();
}
use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.
the class OAuth2FailureTest method unknownHost.
@Test
public void unknownHost() {
OAuth2Auth auth = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://zlouklfoux.net.com.info.pimpo.molo"));
auth.authenticate(tokenConfig, res -> {
if (res.failed()) {
assertThat(res.cause(), instanceOf(UnknownHostException.class));
testComplete();
} else {
fail("Should have failed");
}
});
await();
}
use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions 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.OAuth2ClientOptions in project vertx-auth by vert-x3.
the class OAuth2AccessTokenTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:8080"));
final CountDownLatch latch = new CountDownLatch(1);
server = vertx.createHttpServer().requestHandler(req -> {
if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> {
try {
JsonObject expectedRequest = config;
assertEquals(expectedRequest, queryToJSON(buffer.toString()));
} catch (UnsupportedEncodingException e) {
fail(e);
}
req.response().putHeader("Content-Type", "application/json").end(fixture.encode());
});
} else if (req.method() == HttpMethod.POST && "/oauth/revoke".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> {
// Revoke does not pass auth details
JsonObject expectedRequest = removeAuthDetails(config);
try {
assertEquals(expectedRequest, queryToJSON(buffer.toString()));
} catch (UnsupportedEncodingException e) {
fail(e);
}
req.response().end();
});
} else if (req.method() == HttpMethod.POST && "/oauth/introspect".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> {
try {
assertEquals(config, queryToJSON(buffer.toString()));
} catch (UnsupportedEncodingException e) {
fail(e);
}
req.response().putHeader("Content-Type", "application/json").end(fixtureIntrospect.encode());
});
} else {
req.response().setStatusCode(400).end();
}
}).listen(8080, ready -> {
if (ready.failed()) {
throw new RuntimeException(ready.cause());
}
// ready
latch.countDown();
});
latch.await();
}
Aggregations