Java Code Examples for io.vertx.ext.auth.authorization.AuthorizationProvider#getAuthorizations()

The following examples show how to use io.vertx.ext.auth.authorization.AuthorizationProvider#getAuthorizations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthorizationHandlerImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
/**
 * this method checks that the specified authorization match the current content.
 * It doesn't fetch all providers at once in order to do early-out, but rather tries to be smart and fetch authorizations one provider at a time
 *
 * @param routingContext
 * @param authorizationContext
 * @param providers
 */
private void checkOrFetchAuthorizations(RoutingContext routingContext, AuthorizationContext authorizationContext, Iterator<AuthorizationProvider> providers) {
  if (authorization.match(authorizationContext)) {
    routingContext.next();
    return;
  }
  if (!providers.hasNext()) {
    routingContext.fail(FORBIDDEN_CODE, FORBIDDEN_EXCEPTION);
    return;
  }

  // there was no match, in this case we do the following:
  // 1) contact the next provider we haven't contacted yet
  // 2) if there is a match, get out right away otherwise repeat 1)
  while (providers.hasNext()) {
    AuthorizationProvider provider = providers.next();
    // we haven't fetch authorization from this provider yet
    if (! routingContext.user().authorizations().getProviderIds().contains(provider.getId())) {
      provider.getAuthorizations(routingContext.user(), authorizationResult -> {
        if (authorizationResult.failed()) {
          LOG.warn("An error occured getting authorization - providerId: " + provider.getId(), authorizationResult.cause());
          // note that we don't 'record' the fact that we tried to fetch the authorization provider. therefore it will be re-fetched later-on
        }
        checkOrFetchAuthorizations(routingContext, authorizationContext, providers);
      });
      // get out right now as the callback will decide what to do next
      return;
    }
  }
}
 
Example 2
Source File: AuthCommonExamples.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
public void example2(User user, AuthorizationProvider authorizationProvider) {
  // load the authorization for the given user:
  authorizationProvider.getAuthorizations(user, res -> {
    if (res.succeeded()) {
      // cache is populated, perform query
      if (PermissionBasedAuthorization.create("printer1234").match(user)) {
        System.out.println("User has the authority");
      } else {
        System.out.println("User does not have the authority");
      }
    }
  });
}
 
Example 3
Source File: AuthCommonExamples.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
public void example3(User user, AuthorizationProvider authorizationProvider) {
  // load the authorization for the given user:
  authorizationProvider.getAuthorizations(user, res -> {
    if (res.succeeded()) {
      // cache is populated, perform query
      if (RoleBasedAuthorization.create("admin").match(user)) {
        System.out.println("User has the authority");
      } else {
        System.out.println("User does not have the authority");
      }
    }
  });
}