Search in sources :

Example 76 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project spring-boot by spring-projects.

the class UserInfoTokenServices method extractAuthentication.

private OAuth2Authentication extractAuthentication(Map<String, Object> map) {
    Object principal = getPrincipal(map);
    List<GrantedAuthority> authorities = this.authoritiesExtractor.extractAuthorities(map);
    OAuth2Request request = new OAuth2Request(null, this.clientId, null, true, null, null, null, null, null);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
    token.setDetails(map);
    return new OAuth2Authentication(request, token);
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) GrantedAuthority(org.springframework.security.core.GrantedAuthority) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 77 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project coffeenet-starter by coffeenet.

the class CoffeeNetWebExtractorTest method extractApps.

@Test
public void extractApps() {
    Map<String, List<CoffeeNetApp>> apps = new HashMap<>();
    CoffeeNetApp coffeeNetApp = new CoffeeNetApp("Coffee App", "coffeeapp.coffeenet", emptySet());
    apps.put("cna1", singletonList(coffeeNetApp));
    CoffeeNetApp profileApp = new CoffeeNetApp("Profile", "profile.coffeenet", emptySet());
    apps.put("profile", singletonList(profileApp));
    sut.registerService(APP_SERVICE, coffeeNetAppServiceMock);
    when(coffeeNetAppServiceMock.getApps(any())).thenReturn(apps);
    // user
    sut.registerService(USER_SERVICE, coffeeNetCurrentUserServiceMock);
    List<GrantedAuthority> authorities = singletonList(new SimpleGrantedAuthority("ROLE_COFFEENET-ADMIN"));
    Optional<CoffeeNetUserDetails> user = of(new HumanCoffeeNetUser("username", "email", authorities));
    when(coffeeNetCurrentUserServiceMock.get()).thenReturn(user);
    Optional<Map<String, List<CoffeeNetApp>>> extractedApps = sut.extractApps();
    Map<String, List<CoffeeNetApp>> coffeeNetApps = extractedApps.get();
    assertThat(coffeeNetApps).hasSize(2);
    assertThat(coffeeNetApps.get("apps")).hasSize(1);
    assertThat(coffeeNetApps.get("apps").get(0)).isSameAs(coffeeNetApp);
    assertThat(coffeeNetApps.get("profile")).hasSize(1);
    assertThat(coffeeNetApps.get("profile").get(0)).isSameAs(profileApp);
}
Also used : HashMap(java.util.HashMap) HumanCoffeeNetUser(coffee.synyx.autoconfigure.security.service.HumanCoffeeNetUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) CoffeeNetUserDetails(coffee.synyx.autoconfigure.security.service.CoffeeNetUserDetails) CoffeeNetApp(coffee.synyx.autoconfigure.discovery.service.CoffeeNetApp) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 78 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project coffeenet-starter by coffeenet.

the class CoffeeNetWebExtractorTest method extractAppsNoProfile.

@Test
public void extractAppsNoProfile() {
    Map<String, List<CoffeeNetApp>> apps = new HashMap<>();
    CoffeeNetApp coffeeNetApp = new CoffeeNetApp("Coffee App", "coffeeapp.coffeenet", emptySet());
    apps.put("apps", singletonList(coffeeNetApp));
    sut.registerService(APP_SERVICE, coffeeNetAppServiceMock);
    when(coffeeNetAppServiceMock.getApps(any())).thenReturn(apps);
    // user
    sut.registerService(USER_SERVICE, coffeeNetCurrentUserServiceMock);
    List<GrantedAuthority> authorities = singletonList(new SimpleGrantedAuthority("ROLE_COFFEENET-ADMIN"));
    Optional<CoffeeNetUserDetails> user = of(new HumanCoffeeNetUser("username", "email", authorities));
    when(coffeeNetCurrentUserServiceMock.get()).thenReturn(user);
    Optional<Map<String, List<CoffeeNetApp>>> extractedApps = sut.extractApps();
    Map<String, List<CoffeeNetApp>> coffeeNetApps = extractedApps.get();
    assertThat(coffeeNetApps).hasSize(1);
    assertThat(coffeeNetApps.get("apps")).hasSize(1);
    assertThat(coffeeNetApps.get("apps").get(0)).isSameAs(coffeeNetApp);
    assertThat(coffeeNetApps.get("profile")).isNull();
}
Also used : HashMap(java.util.HashMap) HumanCoffeeNetUser(coffee.synyx.autoconfigure.security.service.HumanCoffeeNetUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) CoffeeNetUserDetails(coffee.synyx.autoconfigure.security.service.CoffeeNetUserDetails) CoffeeNetApp(coffee.synyx.autoconfigure.discovery.service.CoffeeNetApp) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 79 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project coffeenet-starter by coffeenet.

the class CoffeeNetAuthoritiesExtractor method extractAuthoritiesOutOfPrincipal.

private static List<GrantedAuthority> extractAuthoritiesOutOfPrincipal(Map principal) {
    List<GrantedAuthority> authoritiesList;
    if (principal.containsKey(AUTHORITIES) && principal.get(AUTHORITIES) instanceof Collection) {
        String authorities = collectionToCommaDelimitedString((Collection) principal.get(AUTHORITIES));
        authoritiesList = AuthorityUtils.commaSeparatedStringToAuthorityList(authorities);
    } else {
        LOGGER.warn(ERROR_MSG);
        authoritiesList = AuthorityUtils.commaSeparatedStringToAuthorityList(DEFAULT_AUTHORITY);
    }
    return authoritiesList;
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) Collection(java.util.Collection) StringUtils.collectionToCommaDelimitedString(org.springframework.util.StringUtils.collectionToCommaDelimitedString)

Example 80 with GrantedAuthority

use of org.springframework.security.core.GrantedAuthority in project coffeenet-starter by coffeenet.

the class HumanCoffeeNetUserTest method isAdminNotAndWrongAuthorities.

@Test
public void isAdminNotAndWrongAuthorities() {
    List<GrantedAuthority> authorities = singletonList(new SimpleGrantedAuthority("ROLE_NO"));
    HumanCoffeeNetUser sut = new HumanCoffeeNetUser("username", "email@coffeenet", authorities);
    assertThat(sut.isCoffeeNetAdmin(), is(false));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.Test)

Aggregations

GrantedAuthority (org.springframework.security.core.GrantedAuthority)188 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)90 Authentication (org.springframework.security.core.Authentication)55 ArrayList (java.util.ArrayList)43 Test (org.junit.Test)42 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)37 HashSet (java.util.HashSet)27 UserDetails (org.springframework.security.core.userdetails.UserDetails)16 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)15 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)11 Before (org.junit.Before)10 SecurityContext (org.springframework.security.core.context.SecurityContext)10 User (org.springframework.security.core.userdetails.User)10 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)10 DefaultGrantedAuthority (eu.bcvsolutions.idm.core.security.api.domain.DefaultGrantedAuthority)9 List (java.util.List)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)9 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8