/** * Determines if the specified {@code data} is authorized for this service. If the result resolves * to {@code true}, the request is authorized, or {@code false} otherwise. If the future resolves * exceptionally, the request will not be authorized. */ static <T> CompletionStage<Boolean> authorize(Authorizer<T> authorizer, ServiceRequestContext ctx, T data) { try { final CompletionStage<Boolean> f = authorizer.authorize(ctx, data); if (f == null) { throw new NullPointerException("An " + Authorizer.class.getSimpleName() + " returned null: " + authorizer); } return f; } catch (Throwable cause) { return CompletableFutures.exceptionallyCompletedFuture(cause); } }
/** * Adds an {@link Authorizer}. */ public HttpAuthServiceBuilder add(Authorizer<HttpRequest> authorizer) { requireNonNull(authorizer, "authorizer"); if (this.authorizer == null) { this.authorizer = authorizer; } else { this.authorizer = this.authorizer.orElse(authorizer); } return this; }
/** * Adds a token-based {@link Authorizer}. */ public <T> HttpAuthServiceBuilder addTokenAuthorizer( Function<HttpHeaders, T> tokenExtractor, Authorizer<? super T> authorizer) { requireNonNull(tokenExtractor, "tokenExtractor"); requireNonNull(authorizer, "authorizer"); final Authorizer<HttpRequest> requestAuthorizer = (ctx, req) -> { final T token = tokenExtractor.apply(req.headers()); if (token == null) { return CompletableFuture.completedFuture(false); } return authorizer.authorize(ctx, token); }; add(requestAuthorizer); return this; }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from(authorizer.authorize(ctx, req).handle((result, cause) -> { if (cause == null && result) {
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from(authorizer.authorize(ctx, req).handle((result, cause) -> { if (cause == null && result) {