Search in sources :

Example 1 with UsernamePasswordAuthentication

use of org.apache.ftpserver.usermanager.UsernamePasswordAuthentication in project ddf by codice.

the class UserManagerImplTest method nullShiroSubject.

@Test(expected = AuthenticationFailedException.class)
public void nullShiroSubject() throws SecurityServiceException, AuthenticationFailedException {
    UsernamePasswordAuthentication upa = mock(UsernamePasswordAuthentication.class);
    when(upa.getUsername()).thenReturn(USER);
    when(upa.getPassword()).thenReturn(PASSWORD);
    when(securityManager.getSubject(upa)).thenReturn(null);
    userManager.authenticate(upa);
}
Also used : UsernamePasswordAuthentication(org.apache.ftpserver.usermanager.UsernamePasswordAuthentication) Test(org.junit.Test)

Example 2 with UsernamePasswordAuthentication

use of org.apache.ftpserver.usermanager.UsernamePasswordAuthentication in project ddf by codice.

the class UserManagerImplTest method shiroUnsupportedAuthentication.

@Test(expected = AuthenticationFailedException.class)
public void shiroUnsupportedAuthentication() throws SecurityServiceException, AuthenticationFailedException {
    UsernamePasswordAuthentication upa = mock(UsernamePasswordAuthentication.class);
    when(upa.getUsername()).thenReturn(USER);
    when(upa.getPassword()).thenReturn(PASSWORD);
    when(securityManager.getSubject(any(Authentication.class))).thenThrow(SecurityServiceException.class);
    userManager.authenticate(upa);
}
Also used : AnonymousAuthentication(org.apache.ftpserver.usermanager.AnonymousAuthentication) Authentication(org.apache.ftpserver.ftplet.Authentication) UsernamePasswordAuthentication(org.apache.ftpserver.usermanager.UsernamePasswordAuthentication) UsernamePasswordAuthentication(org.apache.ftpserver.usermanager.UsernamePasswordAuthentication) Test(org.junit.Test)

Example 3 with UsernamePasswordAuthentication

use of org.apache.ftpserver.usermanager.UsernamePasswordAuthentication in project ddf by codice.

the class UserManagerImplTest method authenticationSuccess.

@Test
public void authenticationSuccess() throws SecurityServiceException, AuthenticationFailedException {
    UsernamePasswordAuthentication upa = mock(UsernamePasswordAuthentication.class);
    Subject subject = mock(Subject.class);
    when(upa.getUsername()).thenReturn(USER);
    when(upa.getPassword()).thenReturn(PASSWORD);
    when(securityManager.getSubject(any(Authentication.class))).thenReturn(subject);
    userManager.setKarafLocalRoles("admin,localhost");
    assertEquals(userManager.createUser(USER, subject), userManager.authenticate(upa));
}
Also used : AnonymousAuthentication(org.apache.ftpserver.usermanager.AnonymousAuthentication) Authentication(org.apache.ftpserver.ftplet.Authentication) UsernamePasswordAuthentication(org.apache.ftpserver.usermanager.UsernamePasswordAuthentication) UsernamePasswordAuthentication(org.apache.ftpserver.usermanager.UsernamePasswordAuthentication) Subject(ddf.security.Subject) Test(org.junit.Test)

Example 4 with UsernamePasswordAuthentication

use of org.apache.ftpserver.usermanager.UsernamePasswordAuthentication in project ddf by codice.

the class UserManagerImpl method authenticate.

/**
     * @param authentication The {@link Authentication} that proves the users identity. {@link org.apache.ftpserver.usermanager.AnonymousAuthentication} is not permitted
     * @return {@link User} upon successful authorization
     * @throws AuthenticationFailedException upon unsuccessful authorization
     */
public User authenticate(Authentication authentication) throws AuthenticationFailedException {
    UPAuthenticationToken upAuthenticationToken;
    String username;
    User user;
    if (authentication instanceof UsernamePasswordAuthentication) {
        username = ((UsernamePasswordAuthentication) authentication).getUsername();
        upAuthenticationToken = new UPAuthenticationToken(username, ((UsernamePasswordAuthentication) authentication).getPassword());
        try {
            Subject subject = securityManager.getSubject(upAuthenticationToken);
            if (subject != null) {
                if (!doesExist(username)) {
                    user = createUser(username, subject);
                } else {
                    user = getUserByName(username);
                    updateUserSubject(user, subject);
                }
                return user;
            }
        } catch (SecurityServiceException e) {
            LOGGER.info("Failure to retrieve subject.", e);
            throw new AuthenticationFailedException("Failure to retrieve subject.");
        }
    }
    throw new AuthenticationFailedException("Authentication failed");
}
Also used : SecurityServiceException(ddf.security.service.SecurityServiceException) FtpUser(ddf.catalog.ftp.user.FtpUser) User(org.apache.ftpserver.ftplet.User) AuthenticationFailedException(org.apache.ftpserver.ftplet.AuthenticationFailedException) UPAuthenticationToken(org.codice.ddf.security.handler.api.UPAuthenticationToken) UsernamePasswordAuthentication(org.apache.ftpserver.usermanager.UsernamePasswordAuthentication) Subject(ddf.security.Subject)

Aggregations

UsernamePasswordAuthentication (org.apache.ftpserver.usermanager.UsernamePasswordAuthentication)4 Test (org.junit.Test)3 Subject (ddf.security.Subject)2 Authentication (org.apache.ftpserver.ftplet.Authentication)2 AnonymousAuthentication (org.apache.ftpserver.usermanager.AnonymousAuthentication)2 FtpUser (ddf.catalog.ftp.user.FtpUser)1 SecurityServiceException (ddf.security.service.SecurityServiceException)1 AuthenticationFailedException (org.apache.ftpserver.ftplet.AuthenticationFailedException)1 User (org.apache.ftpserver.ftplet.User)1 UPAuthenticationToken (org.codice.ddf.security.handler.api.UPAuthenticationToken)1