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

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

Introduction

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

Prototype

Object getDetails();

Source Link

Document

Stores additional details about the authentication request.

Usage

From source file:org.springframework.security.extensions.kerberos.KerberosServiceAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    KerberosServiceRequestToken auth = (KerberosServiceRequestToken) authentication;
    byte[] token = auth.getToken();
    LOG.debug("Try to validate Kerberos Token");
    String username = this.ticketValidator.validateTicket(token);
    LOG.debug("Succesfully validated " + username);
    UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
    userDetailsChecker.check(userDetails);
    additionalAuthenticationChecks(userDetails, auth);
    KerberosServiceRequestToken responseAuth = new KerberosServiceRequestToken(userDetails,
            userDetails.getAuthorities(), token);
    responseAuth.setDetails(authentication.getDetails());
    return responseAuth;

}

From source file:org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    KerberosServiceRequestToken auth = (KerberosServiceRequestToken) authentication;
    byte[] token = auth.getToken();
    LOG.debug("Try to validate Kerberos Token");
    KerberosTicketValidation ticketValidation = this.ticketValidator.validateTicket(token);
    LOG.debug("Successfully validated " + ticketValidation.username());
    UserDetails userDetails = this.userDetailsService.loadUserByUsername(ticketValidation.username());
    userDetailsChecker.check(userDetails);
    additionalAuthenticationChecks(userDetails, auth);
    KerberosServiceRequestToken responseAuth = new KerberosServiceRequestToken(userDetails, ticketValidation,
            userDetails.getAuthorities(), token);
    responseAuth.setDetails(authentication.getDetails());
    return responseAuth;
}

From source file:org.springframework.security.provisioning.JdbcUserDetailsManager.java

protected Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
    UserDetails user = loadUserByUsername(currentAuth.getName());

    UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user, null,
            user.getAuthorities());// w w w .  ja v a2  s.c  o  m
    newAuthentication.setDetails(currentAuth.getDetails());

    return newAuthentication;
}

From source file:org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider.java

/**
 * Authenticate the given PreAuthenticatedAuthenticationToken.
 * <p>//from w  w w.jav  a  2 s  .  com
 * If the principal contained in the authentication object is null, the request will
 * be ignored to allow other providers to authenticate it.
 */
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        return null;
    }

    if (logger.isDebugEnabled()) {
        logger.debug("PreAuthenticated authentication request: " + authentication);
    }

    if (authentication.getPrincipal() == null) {
        logger.debug("No pre-authenticated principal found in request.");

        if (throwExceptionWhenTokenRejected) {
            throw new BadCredentialsException("No pre-authenticated principal found in request.");
        }
        return null;
    }

    if (authentication.getCredentials() == null) {
        logger.debug("No pre-authenticated credentials found in request.");

        if (throwExceptionWhenTokenRejected) {
            throw new BadCredentialsException("No pre-authenticated credentials found in request.");
        }
        return null;
    }

    UserDetails ud = preAuthenticatedUserDetailsService
            .loadUserDetails((PreAuthenticatedAuthenticationToken) authentication);

    userDetailsChecker.check(ud);

    PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(ud,
            authentication.getCredentials(), ud.getAuthorities());
    result.setDetails(authentication.getDetails());

    return result;
}

From source file:org.springframework.ws.soap.security.x509.X509AuthenticationProvider.java

/**
 * If the supplied authentication token contains a certificate then this will be passed to the configured
 * {@link X509AuthoritiesPopulator} to obtain the user details and authorities for the user identified by the
 * certificate.<p>If no certificate is present (for example, if the filter is applied to an HttpRequest for
 * which client authentication hasn't been configured in the container) then a BadCredentialsException will be
 * raised.</p>//from   ww  w .  j av  a  2s.c  o  m
 *
 * @param authentication the authentication request.
 *
 * @return an X509AuthenticationToken containing the authorities of the principal represented by the certificate.
 *
 * @throws AuthenticationException if the {@link X509AuthoritiesPopulator} rejects the certficate.
 * @throws BadCredentialsException if no certificate was presented in the authentication request.
 */
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        return null;
    }

    if (logger.isDebugEnabled()) {
        logger.debug("X509 authentication request: " + authentication);
    }

    X509Certificate clientCertificate = (X509Certificate) authentication.getCredentials();

    if (clientCertificate == null) {
        throw new BadCredentialsException(
                messages.getMessage("X509AuthenticationProvider.certificateNull", "Certificate is null"));
    }

    UserDetails user = userCache.getUserFromCache(clientCertificate);

    if (user == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Authenticating with certificate " + clientCertificate);
        }
        user = x509AuthoritiesPopulator.getUserDetails(clientCertificate);
        userCache.putUserInCache(clientCertificate, user);
    }

    X509AuthenticationToken result = new X509AuthenticationToken(user, clientCertificate,
            user.getAuthorities());

    result.setDetails(authentication.getDetails());

    return result;
}

From source file:org.tightblog.ui.security.MultiFactorAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    // check username & password first
    Authentication result = super.authenticate(auth);

    // if here, username & password were correct, so check validation code if we're using MFA
    if (mfaEnabled) {
        String verificationCode = ((CustomWebAuthenticationDetails) auth.getDetails()).getVerificationCode();

        UserCredentials creds = userCredentialsRepository.findByUserName(auth.getName());

        if (creds.getMfaSecret() != null) {
            Totp totp = new Totp(creds.getMfaSecret());
            if (!isValidLong(verificationCode) || !totp.verify(verificationCode)) {
                throw new InvalidVerificationCodeException("Google Authenticator code invalid");
            }//w ww. j  av  a 2s  .c o m
        }
    }

    return result;
}

From source file:org.training.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-email", method = RequestMethod.POST)
@RequireHardLogIn//from  ww  w.  jav  a 2s .  co m
public String updateEmail(final UpdateEmailForm updateEmailForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectAttributes) throws CMSItemNotFoundException {
    getEmailValidator().validate(updateEmailForm, bindingResult);
    String returnAction = REDIRECT_TO_UPDATE_EMAIL_PAGE;

    if (!bindingResult.hasErrors() && !updateEmailForm.getEmail().equals(updateEmailForm.getChkEmail())) {
        bindingResult.rejectValue("chkEmail", "validation.checkEmail.equals", new Object[] {},
                "validation.checkEmail.equals");
    }

    if (bindingResult.hasErrors()) {
        returnAction = setErrorMessagesAndCMSPage(model, UPDATE_EMAIL_CMS_PAGE);
    } else {
        try {
            customCustomerFacade.changeUid(updateEmailForm.getEmail(), updateEmailForm.getPassword());
            GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                    "text.account.profile.confirmationUpdated", null);

            // Replace the spring security authentication with the new UID
            final String newUid = customCustomerFacade.getCurrentCustomer().getUid().toLowerCase();
            final Authentication oldAuthentication = SecurityContextHolder.getContext().getAuthentication();
            final UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(
                    newUid, null, oldAuthentication.getAuthorities());
            newAuthentication.setDetails(oldAuthentication.getDetails());
            SecurityContextHolder.getContext().setAuthentication(newAuthentication);
        } catch (final DuplicateUidException e) {
            bindingResult.rejectValue("email", "profile.email.unique");
            returnAction = setErrorMessagesAndCMSPage(model, UPDATE_EMAIL_CMS_PAGE);
        } catch (final PasswordMismatchException passwordMismatchException) {
            bindingResult.rejectValue("password", PROFILE_CURRENT_PASSWORD_INVALID);
            returnAction = setErrorMessagesAndCMSPage(model, UPDATE_EMAIL_CMS_PAGE);
        }
    }

    return returnAction;
}

From source file:software.coolstuff.springframework.owncloud.service.impl.OwncloudUtils.java

/**
 * Checks, if the given Authentication Object is authenticated
 * by the Owncloud{Rest|Local}AuthenticationProvider
 * @param authentication Authentication Object
 * @return is authenticated by the Owncloud{Rest|Local}AuthenticationProvider
 *//*from w ww  .j  a v a2  s  .  com*/
public static boolean isValidAuthentication(Authentication authentication) {
    if (authentication == null) {
        return false;
    }

    // if UserDetails are set then it must be of Class OwncloudUserDetails
    if (authentication.getDetails() != null
            && !ClassUtils.isAssignable(authentication.getPrincipal().getClass(), OwncloudUserDetails.class)) {
        return false;
    }

    if (authentication.getCredentials() != null) {
        // if Credentials are available then these must be of Class CharSequence and not empty
        return CharSequence.class.isAssignableFrom(authentication.getCredentials().getClass())
                && StringUtils.isNotBlank((CharSequence) authentication.getCredentials());
    }

    if (authentication.getPrincipal() != null) {
        // Password of the UserDetails must not be empty
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return StringUtils.isNotBlank(userDetails.getPassword());
    }

    return false;
}

From source file:ubc.pavlab.aspiredb.server.security.authentication.UserManagerImpl.java

protected Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
    UserDetails user = loadUserByUsername(currentAuth.getName());

    UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user,
            user.getPassword(), user.getAuthorities());
    newAuthentication.setDetails(currentAuth.getDetails());

    return newAuthentication;
}

From source file:ubic.gemma.core.security.authentication.UserManagerImpl.java

private Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
    UserDetails user = this.loadUserByUsername(currentAuth.getName());

    UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user,
            user.getPassword(), user.getAuthorities());
    newAuthentication.setDetails(currentAuth.getDetails());

    return newAuthentication;
}