Search in sources :

Example 46 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority 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 47 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority 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 48 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority 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)

Example 49 with SimpleGrantedAuthority

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

the class HumanCoffeeNetUserTest method isAdmin.

@Test
public void isAdmin() {
    List<GrantedAuthority> authorities = singletonList(new SimpleGrantedAuthority("ROLE_COFFEENET-ADMIN"));
    HumanCoffeeNetUser sut = new HumanCoffeeNetUser("username", "email@coffeenet", authorities);
    assertThat(sut.isCoffeeNetAdmin(), is(true));
}
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)

Example 50 with SimpleGrantedAuthority

use of org.springframework.security.core.authority.SimpleGrantedAuthority in project dhis2-core by dhis2.

the class DhisConvenienceTest method saveAndInjectUserSecurityContext.

protected void saveAndInjectUserSecurityContext(User user) {
    userService.addUser(user);
    userService.addUserCredentials(user.getUserCredentials());
    List<GrantedAuthority> grantedAuthorities = user.getUserCredentials().getAllAuthorities().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUserCredentials().getUsername(), user.getUserCredentials().getPassword(), grantedAuthorities);
    Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, "", grantedAuthorities);
    SecurityContextHolder.getContext().setAuthentication(authentication);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.hisp.dhis.user.User) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Aggregations

SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)109 GrantedAuthority (org.springframework.security.core.GrantedAuthority)64 Test (org.junit.Test)49 ArrayList (java.util.ArrayList)30 Authentication (org.springframework.security.core.Authentication)27 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)22 HashSet (java.util.HashSet)14 User (org.springframework.security.core.userdetails.User)11 ObjectIdentity (org.springframework.security.acls.model.ObjectIdentity)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 MutableAcl (org.springframework.security.acls.model.MutableAcl)7 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)7 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)7 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)7 Before (org.junit.Before)6 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)6 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)6 List (java.util.List)5 Map (java.util.Map)5 DBUnitTest (org.orcid.test.DBUnitTest)5