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:software.coolstuff.springframework.owncloud.service.impl.AbstractOwncloudServiceTest.java

protected final String getSecurityContextBasicAuthorizationHeader() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return "Basic " + Base64.getEncoder()
            .encodeToString((authentication.getName() + ":" + authentication.getCredentials()).getBytes());
}

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

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getName();
    if (StringUtils.isBlank(username)) {
        log.error("Username is null or empty");
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad Credentials"));
    }/*  www  .j a  va 2  s .c o m*/

    String password = authentication.getCredentials() != null ? authentication.getCredentials().toString()
            : null;
    if (StringUtils.isBlank(password)) {
        log.error("Password is null or empty");
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad Credentials"));
    }

    log.debug("Get Information about User {} from the Resource Service", username);
    OwncloudLocalUserData.User user = localDataService.getUser(username);
    if (user == null) {
        log.error("User {} has not been found", username);
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad Credentials"));
    }
    if (!user.isEnabled()) {
        log.error("User {} is disabled", username);
        throw new DisabledException(
                messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled", "Disabled"));
    }

    if (!StringUtils.equals(password, user.getPassword())) {
        log.error("Wrong Password of User {}", username);
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad Credentials"));
    }

    log.debug("Set a new UsernamePasswordAuthenticationToken with User {} to the SecurityContextHolder",
            username);
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(username, password));

    log.info("User {} has been successfully authenticated. Get Information from UserDetailsService", username);
    OwncloudUserDetails owncloudUserDetails = (OwncloudUserDetails) userDetailsService
            .loadUserByUsername(username);
    log.trace("Set the Password of User {} to the Authentication Object", username);
    owncloudUserDetails.setPassword(password);

    return new UsernamePasswordAuthenticationToken(owncloudUserDetails, password,
            grantedAuthoritiesMappingService.mapGrantedAuthorities(owncloudUserDetails));
}

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   ww  w . ja v a2  s  . c om
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:software.coolstuff.springframework.owncloud.service.impl.rest.OwncloudRestAuthenticationProviderImpl.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getName();
    if (StringUtils.isBlank(username)) {
        log.warn("Username is null or empty");
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad Credentials"));
    }//w ww .  j a v a 2  s.c o m

    String password = authentication.getCredentials() != null ? authentication.getCredentials().toString()
            : null;
    if (StringUtils.isBlank(password)) {
        log.warn("Password is null or empty");
        throw new BadCredentialsException(messages
                .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad Credentials"));
    }

    log.debug("Try to get Information about User {} from Location {}", username, getLocation());
    Ocs.User user = exchange("/cloud/users/{user}", HttpMethod.GET, emptyEntity(username, password),
            Ocs.User.class, username);
    if (!user.getData().isEnabled()) {
        log.error("User {} is disabled", username);
        throw new DisabledException(
                messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled", "Disabled"));
    }

    log.debug("Set a new UsernamePasswordAuthenticationToken with User {} to the SecurityContextHolder",
            username);
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(username, password));

    log.info("User {} has been successfully authenticated. Get Information from UserDetailsService", username);
    OwncloudUserDetails owncloudUserDetails = userDetailsService.loadPreloadedUserByUsername(username, user);
    log.trace("Set the Password of User {} to the Authentication Object", username);
    owncloudUserDetails.setPassword(password);

    return new UsernamePasswordAuthenticationToken(owncloudUserDetails, password,
            grantedAuthoritiesMappingService.mapGrantedAuthorities(owncloudUserDetails));
}

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

private String getBasicAuthorizationHeader() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Encoder base64Encoder = Base64.getEncoder();
    String encodedCredentials = base64Encoder.encodeToString(
            (authentication.getName() + ':' + (String) authentication.getCredentials()).getBytes());
    return "Basic " + encodedCredentials;
}

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

public static String encodeCredentialsForBasicAuthorization(Authentication authentication) {
    Validate.notNull(authentication);//from w  w w . j a va2 s . c om
    if (authentication.getCredentials() != null) {
        return encodeCredentialsForBasicAuthorization(authentication.getName(),
                authentication.getCredentials());
    }
    UserDetails userDetails = (UserDetails) authentication.getPrincipal();
    return encodeCredentialsForBasicAuthorization(userDetails.getUsername(), userDetails.getPassword());
}