Search in sources :

Example 1 with DefaultOAuth2AuthenticatedPrincipal

use of org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal in project spring-security by spring-projects.

the class BearerTokenAuthenticationTests method constructorWhenPassingAllAttributesThenTokenIsAuthenticated.

@Test
public void constructorWhenPassingAllAttributesThenTokenIsAuthenticated() {
    OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal("harris", Collections.singletonMap("claim", "value"), null);
    BearerTokenAuthentication authenticated = new BearerTokenAuthentication(principal, this.token, null);
    assertThat(authenticated.isAuthenticated()).isTrue();
}
Also used : OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultOAuth2AuthenticatedPrincipal

use of org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal in project spring-security by spring-projects.

the class JwtBearerTokenAuthenticationConverter method convert.

@Override
public AbstractAuthenticationToken convert(Jwt jwt) {
    OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt());
    Map<String, Object> attributes = jwt.getClaims();
    AbstractAuthenticationToken token = this.jwtAuthenticationConverter.convert(jwt);
    Collection<GrantedAuthority> authorities = token.getAuthorities();
    OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal(attributes, authorities);
    return new BearerTokenAuthentication(principal, accessToken, authorities);
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal)

Example 3 with DefaultOAuth2AuthenticatedPrincipal

use of org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal in project spring-security by spring-projects.

the class TestBearerTokenAuthentications method bearer.

public static BearerTokenAuthentication bearer() {
    Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("SCOPE_USER");
    OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal(Collections.singletonMap("sub", "user"), authorities);
    OAuth2AccessToken token = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "token", Instant.now(), Instant.now().plusSeconds(86400), new HashSet<>(Arrays.asList("USER")));
    return new BearerTokenAuthentication(principal, token, authorities);
}
Also used : OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal)

Example 4 with DefaultOAuth2AuthenticatedPrincipal

use of org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal in project spring-security by spring-projects.

the class BearerTokenAuthenticationTests method getNameWhenHasNoSubjectThenReturnsNull.

@Test
public void getNameWhenHasNoSubjectThenReturnsNull() {
    OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal(Collections.singletonMap("claim", "value"), null);
    BearerTokenAuthentication authenticated = new BearerTokenAuthentication(principal, this.token, null);
    assertThat(authenticated.getName()).isNull();
}
Also used : OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultOAuth2AuthenticatedPrincipal

use of org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal in project spring-security by spring-projects.

the class BearerTokenAuthenticationTests method constructorWhenDefaultParametersThenSetsPrincipalToAttributesCopy.

// gh-6843
@Test
public void constructorWhenDefaultParametersThenSetsPrincipalToAttributesCopy() {
    JSONObject attributes = new JSONObject();
    attributes.put("active", true);
    OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal(attributes, null);
    BearerTokenAuthentication token = new BearerTokenAuthentication(principal, this.token, null);
    assertThat(token.getPrincipal()).isNotSameAs(attributes);
    assertThat(token.getTokenAttributes()).isNotSameAs(attributes);
}
Also used : JSONObject(net.minidev.json.JSONObject) OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultOAuth2AuthenticatedPrincipal (org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal)8 OAuth2AuthenticatedPrincipal (org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal)7 Test (org.junit.jupiter.api.Test)5 JSONObject (net.minidev.json.JSONObject)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)2 URL (java.net.URL)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)1