use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-web by vert-x3.
the class OAuth2AuthHandlerTest method testPasswordFlow.
@Test
public void testPasswordFlow() throws Exception {
// lets mock a oauth2 server using code auth code flow
OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.PASSWORD, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:10000"));
final CountDownLatch latch = new CountDownLatch(1);
HttpServer server = vertx.createHttpServer().requestHandler(req -> {
if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> {
final String queryString = buffer.toString();
assertTrue(queryString.contains("username=paulo"));
assertTrue(queryString.contains("password=bananas"));
assertTrue(queryString.contains("grant_type=password"));
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 -> req.response().end());
} else {
req.response().setStatusCode(400).end();
}
}).listen(10000, ready -> {
if (ready.failed()) {
throw new RuntimeException(ready.cause());
}
// ready
latch.countDown();
});
latch.await();
AuthHandler oauth2Handler = BasicAuthHandler.create(oauth2);
// protect everything under /protected
router.route("/protected/*").handler(oauth2Handler);
// mount some handler under the protected zone
router.route("/protected/somepage").handler(rc -> {
assertNotNull(rc.user());
rc.response().end("Welcome to the protected resource!");
});
testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString("paulo:bananas".getBytes())), res -> {
// in this case we should get the resource
}, 200, "OK", "Welcome to the protected resource!");
testRequest(HttpMethod.GET, "/protected/somepage", 401, "Unauthorized");
server.close();
}
use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.
the class OAuth2FailureTest 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 {
assertEquals(config, queryToJSON(buffer.toString()));
} catch (UnsupportedEncodingException e) {
fail(e);
}
req.response().setStatusCode(code).end();
});
} 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 OAuth2PasswordTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.PASSWORD, 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 AuthCodeImpl method authorizeURL.
/**
* Redirect the user to the authorization page
*
* @param params - redirectURI: A String that represents the registered application URI where the user is redirected after authorization.
* scope: A String that represents the application privileges.
* scopes: A array of strings that will encoded as a single string "scope" following the provider requirements
* state: A String that represents an optional opaque value used by the client to maintain state between the request and the callback.
*/
@Override
public String authorizeURL(JsonObject params) {
final JsonObject query = params.copy();
final OAuth2ClientOptions config = provider.getConfig();
if (query.containsKey("scopes")) {
// scopes have been passed as a list so the provider must generate the correct string for it
query.put("scope", String.join(config.getScopeSeparator(), query.getJsonArray("scopes").getList()));
query.remove("scopes");
}
query.put("response_type", "code");
query.put("client_id", config.getClientID());
return config.getSite() + config.getAuthorizationPath() + '?' + stringify(query);
}
use of io.vertx.ext.auth.oauth2.OAuth2ClientOptions in project vertx-auth by vert-x3.
the class OAuth2AuthCodeErrorTest 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 {
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();
}
Aggregations