Search in sources :

Example 1 with Authentication

use of org.apache.jackrabbit.core.security.authentication.Authentication in project pentaho-platform by pentaho.

the class SpringSecurityLoginModule method getAuthentication.

/**
 * {@inheritDoc}
 *
 * Creates a {@code UsernamePasswordAuthenticationToken} from the given {@code principal} and {@code credentials}
 * and passes to Spring Security {@code AuthenticationManager}.
 */
@Override
protected Authentication getAuthentication(final Principal principal, final Credentials credentials) throws RepositoryException {
    // only handles SimpleCredential instances; DefaultLoginModule behaves the same way (albeit indirectly)
    if (!(credentials instanceof SimpleCredentials)) {
        // $NON-NLS-1$
        logger.debug("credentials not instance of SimpleCredentials; returning null");
        return null;
    }
    SimpleCredentials simpleCredentials = (SimpleCredentials) credentials;
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(simpleCredentials.getUserID(), String.valueOf(simpleCredentials.getPassword()));
    boolean authenticated = false;
    try {
        org.springframework.security.core.Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getName().equals(simpleCredentials.getUserID())) {
            // see if there's already an active Authentication for this user.
            authenticated = true;
        } else {
            // delegate to Spring Security
            getAuthenticationManager().authenticate(token);
            authenticated = true;
        }
    } catch (AuthenticationException e) {
        // $NON-NLS-1$
        logger.debug("authentication exception", e);
    }
    final boolean authenticateResult = authenticated;
    return new Authentication() {

        public boolean canHandle(Credentials credentials) {
            // this is decided earlier in getAuthentication
            return true;
        }

        public boolean authenticate(Credentials credentials) throws RepositoryException {
            return authenticateResult;
        }
    };
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.apache.jackrabbit.core.security.authentication.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials)

Aggregations

Credentials (javax.jcr.Credentials)1 SimpleCredentials (javax.jcr.SimpleCredentials)1 Authentication (org.apache.jackrabbit.core.security.authentication.Authentication)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1