use of org.mule.runtime.api.security.Authentication in project mule by mulesoft.
the class DataWeaveExpressionLanguageAdaptorTestCase method authenticationBinding.
@Test
public void authenticationBinding() throws Exception {
CoreEvent event = spy(testEvent());
Authentication authentication = new DefaultMuleAuthentication(new DefaultMuleCredentials("username", "password".toCharArray()));
when(event.getAuthentication()).thenReturn(of(authentication));
TypedValue result = expressionLanguage.evaluate(AUTHENTICATION, event, BindingContext.builder().build());
assertThat(result.getValue(), is(instanceOf(Authentication.class)));
assertThat(result.getValue(), is(authentication));
assertThat(result.getDataType().getType(), is(equalTo(Authentication.class)));
}
use of org.mule.runtime.api.security.Authentication in project mule by mulesoft.
the class UsernamePasswordAuthenticationFilter method authenticate.
/**
* Authenticates the current message.
*
* @param event the current message recieved
* @throws SecurityException if authentication fails
*/
@Override
public SecurityContext authenticate(CoreEvent event) throws SecurityException, SecurityProviderNotFoundException, UnknownAuthenticationTypeException {
Authentication authentication = getAuthenticationToken(event);
Authentication authResult;
try {
authResult = getSecurityManager().authenticate(authentication);
} catch (UnauthorisedException e) {
// Authentication failed
if (logger.isDebugEnabled()) {
logger.debug("Authentication request for user: " + username + " failed: " + e.toString());
}
throw new UnauthorisedException(authFailedForUser(authentication.getPrincipal().toString()), e);
}
// Authentication success
if (logger.isDebugEnabled()) {
logger.debug("Authentication success: " + authResult.toString());
}
SecurityContext context = getSecurityManager().createSecurityContext(authResult);
context.setAuthentication(authResult);
return context;
}
use of org.mule.runtime.api.security.Authentication in project mule by mulesoft.
the class TestMultiuserSecurityProvider method authenticate.
@Override
public Authentication authenticate(Authentication authentication) throws SecurityException {
String user = (String) authentication.getPrincipal();
LOGGER.debug("Authenticating user: " + user);
// Check to see if user has already been authenticated
Authentication oldAuth = authentications.get(user);
if (oldAuth != null) {
authentication = oldAuth;
Map<String, Object> props = new HashMap<>(authentication.getProperties());
int numberLogins = (Integer) props.get(PROPERTY_NUMBER_LOGINS);
String favoriteColor = (String) props.get(PROPERTY_FAVORITE_COLOR);
props.put(PROPERTY_NUMBER_LOGINS, numberLogins + 1);
authentication = authentication.setProperties(props);
authentications.put(user, authentication);
LOGGER.info("Welcome back " + user + " (" + numberLogins + 1 + " logins), we remembered your favorite color: " + favoriteColor);
} else {
String favoriteColor = getFavoriteColor(user);
LOGGER.info("First login for user: " + user + ", favorite color is " + favoriteColor);
Map<String, Object> props = new HashMap<String, Object>();
props.put(PROPERTY_NUMBER_LOGINS, 1);
props.put(PROPERTY_FAVORITE_COLOR, favoriteColor);
authentication = authentication.setProperties(props);
authentications.put(user, authentication);
}
return authentication;
}
use of org.mule.runtime.api.security.Authentication in project mule by mulesoft.
the class DefaultMuleSecurityManager method authenticate.
/**
* {@inheritDoc}
*/
@Override
public Authentication authenticate(Authentication authentication) throws SecurityException, SecurityProviderNotFoundException {
Iterator<SecurityProvider> iter = providers.values().iterator();
Class<? extends Authentication> toTest = authentication.getClass();
while (iter.hasNext()) {
SecurityProvider provider = iter.next();
if (provider.supports(toTest)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Authentication attempt using " + provider.getClass().getName());
}
Authentication result = null;
try {
result = provider.authenticate(authentication);
} catch (Exception e) {
if (!iter.hasNext()) {
throw new UnauthorisedException(authorizationAttemptFailed(), e);
}
}
if (result != null) {
return result;
}
}
}
throw new SecurityProviderNotFoundException(toTest.getName());
}
use of org.mule.runtime.api.security.Authentication in project mule by mulesoft.
the class MuleEncryptionEndpointSecurityFilter method authenticateInbound.
@Override
protected SecurityContext authenticateInbound(CoreEvent event) throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException, EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException {
String userHeader = (String) credentialsAccessor.getCredentials(event);
if (userHeader == null) {
throw new CredentialsNotSetException(event, event.getSecurityContext(), this);
}
Credentials user = new DefaultMuleCredentials(userHeader, getSecurityManager());
Authentication authentication;
try {
authentication = getSecurityManager().authenticate(new DefaultMuleAuthentication(user));
} catch (Exception e) {
// Authentication failed
if (logger.isDebugEnabled()) {
logger.debug("Authentication request for user: " + user.getUsername() + " failed: " + e.toString());
}
throw new UnauthorisedException(authFailedForUser(user.getUsername()), e);
}
// Authentication success
if (logger.isDebugEnabled()) {
logger.debug("Authentication success: " + authentication.toString());
}
SecurityContext context = getSecurityManager().createSecurityContext(authentication);
context.setAuthentication(authentication);
return context;
}
Aggregations