/** * Returns a newly-created decorator that decorates a {@link Service} with a new {@link HttpAuthService} * based on the {@link Authorizer}s added to this builder. */ public Function<Service<HttpRequest, HttpResponse>, HttpAuthService> newDecorator() { final Authorizer<HttpRequest> authorizer = authorizer(); final AuthSuccessHandler<HttpRequest, HttpResponse> successHandler = this.successHandler; final AuthFailureHandler<HttpRequest, HttpResponse> failureHandler = this.failureHandler; return service -> new HttpAuthService(service, authorizer, successHandler, failureHandler); }
/** * Creates a new {@link OAuth1aToken} from the given arguments. */ public static OAuth1aToken of(Map<String, String> params) { return new OAuth1aToken(params); }
/** * Creates a new {@link OAuth2Token} from the given {@code accessToken}. */ public static OAuth2Token of(String accessToken) { return new OAuth2Token(accessToken); }
/** * Creates a new HTTP authorization {@link Service} decorator using the specified * {@link Authorizer}s. * * @param authorizers a list of {@link Authorizer}s. */ public static Function<Service<HttpRequest, HttpResponse>, HttpAuthService> newDecorator( Iterable<? extends Authorizer<HttpRequest>> authorizers) { return new HttpAuthServiceBuilder().add(authorizers).newDecorator(); }
@Override public String toString() { return MoreObjects.toStringHelper(this) .add("realm", realm()) .add("consumerKey", consumerKey()) .add("token", "****") .add("signatureMethod", signatureMethod()) .add("signature", signature()) .add("timestamp", timestamp()) .add("nonce", nonce()) .add("version", version()) .add("additionals", additionals()) .toString(); } }
/** * 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(AuthorizerUtil.authorize(authorizer, ctx, req).handleAsync((result, cause) -> { try { if (cause == null) { if (result != null) { return result ? successHandler.authSucceeded(delegate(), ctx, req) : failureHandler.authFailed(delegate(), ctx, req, null); } cause = AuthorizerUtil.newNullResultException(authorizer); } return failureHandler.authFailed(delegate(), ctx, req, cause); } catch (Exception e) { return Exceptions.throwUnsafely(e); } }, ctx.contextAwareEventLoop())); } }
/** * Creates a new HTTP authorization {@link Service} decorator using the specified * {@link Authorizer}s. * * @param authorizers the array of {@link Authorizer}s. */ @SafeVarargs public static Function<Service<HttpRequest, HttpResponse>, HttpAuthService> newDecorator(Authorizer<HttpRequest>... authorizers) { return newDecorator(ImmutableList.copyOf(requireNonNull(authorizers, "authorizers"))); }
/** * Adds multiple {@link Authorizer}s. */ public HttpAuthServiceBuilder add(Iterable<? extends Authorizer<HttpRequest>> authorizers) { requireNonNull(authorizers, "authorizers"); authorizers.forEach(a -> { requireNonNull(a, "authorizers contains null."); add(a); }); return this; }
/** * Adds an HTTP basic {@link Authorizer} for the given {@code header}. */ public HttpAuthServiceBuilder addBasicAuth(Authorizer<? super BasicToken> authorizer, AsciiString header) { return addTokenAuthorizer(new BasicTokenExtractor(requireNonNull(header, "header")), requireNonNull(authorizer, "authorizer")); }
/** * Adds an OAuth1a {@link Authorizer} for the given {@code header}. */ public HttpAuthServiceBuilder addOAuth1a(Authorizer<? super OAuth1aToken> authorizer, AsciiString header) { return addTokenAuthorizer(new OAuth1aTokenExtractor(requireNonNull(header, "header")), requireNonNull(authorizer, "authorizer")); }
/** * Adds an OAuth2 {@link Authorizer} for the given {@code header}. */ public HttpAuthServiceBuilder addOAuth2(Authorizer<? super OAuth2Token> authorizer, AsciiString header) { return addTokenAuthorizer(new OAuth2TokenExtractor(requireNonNull(header, "header")), requireNonNull(authorizer, "authorizer")); }
@Override public CompletionStage<Boolean> authorize(ServiceRequestContext ctx, T data) { return AuthorizerUtil.authorize(self, ctx, data).thenComposeAsync(result -> { if (result == null) { throw AuthorizerUtil.newNullResultException(self); } else { return result ? CompletableFuture.completedFuture(true) : AuthorizerUtil.authorize(nextAuthorizer, ctx, data); } }, ctx.contextAwareEventLoop()); }
/** * Adds an HTTP basic {@link Authorizer}. */ public HttpAuthServiceBuilder addBasicAuth(Authorizer<? super BasicToken> authorizer) { return addTokenAuthorizer(AuthTokenExtractors.BASIC, requireNonNull(authorizer, "authorizer")); }
/** * Creates a new {@link BasicToken} from the given {@code username} and {@code password}. */ public static BasicToken of(String username, String password) { return new BasicToken(username, password); }
/** * 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; }
/** * Returns a newly-created {@link HttpAuthService} based on the {@link Authorizer}s added to this builder. */ public HttpAuthService build(Service<HttpRequest, HttpResponse> delegate) { return new HttpAuthService(requireNonNull(delegate, "delegate"), authorizer(), successHandler, failureHandler); }
/** * Adds an OAuth1a {@link Authorizer}. */ public HttpAuthServiceBuilder addOAuth1a(Authorizer<? super OAuth1aToken> authorizer) { return addTokenAuthorizer(AuthTokenExtractors.OAUTH1A, requireNonNull(authorizer, "authorizer")); }
/** * Adds an OAuth2 {@link Authorizer}. */ public HttpAuthServiceBuilder addOAuth2(Authorizer<? super OAuth2Token> authorizer) { return addTokenAuthorizer(AuthTokenExtractors.OAUTH2, requireNonNull(authorizer, "authorizer")); }