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;
}
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();
}
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);
}
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();
}
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));
}
Aggregations