use of org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication 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);
}
use of org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication 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();
}
use of org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication 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);
}
use of org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication in project spring-security by spring-projects.
the class BearerTokenAuthenticationTests method toStringWhenAttributesContainsURLThenDoesNotFail.
// gh-6843
@Test
public void toStringWhenAttributesContainsURLThenDoesNotFail() throws Exception {
JSONObject attributes = new JSONObject(Collections.singletonMap("iss", new URL("https://idp.example.com")));
OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal(attributes, null);
BearerTokenAuthentication token = new BearerTokenAuthentication(principal, this.token, null);
token.toString();
}
use of org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication in project spring-security by spring-projects.
the class JwtBearerTokenAuthenticationConverterTests method convertWhenJwtThenBearerTokenAuthentication.
@Test
public void convertWhenJwtThenBearerTokenAuthentication() {
// @formatter:off
Jwt jwt = Jwt.withTokenValue("token-value").claim("claim", "value").header("header", "value").build();
// @formatter:on
AbstractAuthenticationToken token = this.converter.convert(jwt);
assertThat(token).isInstanceOf(BearerTokenAuthentication.class);
BearerTokenAuthentication bearerToken = (BearerTokenAuthentication) token;
assertThat(bearerToken.getToken().getTokenValue()).isEqualTo("token-value");
assertThat(bearerToken.getTokenAttributes()).containsOnlyKeys("claim");
assertThat(bearerToken.getAuthorities()).isEmpty();
}
Aggregations