Search in sources :

Example 76 with SimpleGrantedAuthority

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

the class UserTests method testNullWithinGrantedAuthorityElementIsRejected.

@Test
public void testNullWithinGrantedAuthorityElementIsRejected() throws Exception {
    try {
        List<GrantedAuthority> auths = AuthorityUtils.createAuthorityList("ROLE_ONE");
        auths.add(null);
        auths.add(new SimpleGrantedAuthority("ROLE_THREE"));
        new User(null, "koala", true, true, true, true, auths);
        fail("Should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
}
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 77 with SimpleGrantedAuthority

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

the class UsernamePasswordAuthenticationTokenMixinTests method deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithUserTest.

@Test
public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithUserTest() throws IOException {
    UsernamePasswordAuthenticationToken token = mapper.readValue(AUTHENTICATED_JSON, UsernamePasswordAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
    assertThat(((User) token.getPrincipal()).getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(token.isAuthenticated()).isEqualTo(true);
    assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 78 with SimpleGrantedAuthority

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

the class AnonymousAuthenticationTokenMixinTests method deserializeAnonymousAuthenticationTokenTest.

@Test
public void deserializeAnonymousAuthenticationTokenTest() throws IOException {
    AnonymousAuthenticationToken token = mapper.readValue(ANONYMOUS_JSON, AnonymousAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getKeyHash()).isEqualTo(HASH_KEY.hashCode());
    assertThat(token.getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Test(org.junit.Test)

Example 79 with SimpleGrantedAuthority

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

the class RememberMeAuthenticationTokenMixinTests method deserializeRememberMeAuthenticationTokenWithUserTest.

@Test
public void deserializeRememberMeAuthenticationTokenWithUserTest() throws IOException {
    RememberMeAuthenticationToken token = mapper.readValue(String.format(REMEMBERME_AUTH_JSON, "\"password\""), RememberMeAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
    assertThat(((User) token.getPrincipal()).getUsername()).isEqualTo("admin");
    assertThat(((User) token.getPrincipal()).getPassword()).isEqualTo("1234");
    assertThat(((User) token.getPrincipal()).getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
    assertThat(((User) token.getPrincipal()).isEnabled()).isEqualTo(true);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

Example 80 with SimpleGrantedAuthority

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

the class RememberMeAuthenticationTokenMixinTests method serializeRememberMeAuthenticationToken.

@Test
public void serializeRememberMeAuthenticationToken() throws JsonProcessingException, JSONException {
    RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, "admin", Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));
    String actualJson = mapper.writeValueAsString(token);
    JSONAssert.assertEquals(REMEMBERME_AUTH_STRINGPRINCIPAL_JSON, actualJson, true);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) Test(org.junit.Test)

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