io.dropwizard.auth.Authorizer Java Examples

The following examples show how to use io.dropwizard.auth.Authorizer. 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: JsonWebTokenAuthFilter.java    From jobson with Apache License 2.0 6 votes vote down vote up
private static <P extends Principal> SecurityContext createSecurityContext(P principal, Authorizer<P> authorizer, boolean isSecure) {
    return new SecurityContext() {
        @Override
        public Principal getUserPrincipal() {
            return principal;
        }

        @Override
        public boolean isUserInRole(String s) {
            return authorizer.authorize(principal, s);
        }

        @Override
        public boolean isSecure() {
            return isSecure;
        }

        @Override
        public String getAuthenticationScheme() {
            return AUTHENTICATION_SCHEME_NAME;
        }
    };
}
 
Example #2
Source File: ChainedAuthProviderTest.java    From dropwizard-java8 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public ChainedAuthTestResourceConfig() {
    super(true, new MetricRegistry());

    final Authorizer<Principal> authorizer = AuthUtil.getTestAuthorizer(ADMIN_USER, ADMIN_ROLE);
    final AuthFilter<BasicCredentials, Principal> basicAuthFilter = new BasicCredentialAuthFilter.Builder<>()
            .setAuthenticator(AuthUtil.getBasicAuthenticator(ImmutableList.of(ADMIN_USER, ORDINARY_USER)))
            .setAuthorizer(authorizer)
            .buildAuthFilter();

    final AuthFilter<String, Principal> oAuthFilter = new OAuthCredentialAuthFilter.Builder<>()
            .setAuthenticator(AuthUtil.getSingleUserOAuthAuthenticator(BEARER_USER, ADMIN_USER))
            .setPrefix(BEARER_PREFIX)
            .setAuthorizer(authorizer)
            .buildAuthFilter();

    register(new AuthValueFactoryProvider.Binder(Principal.class));
    register(new AuthDynamicFeature(new ChainedAuthFilter<>(buildHandlerList(basicAuthFilter, oAuthFilter))));
    register(RolesAllowedDynamicFeature.class);
    register(AuthResource.class);
}
 
Example #3
Source File: JsonWebTokenAuthFilterTest.java    From jobson with Apache License 2.0 4 votes vote down vote up
private static JsonWebTokenAuthFilter<Principal> createValidAuthFilterInstance() throws AuthenticationException {
    final Authenticator<String, Principal> authenticator = generateAlwaysAuthenticatesMock();
    final Authorizer<Principal> authorizer = generateAlwaysAuthorizedMock();

    return createAuthFilter(authenticator, authorizer);
}
 
Example #4
Source File: JsonWebTokenAuthFilterTest.java    From jobson with Apache License 2.0 4 votes vote down vote up
private static JsonWebTokenAuthFilter<Principal> createAuthFilter(Authenticator<String, Principal> authenticator, Authorizer<Principal> authorizer) {
    return (JsonWebTokenAuthFilter<Principal>)new JsonWebTokenAuthFilter.Builder().setAuthenticator(authenticator).setAuthorizer(authorizer).buildAuthFilter();
}
 
Example #5
Source File: JsonWebTokenAuthFilterTest.java    From jobson with Apache License 2.0 4 votes vote down vote up
private static JsonWebTokenAuthFilter createAuthFilterWithAuthenticator(Authenticator<String, Principal> authenticator) {
    final Authorizer<Principal> authorizer = generateAlwaysAuthorizedMock();

    return createAuthFilter(authenticator, authorizer);
}
 
Example #6
Source File: JsonWebTokenAuthFilterTest.java    From jobson with Apache License 2.0 4 votes vote down vote up
private static JsonWebTokenAuthFilter createAuthFilterWithAuthorizer(Authorizer<Principal> authorizer) throws AuthenticationException {
    final Authenticator<String, Principal> authenticator = generateAlwaysAuthenticatesMock();

    return createAuthFilter(authenticator, authorizer);
}
 
Example #7
Source File: JsonWebTokenAuthFilterTest.java    From jobson with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecurityContextUsesProvidedAuthorizer() throws AuthenticationException, IOException {
    final Authorizer<Principal> authorizer = mock(Authorizer.class);
    when(authorizer.authorize(any(), any())).thenReturn(true);

    final JsonWebTokenAuthFilter filter = createAuthFilterWithAuthorizer(authorizer);

    final ContainerRequestContext request = createDummyRequest();

    filter.filter(request);

    final SecurityContext securityContext = request.getSecurityContext();

    verify(authorizer, times(0)).authorize(any(), any());

    final String role = TestHelpers.generateRandomString();

    securityContext.isUserInRole(role);

    verify(authorizer, times(1)).authorize(any(), any());
}
 
Example #8
Source File: AuthUtil.java    From dropwizard-auth-jwt with Apache License 2.0 4 votes vote down vote up
public static Authorizer<Principal> getTestAuthorizer(final String validUser, final String validRole) {
    return (principal, role) -> principal != null && validUser.equals(principal.getName()) && validRole.equals(role);
}
 
Example #9
Source File: AuthUtil.java    From dropwizard-java8 with Apache License 2.0 4 votes vote down vote up
public static Authorizer<Principal> getTestAuthorizer(final String validUser,
                                                      final String validRole) {
    return (principal, role) -> principal != null
            && validUser.equals(principal.getName())
            && validRole.equals(role);
}
 
Example #10
Source File: JsonWebTokenAuthFilterTest.java    From jobson with Apache License 2.0 3 votes vote down vote up
private static Authorizer<Principal> generateAlwaysAuthorizedMock() {
    final Authorizer authorizer = mock(Authorizer.class);

    when(authorizer.authorize(any(), any())).thenReturn(true);

    return authorizer;
}
 
Example #11
Source File: KeycloakBundle.java    From keycloak-dropwizard-integration with Apache License 2.0 2 votes vote down vote up
/**
 * Return the Authorizer instance that will be used to check the @RolesAllowed annotations.
 * Override this method to provide an instance of a different instance of another class.
 *
 * @return the class.
 */
protected Authorizer createAuthorizer() {
    return new UserAuthorizer();
}
 
Example #12
Source File: AuthFilter.java    From dropwizard-java8 with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the given authorizer
 *
 * @param authorizer an {@link Authorizer}
 * @return the current builder
 */
public AuthFilterBuilder<C, P, T> setAuthorizer(Authorizer<P> authorizer) {
    this.authorizer = authorizer;
    return this;
}