use of io.vertx.reactivex.ext.auth.User in project gravitee-access-management by gravitee-io.
the class SSOSessionHandler method authorizeUser.
private void authorizeUser(RoutingContext context, Handler<AsyncResult<Void>> handler) {
// retrieve end user and check if it's authorized to call the subsequence handlers
User authenticatedUser = context.user();
io.gravitee.am.model.User endUser = ((io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User) authenticatedUser.getDelegate()).getUser();
// check account status
checkAccountStatus(context, endUser, accountHandler -> {
if (accountHandler.failed()) {
handler.handle(Future.failedFuture(accountHandler.cause()));
return;
}
// additional check
checkClient(context, endUser, clientHandler -> {
if (clientHandler.failed()) {
handler.handle(Future.failedFuture(clientHandler.cause()));
return;
}
// continue
handler.handle(Future.succeededFuture());
});
});
}
use of io.vertx.reactivex.ext.auth.User in project gravitee-access-management by gravitee-io.
the class SSOSessionHandler method checkClient.
private void checkClient(RoutingContext context, io.gravitee.am.model.User user, Handler<AsyncResult<Void>> handler) {
final String clientId = context.request().getParam(Parameters.CLIENT_ID);
// no client to check, continue
if (clientId == null) {
handler.handle(Future.succeededFuture());
return;
}
// no client to check for the user, continue
if (user.getClient() == null) {
handler.handle(Future.succeededFuture());
return;
}
// check if both clients (requested and user client) share the same identity provider
Single.zip(getClient(clientId), getClient(user.getClient()), (optRequestedClient, optUserClient) -> {
Client requestedClient = optRequestedClient.get();
Client userClient = optUserClient.get();
// no client to check, continue
if (requestedClient == null) {
return Completable.complete();
}
// no client to check for the user, continue
if (userClient == null) {
return Completable.complete();
}
// if same client, nothing to do, continue
if (userClient.getId().equals(requestedClient.getId())) {
return Completable.complete();
}
// both clients are sharing the same provider, continue
if (requestedClient.getClientId() != null && requestedClient.getIdentityProviders().stream().anyMatch(appIdp -> appIdp.getIdentity().equals(user.getSource()))) {
return Completable.complete();
}
// throw error
throw new InvalidRequestException("User is not on a shared identity provider");
}).subscribe(__ -> handler.handle(Future.succeededFuture()), error -> handler.handle(Future.failedFuture(error)));
}
use of io.vertx.reactivex.ext.auth.User in project gravitee-access-management by gravitee-io.
the class UserConsentPrepareContextHandler method handle.
@Override
public void handle(RoutingContext routingContext) {
// user must redirected here after an authorization request
AuthorizationRequest authorizationRequest = routingContext.get(ConstantKeys.AUTHORIZATION_REQUEST_CONTEXT_KEY);
if (authorizationRequest == null) {
routingContext.response().setStatusCode(400).end("An authorization request is required to handle user approval");
return;
}
// check user
User authenticatedUser = routingContext.user();
if (authenticatedUser == null || !(authenticatedUser.getDelegate() instanceof io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User)) {
routingContext.fail(new AccessDeniedException());
return;
}
// prepare context
Client safeClient = new Client(routingContext.get(ConstantKeys.CLIENT_CONTEXT_KEY));
safeClient.setClientSecret(null);
io.gravitee.am.model.User user = ((io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User) authenticatedUser.getDelegate()).getUser();
prepareContext(routingContext, safeClient, user);
routingContext.next();
}
use of io.vertx.reactivex.ext.auth.User in project gravitee-access-management by gravitee-io.
the class AuthorizationEndpointTest method shouldInvokeAuthorizationEndpoint_emptyScope.
@Test
public void shouldInvokeAuthorizationEndpoint_emptyScope() throws Exception {
final Client client = new Client();
client.setId("client-id");
client.setClientId("client-id");
client.setRedirectUris(Collections.singletonList("http://localhost:9999/callback"));
AuthorizationRequest authorizationRequest = new AuthorizationRequest();
authorizationRequest.setApproved(true);
authorizationRequest.setResponseType(ResponseType.CODE);
authorizationRequest.setRedirectUri("http://localhost:9999/callback");
AuthorizationResponse authorizationResponse = new AuthorizationCodeResponse();
authorizationResponse.setRedirectUri(authorizationRequest.getRedirectUri());
((AuthorizationCodeResponse) authorizationResponse).setCode("test-code");
router.route().order(-1).handler(routingContext -> {
routingContext.setUser(new User(new io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User(new io.gravitee.am.model.User())));
routingContext.next();
});
when(clientSyncService.findByClientId("client-id")).thenReturn(Maybe.just(client));
when(flow.run(any(), any(), any())).thenReturn(Single.just(authorizationResponse));
router.route().order(-1).handler(routingContext -> {
routingContext.put(CLIENT_CONTEXT_KEY, client);
routingContext.next();
});
testRequest(HttpMethod.GET, "/oauth/authorize?response_type=code&client_id=client-id&redirect_uri=http://localhost:9999/callback&scope=", null, resp -> {
String location = resp.headers().get("location");
assertNotNull(location);
assertEquals("http://localhost:9999/callback?code=test-code", location);
}, HttpStatusCode.FOUND_302, "Found", null);
}
use of io.vertx.reactivex.ext.auth.User in project gravitee-access-management by gravitee-io.
the class AuthorizationEndpointTest method shouldInvokeAuthorizationEndpoint_implicitFlow.
private void shouldInvokeAuthorizationEndpoint_implicitFlow(String responseType, String expectedCallback, Token accessToken, String idToken) throws Exception {
final Client client = new Client();
client.setId("client-id");
client.setClientId("client-id");
client.setScopeSettings(Collections.singletonList(new ApplicationScopeSettings("read")));
client.setRedirectUris(Collections.singletonList("http://localhost:9999/callback"));
client.setAuthorizedGrantTypes(Arrays.asList(GrantType.IMPLICIT));
client.setResponseTypes(Arrays.asList(responseType));
AuthorizationRequest authorizationRequest = new AuthorizationRequest();
authorizationRequest.setApproved(true);
authorizationRequest.setResponseType(responseType);
authorizationRequest.setRedirectUri("http://localhost:9999/callback");
AuthorizationResponse authorizationResponse = null;
if (accessToken != null) {
authorizationResponse = new ImplicitResponse();
((ImplicitResponse) authorizationResponse).setAccessToken(accessToken);
}
if (idToken != null) {
authorizationResponse = new IDTokenResponse();
((IDTokenResponse) authorizationResponse).setIdToken(idToken);
}
authorizationResponse.setRedirectUri(authorizationRequest.getRedirectUri());
router.route().order(-1).handler(routingContext -> {
routingContext.setUser(new User(new io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User(new io.gravitee.am.model.User())));
routingContext.next();
});
when(clientSyncService.findByClientId("client-id")).thenReturn(Maybe.just(client));
when(flow.run(any(), any(), any())).thenReturn(Single.just(authorizationResponse));
testRequest(HttpMethod.GET, "/oauth/authorize?response_type=" + responseType.replaceAll("\\s", "%20") + "&client_id=client-id&nonce=123&redirect_uri=http://localhost:9999/callback", null, resp -> {
String location = resp.headers().get("location");
assertNotNull(location);
assertEquals("http://localhost:9999/callback#" + expectedCallback, location);
}, HttpStatusCode.FOUND_302, "Found", null);
}
Aggregations