Search in sources :

Example 1 with Authentication

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)));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultMuleAuthentication(org.mule.runtime.api.security.DefaultMuleAuthentication) Authentication(org.mule.runtime.api.security.Authentication) DefaultMuleCredentials(org.mule.runtime.core.api.security.DefaultMuleCredentials) DefaultMuleAuthentication(org.mule.runtime.api.security.DefaultMuleAuthentication) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 2 with Authentication

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;
}
Also used : Authentication(org.mule.runtime.api.security.Authentication) DefaultMuleAuthentication(org.mule.runtime.api.security.DefaultMuleAuthentication) SecurityContext(org.mule.runtime.api.security.SecurityContext) UnauthorisedException(org.mule.runtime.api.security.UnauthorisedException)

Example 3 with Authentication

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;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Authentication(org.mule.runtime.api.security.Authentication)

Example 4 with 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());
}
Also used : SecurityProviderNotFoundException(org.mule.runtime.api.security.SecurityProviderNotFoundException) Authentication(org.mule.runtime.api.security.Authentication) SecurityProvider(org.mule.runtime.core.api.security.SecurityProvider) UnauthorisedException(org.mule.runtime.api.security.UnauthorisedException) UnauthorisedException(org.mule.runtime.api.security.UnauthorisedException) SecurityException(org.mule.runtime.api.security.SecurityException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) SecurityProviderNotFoundException(org.mule.runtime.api.security.SecurityProviderNotFoundException) UnknownAuthenticationTypeException(org.mule.runtime.api.security.UnknownAuthenticationTypeException)

Example 5 with Authentication

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;
}
Also used : Authentication(org.mule.runtime.api.security.Authentication) DefaultMuleAuthentication(org.mule.runtime.api.security.DefaultMuleAuthentication) SecurityContext(org.mule.runtime.api.security.SecurityContext) DefaultMuleCredentials(org.mule.runtime.core.api.security.DefaultMuleCredentials) DefaultMuleAuthentication(org.mule.runtime.api.security.DefaultMuleAuthentication) UnauthorisedException(org.mule.runtime.api.security.UnauthorisedException) CredentialsNotSetException(org.mule.runtime.core.internal.security.CredentialsNotSetException) Credentials(org.mule.runtime.api.security.Credentials) DefaultMuleCredentials(org.mule.runtime.core.api.security.DefaultMuleCredentials) EncryptionStrategyNotFoundException(org.mule.runtime.core.api.security.EncryptionStrategyNotFoundException) UnauthorisedException(org.mule.runtime.api.security.UnauthorisedException) SecurityException(org.mule.runtime.api.security.SecurityException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) CryptoFailureException(org.mule.runtime.core.api.security.CryptoFailureException) SecurityProviderNotFoundException(org.mule.runtime.api.security.SecurityProviderNotFoundException) CredentialsNotSetException(org.mule.runtime.core.internal.security.CredentialsNotSetException) UnknownAuthenticationTypeException(org.mule.runtime.api.security.UnknownAuthenticationTypeException)

Aggregations

Authentication (org.mule.runtime.api.security.Authentication)7 DefaultMuleAuthentication (org.mule.runtime.api.security.DefaultMuleAuthentication)5 SecurityContext (org.mule.runtime.api.security.SecurityContext)3 UnauthorisedException (org.mule.runtime.api.security.UnauthorisedException)3 DefaultMuleCredentials (org.mule.runtime.core.api.security.DefaultMuleCredentials)3 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)2 SecurityException (org.mule.runtime.api.security.SecurityException)2 SecurityProviderNotFoundException (org.mule.runtime.api.security.SecurityProviderNotFoundException)2 UnknownAuthenticationTypeException (org.mule.runtime.api.security.UnknownAuthenticationTypeException)2 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 Credentials (org.mule.runtime.api.security.Credentials)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 CryptoFailureException (org.mule.runtime.core.api.security.CryptoFailureException)1 EncryptionStrategyNotFoundException (org.mule.runtime.core.api.security.EncryptionStrategyNotFoundException)1 SecurityProvider (org.mule.runtime.core.api.security.SecurityProvider)1 CredentialsNotSetException (org.mule.runtime.core.internal.security.CredentialsNotSetException)1 DefaultSecurityContextFactory (org.mule.runtime.core.internal.security.DefaultSecurityContextFactory)1