Search in sources :

Example 1 with SimbaPrincipal

use of org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal 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 SimbaPrincipal

use of org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal in project simba-os by cegeka.

the class SimbaAuthenticatedInjectableTest method getValue_WhenDomainProviderReturnsNull_AndIsRequired_ThenNoExceptionIsThrown.

@Test
public void getValue_WhenDomainProviderReturnsNull_AndIsRequired_ThenNoExceptionIsThrown() throws Exception {
    SimbaPrincipal principal = new SimbaPrincipal("user", "token");
    when(authenticatorMock.authenticate(simbaCredentials)).thenReturn(Optional.of(principal));
    when(domainProviderMock.lookUp(principal)).thenReturn(null);
    injectable = new SimbaAuthenticatedInjectable<AuthenticatedPrincipal>(authenticatorMock, simbaCredentialsFactoryMock, domainProviderMock, true);
    AuthenticatedPrincipal actual = injectable.getValue(dummyHttpContext);
    assertThat(actual).isNull();
}
Also used : SimbaPrincipal(org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal) Test(org.junit.Test)

Example 3 with SimbaPrincipal

use of org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal in project simba-os by cegeka.

the class SimbaGateway method authenticate.

/**
	 * On successful authentication returns a present SimbaPrincipal
	 * On failed authentication returns either an absent SimbaPrincipal, or throws a SimbaUnavailableException
	 * 
	 * @param credentials
	 * @return an absent SimbaPrincipal when the ActionDescriptor does not contain DO_FILTER_AND_SET_PRINCIPAL (means authentication failed)
	 *         a present SimbaPrincipal when the ActionDescriptor does contain DO_FILTER_AND_SET_PRINCIPAL (means authentication was successful)
	 * @throws SimbaUnavailableException
	 */
public Optional<SimbaPrincipal> authenticate(SimbaCredentials credentials) throws SimbaUnavailableException {
    SimbaPrincipal principal = null;
    ActionDescriptor actionDescriptor = processRequestInSimba(credentials.asRequestData(), SESSION_AUTHENTICATE_CHAIN);
    if (isValidActionDescriptor(actionDescriptor) && actionDescriptor.getActionTypes().contains(ActionType.DO_FILTER_AND_SET_PRINCIPAL)) {
        String token = actionDescriptor.getSsoToken() != null ? actionDescriptor.getSsoToken().getToken() : null;
        principal = new SimbaPrincipal(actionDescriptor.getPrincipal(), token);
    }
    return Optional.fromNullable(principal);
}
Also used : SimbaPrincipal(org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal) ActionDescriptor(org.simbasecurity.api.service.thrift.ActionDescriptor)

Example 4 with SimbaPrincipal

use of org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal in project simba-os by cegeka.

the class SimbaGatewayTest method authenticate_WhenPrincipalWasNotSet_ReturnsAbsentPrincipal.

@Test
public void authenticate_WhenPrincipalWasNotSet_ReturnsAbsentPrincipal() throws Exception {
    SimbaCredentials credentials = mock(SimbaCredentials.class);
    RequestData requestData = mock(RequestData.class);
    when(credentials.asRequestData()).thenReturn(requestData);
    Client authenticationServiceMock = setupSimbaServiceToReturnASimbaAuthenticationService();
    String principal = "simbaUsername";
    String ssoToken = "ssotoken";
    ActionDescriptor actionDescriptor = new ActionDescriptorBuilderForTests().withActionTypes(ActionType.ADD_PARAMETER_TO_TARGET).withPrincipal(principal).withSsoToken(new SSOToken(ssoToken)).build();
    when(authenticationServiceMock.processRequest(requestData, SimbaGateway.SESSION_AUTHENTICATE_CHAIN)).thenReturn(actionDescriptor);
    Optional<SimbaPrincipal> simbaPrincipal = simbaGateway.authenticate(credentials);
    assertThat(simbaPrincipal.isPresent()).isFalse();
}
Also used : SimbaPrincipal(org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal) ActionDescriptorBuilderForTests(org.simbasecurity.dwclient.test.stub.simba.ActionDescriptorBuilderForTests) SSOToken(org.simbasecurity.api.service.thrift.SSOToken) SimbaCredentials(org.simbasecurity.dwclient.dropwizard.credentials.SimbaCredentials) RequestData(org.simbasecurity.api.service.thrift.RequestData) ActionDescriptor(org.simbasecurity.api.service.thrift.ActionDescriptor) Client(org.simbasecurity.api.service.thrift.AuthenticationFilterService.Client) THttpClient(org.apache.thrift.transport.THttpClient) Test(org.junit.Test)

Example 5 with SimbaPrincipal

use of org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal in project simba-os by cegeka.

the class SimbaGatewayTest method authenticate_WhenPrincipalWasSet_ReturnPrincipal.

@Test
public void authenticate_WhenPrincipalWasSet_ReturnPrincipal() throws Exception {
    SimbaCredentials credentials = mock(SimbaCredentials.class);
    RequestData requestData = mock(RequestData.class);
    when(credentials.asRequestData()).thenReturn(requestData);
    Client authenticationServiceMock = setupSimbaServiceToReturnASimbaAuthenticationService();
    String principal = "simbaUsername";
    String token = "token";
    ActionDescriptor actionDescriptor = new ActionDescriptorBuilderForTests().withActionTypes(ActionType.DO_FILTER_AND_SET_PRINCIPAL).withPrincipal(principal).withSsoToken(new SSOToken(token)).build();
    when(authenticationServiceMock.processRequest(requestData, SESSION_AUTHENTICATE_CHAIN)).thenReturn(actionDescriptor);
    Optional<SimbaPrincipal> simbaPrincipal = simbaGateway.authenticate(credentials);
    assertThat(simbaPrincipal.get()).isEqualTo(new SimbaPrincipal(principal, token));
}
Also used : SimbaPrincipal(org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal) ActionDescriptorBuilderForTests(org.simbasecurity.dwclient.test.stub.simba.ActionDescriptorBuilderForTests) SSOToken(org.simbasecurity.api.service.thrift.SSOToken) SimbaCredentials(org.simbasecurity.dwclient.dropwizard.credentials.SimbaCredentials) RequestData(org.simbasecurity.api.service.thrift.RequestData) ActionDescriptor(org.simbasecurity.api.service.thrift.ActionDescriptor) Client(org.simbasecurity.api.service.thrift.AuthenticationFilterService.Client) THttpClient(org.apache.thrift.transport.THttpClient) Test(org.junit.Test)

Aggregations

SimbaPrincipal (org.simbasecurity.dwclient.dropwizard.credentials.SimbaPrincipal)8 Test (org.junit.Test)5 SimbaCredentials (org.simbasecurity.dwclient.dropwizard.credentials.SimbaCredentials)5 ActionDescriptor (org.simbasecurity.api.service.thrift.ActionDescriptor)3 ContainerRequest (com.sun.jersey.spi.container.ContainerRequest)2 THttpClient (org.apache.thrift.transport.THttpClient)2 Client (org.simbasecurity.api.service.thrift.AuthenticationFilterService.Client)2 RequestData (org.simbasecurity.api.service.thrift.RequestData)2 SSOToken (org.simbasecurity.api.service.thrift.SSOToken)2 ActionDescriptorBuilderForTests (org.simbasecurity.dwclient.test.stub.simba.ActionDescriptorBuilderForTests)2 AuthenticationException (com.yammer.dropwizard.auth.AuthenticationException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 SimbaAuthenticator (org.simbasecurity.dwclient.dropwizard.authenticator.SimbaAuthenticator)1 SimbaCredentialsBuilderForTests (org.simbasecurity.dwclient.dropwizard.credentials.SimbaCredentialsBuilderForTests)1 SimbaCredentialsFactory (org.simbasecurity.dwclient.dropwizard.credentials.SimbaCredentialsFactory)1 SimbaAuthenticatedProvider (org.simbasecurity.dwclient.dropwizard.provider.SimbaAuthenticatedProvider)1