Example usage for org.springframework.security.core Authentication getCredentials

List of usage examples for org.springframework.security.core Authentication getCredentials

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getCredentials.

Prototype

Object getCredentials();

Source Link

Document

The credentials that prove the principal is correct.

Usage

From source file:org.socialsignin.springsocial.security.signin.SpringSocialSecurityAuthenticationFactory.java

public Authentication updateAuthenticationForNewProviders(Authentication existingAuthentication,
        Set<String> providerIds) {
    List<GrantedAuthority> newAuthorities = new ArrayList<GrantedAuthority>();
    for (String providerId : providerIds) {
        newAuthorities.add(userAuthoritiesService.getProviderAuthority(providerId));
    }//ww w  . j a v a  2s . co m
    return createNewAuthentication(existingAuthentication.getName(),
            existingAuthentication.getCredentials() == null ? null
                    : existingAuthentication.getCredentials().toString(),
            addAuthorities(existingAuthentication, newAuthorities));
}

From source file:org.pac4j.saml.client.Saml2ClientWrapper.java

protected Saml2WrapperCredentials retrieveCredentials(final WebContext wc) throws RequiresHttpAction {

    J2EContext jc = (J2EContext) wc;/*from   w  w  w . ja  v a  2 s  .co  m*/
    HttpServletRequest request = jc.getRequest();
    HttpServletResponse response = jc.getResponse();

    org.springframework.security.core.Authentication samlAuthentication = sAMLProcessingFilter
            .attemptAuthentication(request, response);

    NameID nameID = (NameID) samlAuthentication.getPrincipal();

    SAMLCredential sAMLCredential = (SAMLCredential) samlAuthentication.getCredentials();

    List<Attribute> attributes = sAMLCredential.getAttributes();

    Saml2WrapperCredentials saml2Credentials = new Saml2WrapperCredentials(nameID, attributes,
            this.getClass().getSimpleName(), samlAuthentication);

    return saml2Credentials;

}

From source file:org.osiam.auth.login.ldap.OsiamLdapAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) {
    Preconditions.checkArgument(authentication instanceof OsiamLdapAuthentication,
            "OsiamLdapAuthenticationProvider only supports OsiamLdapAuthentication.");

    final OsiamLdapAuthentication userToken = (OsiamLdapAuthentication) authentication;

    String username = userToken.getName();
    String password = (String) authentication.getCredentials();

    if (Strings.isNullOrEmpty(username)) {
        throw new BadCredentialsException("OsiamLdapAuthenticationProvider: Empty Username");
    }// w w  w  . ja  v  a 2 s  .  c o  m

    if (Strings.isNullOrEmpty(password)) {
        throw new BadCredentialsException("OsiamLdapAuthenticationProvider: Empty Password");
    }

    User user = resourceServerConnector.getUserByUsername(username);
    checkIfInternalUserExists(user);

    DirContextOperations userData = doAuthentication(userToken);

    UserDetails ldapUser = osiamLdapUserContextMapper.mapUserFromContext(userData, authentication.getName(),
            loadUserAuthorities(userData, authentication.getName(), (String) authentication.getCredentials()));

    user = synchronizeLdapData(userData, user);

    User authUser = new User.Builder(username).setId(user.getId()).build();

    List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();

    for (Role role : user.getRoles()) {
        grantedAuthorities.add(new SimpleGrantedAuthority(role.getValue()));
    }

    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(authUser, null,
            grantedAuthorities);
    result.setDetails(authentication.getDetails());

    return result;
}

From source file:com.github.jens_meiss.blog.server.service.json.user.UserController.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {

    final String userName = authentication.getName();

    final UserDetailsDTO userDetailsDTO = userService.findByUserName(userName);
    if (userDetailsDTO == null) {
        logger.error("username not found");
        return null;
    }//  w  w  w  .  j  av a2s .c o  m

    final String crendentials = authentication.getCredentials().toString();
    if (crendentials.equals(userDetailsDTO.getPassword()) == false) {
        logger.error("password mismatch");
        return null;
    }

    logger.debug("user successfully authenticated");
    return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
            authentication.getCredentials(), new ArrayList<GrantedAuthority>());
}

From source file:com.stormpath.spring.security.provider.StormpathAuthenticationProvider.java

protected AuthenticationRequest createAuthenticationRequest(Authentication authentication) {
    String username = (String) authentication.getPrincipal();
    String password = (String) authentication.getCredentials();
    return new UsernamePasswordRequest(username, password);
}

From source file:com.hp.autonomy.frontend.configuration.authentication.DefaultLoginAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    final com.hp.autonomy.frontend.configuration.authentication.Authentication<?> authenticationConfig = configService
            .getConfig().getAuthentication();

    if (!LoginTypes.DEFAULT.equalsIgnoreCase(authenticationConfig.getMethod())) {
        return null;
    }//from   w w  w  .  j a  v a 2s.com

    final UsernameAndPassword defaultLogin = authenticationConfig.getDefaultLogin();

    final String username = authentication.getName();
    final String password = authentication.getCredentials().toString();

    if (defaultLogin.getUsername().equals(username) && defaultLogin.getPassword().equals(password)) {
        return new UsernamePasswordAuthenticationToken(username, password,
                Arrays.asList(new SimpleGrantedAuthority(roleDefault)));
    } else {
        throw new BadCredentialsException("Access is denied");
    }
}

From source file:org.cloudfoundry.identity.uaa.login.ChainedAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (authentication == null) {
        return authentication;
    }/*from   w  w  w .  ja  va 2s .  c  o  m*/
    UsernamePasswordAuthenticationToken output = null;
    if (authentication instanceof UsernamePasswordAuthenticationToken) {
        output = (UsernamePasswordAuthenticationToken) authentication;
    } else {
        output = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), authentication.getAuthorities());
        output.setAuthenticated(authentication.isAuthenticated());
        output.setDetails(authentication.getDetails());
    }
    boolean authenticated = false;
    Authentication auth = null;
    AuthenticationException lastException = null;
    for (int i = 0; i < delegates.length && (!authenticated); i++) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Attempting chained authentication of " + output + " with manager:" + delegates[i]);
            }
            auth = delegates[i].authenticate(output);
            authenticated = auth.isAuthenticated();
        } catch (AuthenticationException x) {
            if (logger.isDebugEnabled()) {
                logger.debug("Chained authentication exception:", x);
            }
            lastException = x;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Chained Authentication status of " + output + " with manager:" + delegates[i]
                    + "; Authenticated:" + authenticated);
        }
    }
    if (authenticated) {
        return auth;
    } else if (lastException != null) {
        //we had at least one authentication exception, throw it
        throw lastException;
    } else {
        //not authenticated, but return the last of the result
        return auth;
    }
}

From source file:oauth2.authentication.UserAuthenticationProvider.java

@Override
@Transactional(noRollbackFor = AuthenticationException.class)
public Authentication authenticate(Authentication authentication) {
    checkNotNull(authentication);/* ww  w  . j a v a 2 s  .c  om*/
    String userId = authentication.getName();
    User user = userRepository.findByUserId(userId);
    if (user == null) {
        LOGGER.debug("User {} not found", userId);
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
    }
    try {
        authenticationStrategy.authenticate(user, authentication.getCredentials());
        user.getLoginStatus().loginSuccessful(Instant.now());
        Set<GrantedAuthority> authorities = mapAuthorities(user);
        return createSuccessAuthentication(authentication.getPrincipal(), authentication, authorities);
    } catch (BadCredentialsException ex) {
        user.getLoginStatus().loginFailed(Instant.now());
        throw ex;
    }
}

From source file:org.pac4j.springframework.security.authentication.ClientAuthenticationProvider.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    logger.debug("authentication : {}", authentication);
    if (!supports(authentication.getClass())) {
        logger.debug("unsupported authentication class : {}", authentication.getClass());
        return null;
    }//from   ww  w  .  j  a  va  2s  . c  o m
    final ClientAuthenticationToken token = (ClientAuthenticationToken) authentication;

    // get the credentials
    final Credentials credentials = (Credentials) authentication.getCredentials();
    logger.debug("credentials : {}", credentials);

    // get the right client
    final String clientName = token.getClientName();
    final Client client = this.clients.findClient(clientName);
    // get the user profile
    final UserProfile userProfile = client.getUserProfile(credentials, null);
    logger.debug("userProfile : {}", userProfile);

    // by default, no authorities
    Collection<? extends GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    // get user details and check them
    if (this.userDetailsService != null) {
        final ClientAuthenticationToken tmpToken = new ClientAuthenticationToken(credentials, clientName,
                userProfile, null);
        final UserDetails userDetails = this.userDetailsService.loadUserDetails(tmpToken);
        logger.debug("userDetails : {}", userDetails);
        if (userDetails != null) {
            this.userDetailsChecker.check(userDetails);
            authorities = userDetails.getAuthorities();
            logger.debug("authorities : {}", authorities);
        }
    }

    // new token with credentials (like previously) and user profile and
    // authorities
    final ClientAuthenticationToken result = new ClientAuthenticationToken(credentials, clientName, userProfile,
            authorities);
    result.setDetails(authentication.getDetails());
    logger.debug("result : {}", result);
    return result;
}