Search in sources :

Example 1 with AuthenticationException

use of com.yammer.dropwizard.auth.AuthenticationException in project simba-os by cegeka.

the class SimbaAuthenticatedInjectable method getValue.

@Override
public P getValue(HttpContext httpContext) {
    SimbaCredentials credentials;
    try {
        final ContainerRequest containerRequest = (ContainerRequest) httpContext.getRequest();
        credentials = simbaCredentialsFactory.create(containerRequest);
        final Optional<SimbaPrincipal> result = authenticator.authenticate(credentials);
        if (result.isPresent()) {
            return domainProvider.lookUp(result.get());
        }
    } catch (AuthenticationException e) {
        log.error("Something went wrong in the authentication process", e);
        throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).entity("Something went wrong in the authentication process").type(MediaType.APPLICATION_JSON).build());
    }
    if (required) {
        log.warn("Error authenticating credentials: {}", credentials.getSsoToken());
        throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).entity("You are not allowed to access this resource").type(MediaType.APPLICATION_JSON).build());
    }
    return null;
}
Also used : SimbaPrincipal(org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal) WebApplicationException(javax.ws.rs.WebApplicationException) SimbaCredentials(org.simbasecurity.dwclient.dropwizard.credentials.SimbaCredentials) AuthenticationException(com.yammer.dropwizard.auth.AuthenticationException) ContainerRequest(com.sun.jersey.spi.container.ContainerRequest)

Example 2 with AuthenticationException

use of com.yammer.dropwizard.auth.AuthenticationException in project simba-os by cegeka.

the class SimbaAuthenticatedInjectableTest method getValue_WhenAuthenticatorThrowsAuthenticationException_ThenThrowsWebAppException.

@Test
public void getValue_WhenAuthenticatorThrowsAuthenticationException_ThenThrowsWebAppException() throws Exception {
    when(authenticatorMock.authenticate(simbaCredentials)).thenThrow(new AuthenticationException(""));
    expectedException.expect(WebApplicationExceptionMatcher.webApplicationException(Response.Status.UNAUTHORIZED));
    injectable.getValue(dummyHttpContext);
}
Also used : AuthenticationException(com.yammer.dropwizard.auth.AuthenticationException) Test(org.junit.Test)

Aggregations

AuthenticationException (com.yammer.dropwizard.auth.AuthenticationException)2 ContainerRequest (com.sun.jersey.spi.container.ContainerRequest)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Test (org.junit.Test)1 SimbaCredentials (org.simbasecurity.dwclient.dropwizard.credentials.SimbaCredentials)1 SimbaPrincipal (org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal)1