// Path: spring-security-cas-extension/src/main/java/com/kakawait/spring/security/cas/userdetails/GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService.java
// public class GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService
//         extends AbstractCasAssertionUserDetailsService {
// 
//     private static final String NON_EXISTENT_PASSWORD_VALUE = "NO_PASSWORD";
// 
//     private final String[] attributes;
// 
//     private final Collection<? extends GrantedAuthority> defaultGrantedAuthorities;
// 
//     private boolean toUppercase = true;
// 
//     public GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService(String[] attributes,
//             Collection<? extends GrantedAuthority> defaultGrantedAuthorities) {
//         this.attributes = (attributes == null) ? new String[0] : attributes;
//         this.defaultGrantedAuthorities = (defaultGrantedAuthorities == null) ? new ArrayList<>()
//                                                                              : defaultGrantedAuthorities;
//     }
// 
//     protected UserDetails loadUserDetails(Assertion assertion) {
//         String username = assertion.getPrincipal().getName();
//         if (!StringUtils.hasText(username)) {
//             throw new UsernameNotFoundException("Unable to retrieve username from CAS assertion");
//         }
// 
//         Map<String, Object> principalAttributes = assertion.getPrincipal().getAttributes();
//         List<GrantedAuthority> authorities = Arrays
//                 .stream(attributes)
//                 .map(principalAttributes::get)
//                 .filter(Objects::nonNull)
//                 .flatMap(v -> (v instanceof Collection) ? ((Collection<?>) v).stream() : Stream.of(v))
//                 .map(v -> toUppercase ? v.toString().toUpperCase() : v.toString())
//                 .map(r -> r.replaceFirst("^ROLE_", ""))
//                 .map(r -> new SimpleGrantedAuthority("ROLE_" + r))
//                 .collect(Collectors.toList());
// 
//         authorities.addAll(defaultGrantedAuthorities);
// 
//         return new User(username, NON_EXISTENT_PASSWORD_VALUE, authorities);
//     }
// }


import com.kakawait.spring.security.cas.userdetails.GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.cas.userdetails.AbstractCasAssertionUserDetailsService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
package com.kakawait.spring.boot.security.cas.autoconfigure;



/**
 * @author Thibaud Leprêtre
 */
@ConditionalOnMissingBean(AbstractCasAssertionUserDetailsService.class)
class CasAssertionUserDetailsServiceConfiguration {

    @Bean
    AbstractCasAssertionUserDetailsService defaultRolesAuthenticationUserDetailsService(
            CasSecurityProperties casSecurityProperties) {
        Set<SimpleGrantedAuthority> authorities = Arrays.stream(casSecurityProperties.getUser().getDefaultRoles())
                .map(r -> r.replaceFirst("^ROLE_", ""))
                .map(r -> new SimpleGrantedAuthority("ROLE_" + r))
                .collect(Collectors.toSet());
        String[] attributes = casSecurityProperties.getUser().getRolesAttributes();
// Path: spring-security-cas-extension/src/main/java/com/kakawait/spring/security/cas/userdetails/GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService.java
// public class GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService
//         extends AbstractCasAssertionUserDetailsService {
// 
//     private static final String NON_EXISTENT_PASSWORD_VALUE = "NO_PASSWORD";
// 
//     private final String[] attributes;
// 
//     private final Collection<? extends GrantedAuthority> defaultGrantedAuthorities;
// 
//     private boolean toUppercase = true;
// 
//     public GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService(String[] attributes,
//             Collection<? extends GrantedAuthority> defaultGrantedAuthorities) {
//         this.attributes = (attributes == null) ? new String[0] : attributes;
//         this.defaultGrantedAuthorities = (defaultGrantedAuthorities == null) ? new ArrayList<>()
//                                                                              : defaultGrantedAuthorities;
//     }
// 
//     protected UserDetails loadUserDetails(Assertion assertion) {
//         String username = assertion.getPrincipal().getName();
//         if (!StringUtils.hasText(username)) {
//             throw new UsernameNotFoundException("Unable to retrieve username from CAS assertion");
//         }
// 
//         Map<String, Object> principalAttributes = assertion.getPrincipal().getAttributes();
//         List<GrantedAuthority> authorities = Arrays
//                 .stream(attributes)
//                 .map(principalAttributes::get)
//                 .filter(Objects::nonNull)
//                 .flatMap(v -> (v instanceof Collection) ? ((Collection<?>) v).stream() : Stream.of(v))
//                 .map(v -> toUppercase ? v.toString().toUpperCase() : v.toString())
//                 .map(r -> r.replaceFirst("^ROLE_", ""))
//                 .map(r -> new SimpleGrantedAuthority("ROLE_" + r))
//                 .collect(Collectors.toList());
// 
//         authorities.addAll(defaultGrantedAuthorities);
// 
//         return new User(username, NON_EXISTENT_PASSWORD_VALUE, authorities);
//     }
// }


// Path: cas-security-spring-boot-autoconfigure/src/main/java/com/kakawait/spring/boot/security/cas/autoconfigure/CasAssertionUserDetailsServiceConfiguration.java
import com.kakawait.spring.security.cas.userdetails.GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.cas.userdetails.AbstractCasAssertionUserDetailsService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

package com.kakawait.spring.boot.security.cas.autoconfigure;



/**
 * @author Thibaud Leprêtre
 */
@ConditionalOnMissingBean(AbstractCasAssertionUserDetailsService.class)
class CasAssertionUserDetailsServiceConfiguration {

    @Bean
    AbstractCasAssertionUserDetailsService defaultRolesAuthenticationUserDetailsService(
            CasSecurityProperties casSecurityProperties) {
        Set<SimpleGrantedAuthority> authorities = Arrays.stream(casSecurityProperties.getUser().getDefaultRoles())
                .map(r -> r.replaceFirst("^ROLE_", ""))
                .map(r -> new SimpleGrantedAuthority("ROLE_" + r))
                .collect(Collectors.toSet());
        String[] attributes = casSecurityProperties.getUser().getRolesAttributes();
        return new GrantedAuthoritiesFromAssertionAttributesWithDefaultRolesUserDetailsService(attributes, authorities);