Search in sources :

Example 66 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class CasAuthenticationTokenMixinTests method deserializeCasAuthenticationTestAfterEraseCredentialInvoked.

@Test
public void deserializeCasAuthenticationTestAfterEraseCredentialInvoked() throws Exception {
    CasAuthenticationToken token = mapper.readValue(CAS_TOKEN_CLEARED_JSON, CasAuthenticationToken.class);
    assertThat(((UserDetails) token.getPrincipal()).getPassword()).isNull();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) CasAuthenticationToken(org.springframework.security.cas.authentication.CasAuthenticationToken) Test(org.junit.Test)

Example 67 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class GrantedAuthorityFromAssertionAttributesUserDetailsServiceTests method correctlyExtractsNamedAttributesFromAssertionAndConvertsThemToAuthorities.

@Test
public void correctlyExtractsNamedAttributesFromAssertionAndConvertsThemToAuthorities() {
    GrantedAuthorityFromAssertionAttributesUserDetailsService uds = new GrantedAuthorityFromAssertionAttributesUserDetailsService(new String[] { "a", "b", "c", "d" });
    uds.setConvertToUpperCase(false);
    Assertion assertion = mock(Assertion.class);
    AttributePrincipal principal = mock(AttributePrincipal.class);
    Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("a", Arrays.asList("role_a1", "role_a2"));
    attributes.put("b", "role_b");
    attributes.put("c", "role_c");
    attributes.put("d", null);
    attributes.put("someother", "unused");
    when(assertion.getPrincipal()).thenReturn(principal);
    when(principal.getAttributes()).thenReturn(attributes);
    when(principal.getName()).thenReturn("somebody");
    CasAssertionAuthenticationToken token = new CasAssertionAuthenticationToken(assertion, "ticket");
    UserDetails user = uds.loadUserDetails(token);
    Set<String> roles = AuthorityUtils.authorityListToSet(user.getAuthorities());
    assertThat(roles.size()).isEqualTo(4);
    assertThat(roles).contains("role_a1");
    assertThat(roles).contains("role_a2");
    assertThat(roles).contains("role_b");
    assertThat(roles).contains("role_c");
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) HashMap(java.util.HashMap) Assertion(org.jasig.cas.client.validation.Assertion) CasAssertionAuthenticationToken(org.springframework.security.cas.authentication.CasAssertionAuthenticationToken) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal) Test(org.junit.Test)

Example 68 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class AbstractUserDetailsAuthenticationProvider method authenticate.

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, messages.getMessage("AbstractUserDetailsAuthenticationProvider.onlySupports", "Only UsernamePasswordAuthenticationToken is supported"));
    // Determine username
    String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED" : authentication.getName();
    boolean cacheWasUsed = true;
    UserDetails user = this.userCache.getUserFromCache(username);
    if (user == null) {
        cacheWasUsed = false;
        try {
            user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
        } catch (UsernameNotFoundException notFound) {
            logger.debug("User '" + username + "' not found");
            if (hideUserNotFoundExceptions) {
                throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
            } else {
                throw notFound;
            }
        }
        Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract");
    }
    try {
        preAuthenticationChecks.check(user);
        additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);
    } catch (AuthenticationException exception) {
        if (cacheWasUsed) {
            // There was a problem, so try again after checking
            // we're using latest data (i.e. not from the cache)
            cacheWasUsed = false;
            user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
            preAuthenticationChecks.check(user);
            additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);
        } else {
            throw exception;
        }
    }
    postAuthenticationChecks.check(user);
    if (!cacheWasUsed) {
        this.userCache.putUserInCache(user);
    }
    Object principalToReturn = user;
    if (forcePrincipalAsString) {
        principalToReturn = user.getUsername();
    }
    return createSuccessAuthentication(principalToReturn, authentication, user);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 69 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class JaasNameCallbackHandler method handle.

// ~ Methods
// ========================================================================================================
/**
	 * If the callback passed to the 'handle' method is an instance of NameCallback, the
	 * JaasNameCallbackHandler will call,
	 * callback.setName(authentication.getPrincipal().toString()).
	 *
	 * @param callback
	 * @param authentication
	 *
	 * @throws IOException
	 * @throws UnsupportedCallbackException
	 */
public void handle(Callback callback, Authentication authentication) throws IOException, UnsupportedCallbackException {
    if (callback instanceof NameCallback) {
        NameCallback ncb = (NameCallback) callback;
        String username;
        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        ncb.setName(username);
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) UserDetails(org.springframework.security.core.userdetails.UserDetails)

Example 70 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class JdbcUserDetailsManager method createNewAuthentication.

protected Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
    UserDetails user = loadUserByUsername(currentAuth.getName());
    UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
    newAuthentication.setDetails(currentAuth.getDetails());
    return newAuthentication;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Aggregations

UserDetails (org.springframework.security.core.userdetails.UserDetails)111 Test (org.junit.Test)42 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)33 Authentication (org.springframework.security.core.Authentication)32 GrantedAuthority (org.springframework.security.core.GrantedAuthority)17 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)15 User (org.springframework.security.core.userdetails.User)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)10 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 Transactional (org.springframework.transaction.annotation.Transactional)6 Date (java.util.Date)4 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 AuthenticationException (org.springframework.security.core.AuthenticationException)4