Remove restricted static imports
Replace static imports with class referenced methods. With the exception of a few well known static imports, checkstyle restricts the static imports that a class can use. For example, `asList(...)` would be replaced with `Arrays.asList(...)`. Issue gh-8945
This commit is contained in:
@@ -18,10 +18,6 @@ package org.springframework.security.oauth2.server.resource;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes.INSUFFICIENT_SCOPE;
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes.INVALID_REQUEST;
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes.INVALID_TOKEN;
|
||||
|
||||
/**
|
||||
* A factory for creating {@link BearerTokenError} instances that correspond to the
|
||||
* registered <a href="https://tools.ietf.org/html/rfc6750#section-3.1">Bearer Token Error
|
||||
@@ -47,7 +43,8 @@ public final class BearerTokenErrors {
|
||||
*/
|
||||
public static BearerTokenError invalidRequest(String message) {
|
||||
try {
|
||||
return new BearerTokenError(INVALID_REQUEST, HttpStatus.BAD_REQUEST, message, DEFAULT_URI);
|
||||
return new BearerTokenError(BearerTokenErrorCodes.INVALID_REQUEST, HttpStatus.BAD_REQUEST, message,
|
||||
DEFAULT_URI);
|
||||
}
|
||||
catch (IllegalArgumentException malformed) {
|
||||
// some third-party library error messages are not suitable for RFC 6750's
|
||||
@@ -63,7 +60,8 @@ public final class BearerTokenErrors {
|
||||
*/
|
||||
public static BearerTokenError invalidToken(String message) {
|
||||
try {
|
||||
return new BearerTokenError(INVALID_TOKEN, HttpStatus.UNAUTHORIZED, message, DEFAULT_URI);
|
||||
return new BearerTokenError(BearerTokenErrorCodes.INVALID_TOKEN, HttpStatus.UNAUTHORIZED, message,
|
||||
DEFAULT_URI);
|
||||
}
|
||||
catch (IllegalArgumentException malformed) {
|
||||
// some third-party library error messages are not suitable for RFC 6750's
|
||||
@@ -79,7 +77,8 @@ public final class BearerTokenErrors {
|
||||
*/
|
||||
public static BearerTokenError insufficientScope(String message, String scope) {
|
||||
try {
|
||||
return new BearerTokenError(INSUFFICIENT_SCOPE, HttpStatus.FORBIDDEN, message, DEFAULT_URI, scope);
|
||||
return new BearerTokenError(BearerTokenErrorCodes.INSUFFICIENT_SCOPE, HttpStatus.FORBIDDEN, message,
|
||||
DEFAULT_URI, scope);
|
||||
}
|
||||
catch (IllegalArgumentException malformed) {
|
||||
// some third-party library error messages are not suitable for RFC 6750's
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.security.oauth2.server.resource;
|
||||
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrors.invalidToken;
|
||||
|
||||
/**
|
||||
* An {@link OAuth2AuthenticationException} that indicates an invalid bearer token.
|
||||
*
|
||||
@@ -38,7 +36,7 @@ public class InvalidBearerTokenException extends OAuth2AuthenticationException {
|
||||
* @param description the description
|
||||
*/
|
||||
public InvalidBearerTokenException(String description) {
|
||||
super(invalidToken(description));
|
||||
super(BearerTokenErrors.invalidToken(description));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +50,7 @@ public class InvalidBearerTokenException extends OAuth2AuthenticationException {
|
||||
* @param cause the causing exception
|
||||
*/
|
||||
public InvalidBearerTokenException(String description, Throwable cause) {
|
||||
super(invalidToken(description), cause);
|
||||
super(BearerTokenErrors.invalidToken(description), cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,13 +29,11 @@ import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.BadOpaqueTokenException;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionException;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUED_AT;
|
||||
|
||||
/**
|
||||
* An {@link AuthenticationProvider} implementation for opaque
|
||||
* <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer
|
||||
@@ -113,8 +111,8 @@ public final class OpaqueTokenAuthenticationProvider implements AuthenticationPr
|
||||
}
|
||||
|
||||
private AbstractAuthenticationToken convert(OAuth2AuthenticatedPrincipal principal, String token) {
|
||||
Instant iat = principal.getAttribute(ISSUED_AT);
|
||||
Instant exp = principal.getAttribute(EXPIRES_AT);
|
||||
Instant iat = principal.getAttribute(OAuth2IntrospectionClaimNames.ISSUED_AT);
|
||||
Instant exp = principal.getAttribute(OAuth2IntrospectionClaimNames.EXPIRES_AT);
|
||||
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, token, iat, exp);
|
||||
return new BearerTokenAuthentication(principal, accessToken, principal.getAuthorities());
|
||||
}
|
||||
|
||||
@@ -30,13 +30,11 @@ import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.BadOpaqueTokenException;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionException;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUED_AT;
|
||||
|
||||
/**
|
||||
* An {@link ReactiveAuthenticationManager} implementation for opaque
|
||||
* <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer
|
||||
@@ -84,8 +82,8 @@ public class OpaqueTokenReactiveAuthenticationManager implements ReactiveAuthent
|
||||
|
||||
private Mono<BearerTokenAuthentication> authenticate(String token) {
|
||||
return this.introspector.introspect(token).map(principal -> {
|
||||
Instant iat = principal.getAttribute(ISSUED_AT);
|
||||
Instant exp = principal.getAttribute(EXPIRES_AT);
|
||||
Instant iat = principal.getAttribute(OAuth2IntrospectionClaimNames.ISSUED_AT);
|
||||
Instant exp = principal.getAttribute(OAuth2IntrospectionClaimNames.EXPIRES_AT);
|
||||
|
||||
// construct token
|
||||
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, token, iat, exp);
|
||||
|
||||
@@ -46,14 +46,6 @@ import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestOperations;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.CLIENT_ID;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUED_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUER;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.NOT_BEFORE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
||||
|
||||
/**
|
||||
* A Nimbus implementation of {@link OpaqueTokenIntrospector} that verifies and
|
||||
* introspects a token using the configured
|
||||
@@ -205,28 +197,28 @@ public class NimbusOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
|
||||
for (Audience audience : response.getAudience()) {
|
||||
audiences.add(audience.getValue());
|
||||
}
|
||||
claims.put(AUDIENCE, Collections.unmodifiableList(audiences));
|
||||
claims.put(OAuth2IntrospectionClaimNames.AUDIENCE, Collections.unmodifiableList(audiences));
|
||||
}
|
||||
if (response.getClientID() != null) {
|
||||
claims.put(CLIENT_ID, response.getClientID().getValue());
|
||||
claims.put(OAuth2IntrospectionClaimNames.CLIENT_ID, response.getClientID().getValue());
|
||||
}
|
||||
if (response.getExpirationTime() != null) {
|
||||
Instant exp = response.getExpirationTime().toInstant();
|
||||
claims.put(EXPIRES_AT, exp);
|
||||
claims.put(OAuth2IntrospectionClaimNames.EXPIRES_AT, exp);
|
||||
}
|
||||
if (response.getIssueTime() != null) {
|
||||
Instant iat = response.getIssueTime().toInstant();
|
||||
claims.put(ISSUED_AT, iat);
|
||||
claims.put(OAuth2IntrospectionClaimNames.ISSUED_AT, iat);
|
||||
}
|
||||
if (response.getIssuer() != null) {
|
||||
claims.put(ISSUER, issuer(response.getIssuer().getValue()));
|
||||
claims.put(OAuth2IntrospectionClaimNames.ISSUER, issuer(response.getIssuer().getValue()));
|
||||
}
|
||||
if (response.getNotBeforeTime() != null) {
|
||||
claims.put(NOT_BEFORE, response.getNotBeforeTime().toInstant());
|
||||
claims.put(OAuth2IntrospectionClaimNames.NOT_BEFORE, response.getNotBeforeTime().toInstant());
|
||||
}
|
||||
if (response.getScope() != null) {
|
||||
List<String> scopes = Collections.unmodifiableList(response.getScope().toStringList());
|
||||
claims.put(SCOPE, scopes);
|
||||
claims.put(OAuth2IntrospectionClaimNames.SCOPE, scopes);
|
||||
|
||||
for (String scope : scopes) {
|
||||
authorities.add(new SimpleGrantedAuthority(this.authorityPrefix + scope));
|
||||
@@ -241,7 +233,8 @@ public class NimbusOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
|
||||
return new URL(uri);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new OAuth2IntrospectionException("Invalid " + ISSUER + " value: " + uri);
|
||||
throw new OAuth2IntrospectionException(
|
||||
"Invalid " + OAuth2IntrospectionClaimNames.ISSUER + " value: " + uri);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,14 +43,6 @@ import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.CLIENT_ID;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUED_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUER;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.NOT_BEFORE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
||||
|
||||
/**
|
||||
* A Nimbus implementation of {@link ReactiveOpaqueTokenIntrospector} that verifies and
|
||||
* introspects a token using the configured
|
||||
@@ -158,28 +150,28 @@ public class NimbusReactiveOpaqueTokenIntrospector implements ReactiveOpaqueToke
|
||||
for (Audience audience : response.getAudience()) {
|
||||
audiences.add(audience.getValue());
|
||||
}
|
||||
claims.put(AUDIENCE, Collections.unmodifiableList(audiences));
|
||||
claims.put(OAuth2IntrospectionClaimNames.AUDIENCE, Collections.unmodifiableList(audiences));
|
||||
}
|
||||
if (response.getClientID() != null) {
|
||||
claims.put(CLIENT_ID, response.getClientID().getValue());
|
||||
claims.put(OAuth2IntrospectionClaimNames.CLIENT_ID, response.getClientID().getValue());
|
||||
}
|
||||
if (response.getExpirationTime() != null) {
|
||||
Instant exp = response.getExpirationTime().toInstant();
|
||||
claims.put(EXPIRES_AT, exp);
|
||||
claims.put(OAuth2IntrospectionClaimNames.EXPIRES_AT, exp);
|
||||
}
|
||||
if (response.getIssueTime() != null) {
|
||||
Instant iat = response.getIssueTime().toInstant();
|
||||
claims.put(ISSUED_AT, iat);
|
||||
claims.put(OAuth2IntrospectionClaimNames.ISSUED_AT, iat);
|
||||
}
|
||||
if (response.getIssuer() != null) {
|
||||
claims.put(ISSUER, issuer(response.getIssuer().getValue()));
|
||||
claims.put(OAuth2IntrospectionClaimNames.ISSUER, issuer(response.getIssuer().getValue()));
|
||||
}
|
||||
if (response.getNotBeforeTime() != null) {
|
||||
claims.put(NOT_BEFORE, response.getNotBeforeTime().toInstant());
|
||||
claims.put(OAuth2IntrospectionClaimNames.NOT_BEFORE, response.getNotBeforeTime().toInstant());
|
||||
}
|
||||
if (response.getScope() != null) {
|
||||
List<String> scopes = Collections.unmodifiableList(response.getScope().toStringList());
|
||||
claims.put(SCOPE, scopes);
|
||||
claims.put(OAuth2IntrospectionClaimNames.SCOPE, scopes);
|
||||
|
||||
for (String scope : scopes) {
|
||||
authorities.add(new SimpleGrantedAuthority(this.authorityPrefix + scope));
|
||||
@@ -194,7 +186,8 @@ public class NimbusReactiveOpaqueTokenIntrospector implements ReactiveOpaqueToke
|
||||
return new URL(uri);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new OAuth2IntrospectionException("Invalid " + ISSUER + " value: " + uri);
|
||||
throw new OAuth2IntrospectionException(
|
||||
"Invalid " + OAuth2IntrospectionClaimNames.ISSUER + " value: " + uri);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,9 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenError;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenErrors;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrors.invalidRequest;
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrors.invalidToken;
|
||||
|
||||
/**
|
||||
* The default {@link BearerTokenResolver} implementation based on RFC 6750.
|
||||
*
|
||||
@@ -57,7 +55,8 @@ public final class DefaultBearerTokenResolver implements BearerTokenResolver {
|
||||
String parameterToken = resolveFromRequestParameters(request);
|
||||
if (authorizationHeaderToken != null) {
|
||||
if (parameterToken != null) {
|
||||
BearerTokenError error = invalidRequest("Found multiple bearer tokens in the request");
|
||||
BearerTokenError error = BearerTokenErrors
|
||||
.invalidRequest("Found multiple bearer tokens in the request");
|
||||
throw new OAuth2AuthenticationException(error);
|
||||
}
|
||||
return authorizationHeaderToken;
|
||||
@@ -109,7 +108,7 @@ public final class DefaultBearerTokenResolver implements BearerTokenResolver {
|
||||
Matcher matcher = authorizationPattern.matcher(authorization);
|
||||
|
||||
if (!matcher.matches()) {
|
||||
BearerTokenError error = invalidToken("Bearer token is malformed");
|
||||
BearerTokenError error = BearerTokenErrors.invalidToken("Bearer token is malformed");
|
||||
throw new OAuth2AuthenticationException(error);
|
||||
}
|
||||
|
||||
@@ -128,7 +127,7 @@ public final class DefaultBearerTokenResolver implements BearerTokenResolver {
|
||||
return values[0];
|
||||
}
|
||||
|
||||
BearerTokenError error = invalidRequest("Found multiple bearer tokens in the request");
|
||||
BearerTokenError error = BearerTokenErrors.invalidRequest("Found multiple bearer tokens in the request");
|
||||
throw new OAuth2AuthenticationException(error);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,13 +28,11 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenError;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenErrors;
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrors.invalidRequest;
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrors.invalidToken;
|
||||
|
||||
/**
|
||||
* A strategy for resolving
|
||||
* <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer
|
||||
@@ -70,7 +68,8 @@ public class ServerBearerTokenAuthenticationConverter implements ServerAuthentic
|
||||
String parameterToken = request.getQueryParams().getFirst("access_token");
|
||||
if (authorizationHeaderToken != null) {
|
||||
if (parameterToken != null) {
|
||||
BearerTokenError error = invalidRequest("Found multiple bearer tokens in the request");
|
||||
BearerTokenError error = BearerTokenErrors
|
||||
.invalidRequest("Found multiple bearer tokens in the request");
|
||||
throw new OAuth2AuthenticationException(error);
|
||||
}
|
||||
return authorizationHeaderToken;
|
||||
@@ -122,7 +121,7 @@ public class ServerBearerTokenAuthenticationConverter implements ServerAuthentic
|
||||
}
|
||||
|
||||
private static BearerTokenError invalidTokenError() {
|
||||
return invalidToken("Bearer token is malformed");
|
||||
return BearerTokenErrors.invalidToken("Bearer token is malformed");
|
||||
}
|
||||
|
||||
private boolean isParameterTokenSupportedForRequest(ServerHttpRequest request) {
|
||||
|
||||
@@ -18,13 +18,9 @@ package org.springframework.security.oauth2.server.resource;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.FORBIDDEN;
|
||||
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes.INSUFFICIENT_SCOPE;
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes.INVALID_REQUEST;
|
||||
import static org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes.INVALID_TOKEN;
|
||||
|
||||
public class BearerTokenErrorsTests {
|
||||
|
||||
@@ -32,9 +28,9 @@ public class BearerTokenErrorsTests {
|
||||
public void invalidRequestWhenMessageGivenThenBearerTokenErrorReturned() {
|
||||
String message = "message";
|
||||
BearerTokenError error = BearerTokenErrors.invalidRequest(message);
|
||||
assertThat(error.getErrorCode()).isSameAs(INVALID_REQUEST);
|
||||
assertThat(error.getErrorCode()).isSameAs(BearerTokenErrorCodes.INVALID_REQUEST);
|
||||
assertThat(error.getDescription()).isSameAs(message);
|
||||
assertThat(error.getHttpStatus()).isSameAs(BAD_REQUEST);
|
||||
assertThat(error.getHttpStatus()).isSameAs(HttpStatus.BAD_REQUEST);
|
||||
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
||||
}
|
||||
|
||||
@@ -42,9 +38,9 @@ public class BearerTokenErrorsTests {
|
||||
public void invalidRequestWhenInvalidMessageGivenThenDefaultBearerTokenErrorReturned() {
|
||||
String message = "has \"invalid\" chars";
|
||||
BearerTokenError error = BearerTokenErrors.invalidRequest(message);
|
||||
assertThat(error.getErrorCode()).isSameAs(INVALID_REQUEST);
|
||||
assertThat(error.getErrorCode()).isSameAs(BearerTokenErrorCodes.INVALID_REQUEST);
|
||||
assertThat(error.getDescription()).isEqualTo("Invalid request");
|
||||
assertThat(error.getHttpStatus()).isSameAs(BAD_REQUEST);
|
||||
assertThat(error.getHttpStatus()).isSameAs(HttpStatus.BAD_REQUEST);
|
||||
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
||||
}
|
||||
|
||||
@@ -52,9 +48,9 @@ public class BearerTokenErrorsTests {
|
||||
public void invalidTokenWhenMessageGivenThenBearerTokenErrorReturned() {
|
||||
String message = "message";
|
||||
BearerTokenError error = BearerTokenErrors.invalidToken(message);
|
||||
assertThat(error.getErrorCode()).isSameAs(INVALID_TOKEN);
|
||||
assertThat(error.getErrorCode()).isSameAs(BearerTokenErrorCodes.INVALID_TOKEN);
|
||||
assertThat(error.getDescription()).isSameAs(message);
|
||||
assertThat(error.getHttpStatus()).isSameAs(UNAUTHORIZED);
|
||||
assertThat(error.getHttpStatus()).isSameAs(HttpStatus.UNAUTHORIZED);
|
||||
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
||||
}
|
||||
|
||||
@@ -62,9 +58,9 @@ public class BearerTokenErrorsTests {
|
||||
public void invalidTokenWhenInvalidMessageGivenThenDefaultBearerTokenErrorReturned() {
|
||||
String message = "has \"invalid\" chars";
|
||||
BearerTokenError error = BearerTokenErrors.invalidToken(message);
|
||||
assertThat(error.getErrorCode()).isSameAs(INVALID_TOKEN);
|
||||
assertThat(error.getErrorCode()).isSameAs(BearerTokenErrorCodes.INVALID_TOKEN);
|
||||
assertThat(error.getDescription()).isEqualTo("Invalid token");
|
||||
assertThat(error.getHttpStatus()).isSameAs(UNAUTHORIZED);
|
||||
assertThat(error.getHttpStatus()).isSameAs(HttpStatus.UNAUTHORIZED);
|
||||
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
||||
}
|
||||
|
||||
@@ -73,9 +69,9 @@ public class BearerTokenErrorsTests {
|
||||
String message = "message";
|
||||
String scope = "scope";
|
||||
BearerTokenError error = BearerTokenErrors.insufficientScope(message, scope);
|
||||
assertThat(error.getErrorCode()).isSameAs(INSUFFICIENT_SCOPE);
|
||||
assertThat(error.getErrorCode()).isSameAs(BearerTokenErrorCodes.INSUFFICIENT_SCOPE);
|
||||
assertThat(error.getDescription()).isSameAs(message);
|
||||
assertThat(error.getHttpStatus()).isSameAs(FORBIDDEN);
|
||||
assertThat(error.getHttpStatus()).isSameAs(HttpStatus.FORBIDDEN);
|
||||
assertThat(error.getScope()).isSameAs(scope);
|
||||
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
||||
}
|
||||
@@ -84,9 +80,9 @@ public class BearerTokenErrorsTests {
|
||||
public void insufficientScopeWhenInvalidMessageGivenThenDefaultBearerTokenErrorReturned() {
|
||||
String message = "has \"invalid\" chars";
|
||||
BearerTokenError error = BearerTokenErrors.insufficientScope(message, "scope");
|
||||
assertThat(error.getErrorCode()).isSameAs(INSUFFICIENT_SCOPE);
|
||||
assertThat(error.getErrorCode()).isSameAs(BearerTokenErrorCodes.INSUFFICIENT_SCOPE);
|
||||
assertThat(error.getDescription()).isSameAs("Insufficient scope");
|
||||
assertThat(error.getHttpStatus()).isSameAs(FORBIDDEN);
|
||||
assertThat(error.getHttpStatus()).isSameAs(HttpStatus.FORBIDDEN);
|
||||
assertThat(error.getScope()).isNull();
|
||||
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
||||
}
|
||||
|
||||
@@ -22,13 +22,13 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
|
||||
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultAuthenticationEventPublisher}'s bearer token use cases
|
||||
@@ -42,7 +42,7 @@ public class DefaultAuthenticationEventPublisherBearerTokenTests {
|
||||
@Test
|
||||
public void publishAuthenticationFailureWhenInvalidBearerTokenExceptionThenMaps() {
|
||||
ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
|
||||
Authentication authentication = new JwtAuthenticationToken(jwt().build());
|
||||
Authentication authentication = new JwtAuthenticationToken(TestJwts.jwt().build());
|
||||
Exception cause = new Exception();
|
||||
this.publisher = new DefaultAuthenticationEventPublisher(appPublisher);
|
||||
this.publisher.publishAuthenticationFailure(new InvalidBearerTokenException("invalid"), authentication);
|
||||
|
||||
@@ -33,12 +33,10 @@ import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal;
|
||||
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.CLIENT_ID;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SUBJECT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.USERNAME;
|
||||
|
||||
/**
|
||||
* Tests for {@link BearerTokenAuthentication}
|
||||
@@ -60,9 +58,9 @@ public class BearerTokenAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.attributesMap.put(SUBJECT, this.name);
|
||||
this.attributesMap.put(CLIENT_ID, "client_id");
|
||||
this.attributesMap.put(USERNAME, "username");
|
||||
this.attributesMap.put(OAuth2IntrospectionClaimNames.SUBJECT, this.name);
|
||||
this.attributesMap.put(OAuth2IntrospectionClaimNames.CLIENT_ID, "client_id");
|
||||
this.attributesMap.put(OAuth2IntrospectionClaimNames.USERNAME, "username");
|
||||
this.principal = new DefaultOAuth2AuthenticatedPrincipal(this.attributesMap, null);
|
||||
}
|
||||
|
||||
@@ -86,7 +84,8 @@ public class BearerTokenAuthenticationTests {
|
||||
@Test
|
||||
public void getNameWhenTokenHasUsernameThenReturnsUsernameAttribute() {
|
||||
BearerTokenAuthentication authenticated = new BearerTokenAuthentication(this.principal, this.token, null);
|
||||
assertThat(authenticated.getName()).isEqualTo(this.principal.getAttribute(SUBJECT));
|
||||
assertThat(authenticated.getName())
|
||||
.isEqualTo(this.principal.getAttribute(OAuth2IntrospectionClaimNames.SUBJECT));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -26,10 +26,10 @@ import org.springframework.security.authentication.AbstractAuthenticationToken;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* Tests for {@link JwtAuthenticationConverter}
|
||||
@@ -43,7 +43,7 @@ public class JwtAuthenticationConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenDefaultGrantedAuthoritiesConverterSet() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt);
|
||||
Collection<GrantedAuthority> authorities = authentication.getAuthorities();
|
||||
@@ -61,7 +61,7 @@ public class JwtAuthenticationConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWithOverriddenGrantedAuthoritiesConverter() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
Converter<Jwt, Collection<GrantedAuthority>> grantedAuthoritiesConverter = token -> Arrays
|
||||
.asList(new SimpleGrantedAuthority("blah"));
|
||||
@@ -98,7 +98,7 @@ public class JwtAuthenticationConverterTests {
|
||||
public void convertWhenPrincipalClaimNameSet() {
|
||||
this.jwtAuthenticationConverter.setPrincipalClaimName("user_id");
|
||||
|
||||
Jwt jwt = jwt().claim("user_id", "100").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("user_id", "100").build();
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt);
|
||||
|
||||
assertThat(authentication.getName()).isEqualTo("100");
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.security.oauth2.jwt.BadJwtException;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.JwtDecoder;
|
||||
import org.springframework.security.oauth2.jwt.JwtException;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes;
|
||||
|
||||
@@ -37,7 +38,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* Tests for {@link JwtAuthenticationProvider}
|
||||
@@ -65,7 +65,7 @@ public class JwtAuthenticationProviderTests {
|
||||
public void authenticateWhenJwtDecodesThenAuthenticationHasAttributesContainedInJwt() {
|
||||
BearerTokenAuthenticationToken token = this.authentication();
|
||||
|
||||
Jwt jwt = jwt().claim("name", "value").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("name", "value").build();
|
||||
|
||||
given(this.jwtDecoder.decode("token")).willReturn(jwt);
|
||||
given(this.jwtAuthenticationConverter.convert(jwt)).willReturn(new JwtAuthenticationToken(jwt));
|
||||
@@ -113,7 +113,7 @@ public class JwtAuthenticationProviderTests {
|
||||
Object details = mock(Object.class);
|
||||
token.setDetails(details);
|
||||
|
||||
Jwt jwt = jwt().build();
|
||||
Jwt jwt = TestJwts.jwt().build();
|
||||
JwtAuthenticationToken authentication = new JwtAuthenticationToken(jwt);
|
||||
|
||||
given(this.jwtDecoder.decode(token.getToken())).willReturn(jwt);
|
||||
|
||||
@@ -24,11 +24,11 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.springframework.security.oauth2.jose.jws.JwsAlgorithms.RS256;
|
||||
|
||||
/**
|
||||
* Tests for {@link JwtAuthenticationToken}
|
||||
@@ -124,7 +124,7 @@ public class JwtAuthenticationTokenTests {
|
||||
}
|
||||
|
||||
private Jwt.Builder builder() {
|
||||
return Jwt.withTokenValue("token").header("alg", RS256);
|
||||
return Jwt.withTokenValue("token").header("alg", JwsAlgorithms.RS256);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@ import org.junit.Test;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* Tests for {@link JwtGrantedAuthoritiesConverter}
|
||||
@@ -45,7 +45,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -56,7 +56,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
||||
@@ -68,7 +68,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
|
||||
@@ -80,7 +80,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", "").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -90,7 +90,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -101,7 +101,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
||||
@@ -113,7 +113,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", "message:read message:write").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
|
||||
@@ -125,7 +125,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Collections.emptyList()).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Collections.emptyList()).build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -135,7 +135,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write"))
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write"))
|
||||
.claim("scope", "missive:read missive:write").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
@@ -147,7 +147,8 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write")).claim("scope", "").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).claim("scope", "")
|
||||
.build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -157,7 +158,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyScopeAndEmptyScpAttributeThenTranslatesToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Collections.emptyList()).claim("scope", Collections.emptyList()).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Collections.emptyList()).claim("scope", Collections.emptyList()).build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -167,7 +168,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasNoScopeAndNoScpAttributeThenTranslatesToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("roles", Arrays.asList("message:read", "message:write")).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("roles", Arrays.asList("message:read", "message:write")).build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -177,7 +178,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasUnsupportedTypeForScopeThenTranslatesToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", new String[] { "message:read", "message:write" }).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", new String[] { "message:read", "message:write" }).build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
||||
@@ -187,7 +188,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("roles", Arrays.asList("message:read", "message:write"))
|
||||
Jwt jwt = TestJwts.jwt().claim("roles", Arrays.asList("message:read", "message:write"))
|
||||
.claim("scope", "missive:read missive:write").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
@@ -200,7 +201,8 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("roles", Collections.emptyList()).claim("scope", "missive:read missive:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("roles", Collections.emptyList()).claim("scope", "missive:read missive:write")
|
||||
.build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
||||
@@ -211,7 +213,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", "missive:read missive:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "missive:read missive:write").build();
|
||||
|
||||
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
||||
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
||||
|
||||
@@ -38,11 +38,11 @@ import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.AuthenticationManagerResolver;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
||||
import org.springframework.security.oauth2.jose.TestKeys;
|
||||
import org.springframework.security.oauth2.jwt.JwtClaimNames;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.security.oauth2.jwt.JwtClaimNames.ISS;
|
||||
|
||||
/**
|
||||
* Tests for {@link JwtIssuerAuthenticationManagerResolver}
|
||||
@@ -66,7 +66,7 @@ public class JwtIssuerAuthenticationManagerResolverTests {
|
||||
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json")
|
||||
.setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)));
|
||||
JWSObject jws = new JWSObject(new JWSHeader(JWSAlgorithm.RS256),
|
||||
new Payload(new JSONObject(Collections.singletonMap(ISS, issuer))));
|
||||
new Payload(new JSONObject(Collections.singletonMap(JwtClaimNames.ISS, issuer))));
|
||||
jws.sign(new RSASSASigner(TestKeys.DEFAULT_PRIVATE_KEY));
|
||||
|
||||
JwtIssuerAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerAuthenticationManagerResolver(
|
||||
|
||||
@@ -40,11 +40,11 @@ import org.springframework.security.authentication.ReactiveAuthenticationManager
|
||||
import org.springframework.security.authentication.ReactiveAuthenticationManagerResolver;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
||||
import org.springframework.security.oauth2.jose.TestKeys;
|
||||
import org.springframework.security.oauth2.jwt.JwtClaimNames;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.security.oauth2.jwt.JwtClaimNames.ISS;
|
||||
|
||||
/**
|
||||
* Tests for {@link JwtIssuerReactiveAuthenticationManagerResolver}
|
||||
@@ -67,7 +67,7 @@ public class JwtIssuerReactiveAuthenticationManagerResolverTests {
|
||||
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json")
|
||||
.setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)));
|
||||
JWSObject jws = new JWSObject(new JWSHeader(JWSAlgorithm.RS256),
|
||||
new Payload(new JSONObject(Collections.singletonMap(ISS, issuer))));
|
||||
new Payload(new JSONObject(Collections.singletonMap(JwtClaimNames.ISS, issuer))));
|
||||
jws.sign(new RSASSASigner(TestKeys.DEFAULT_PRIVATE_KEY));
|
||||
|
||||
JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerReactiveAuthenticationManagerResolver(
|
||||
|
||||
@@ -32,13 +32,13 @@ import org.springframework.security.oauth2.jwt.BadJwtException;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.JwtException;
|
||||
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
@@ -57,7 +57,7 @@ public class JwtReactiveAuthenticationManagerTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.manager = new JwtReactiveAuthenticationManager(this.jwtDecoder);
|
||||
this.jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
this.jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.Test;
|
||||
import org.springframework.security.authentication.AuthenticationServiceException;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
|
||||
import org.springframework.security.oauth2.core.TestOAuth2AuthenticatedPrincipals;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionAuthenticatedPrincipal;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames;
|
||||
@@ -37,15 +38,6 @@ import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.security.oauth2.core.TestOAuth2AuthenticatedPrincipals.active;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ACTIVE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUER;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.NOT_BEFORE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SUBJECT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.USERNAME;
|
||||
|
||||
/**
|
||||
* Tests for {@link OpaqueTokenAuthenticationProvider}
|
||||
@@ -56,8 +48,8 @@ public class OpaqueTokenAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void authenticateWhenActiveTokenThenOk() throws Exception {
|
||||
OAuth2AuthenticatedPrincipal principal = active(
|
||||
attributes -> attributes.put("extension_field", "twenty-seven"));
|
||||
OAuth2AuthenticatedPrincipal principal = TestOAuth2AuthenticatedPrincipals
|
||||
.active(attributes -> attributes.put("extension_field", "twenty-seven"));
|
||||
OpaqueTokenIntrospector introspector = mock(OpaqueTokenIntrospector.class);
|
||||
given(introspector.introspect(any())).willReturn(principal);
|
||||
OpaqueTokenAuthenticationProvider provider = new OpaqueTokenAuthenticationProvider(introspector);
|
||||
@@ -67,14 +59,16 @@ public class OpaqueTokenAuthenticationProviderTests {
|
||||
assertThat(result.getPrincipal()).isInstanceOf(OAuth2IntrospectionAuthenticatedPrincipal.class);
|
||||
|
||||
Map<String, Object> attributes = ((OAuth2AuthenticatedPrincipal) result.getPrincipal()).getAttributes();
|
||||
assertThat(attributes).isNotNull().containsEntry(ACTIVE, true)
|
||||
.containsEntry(AUDIENCE, Arrays.asList("https://protected.example.net/resource"))
|
||||
assertThat(attributes).isNotNull().containsEntry(OAuth2IntrospectionClaimNames.ACTIVE, true)
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.AUDIENCE,
|
||||
Arrays.asList("https://protected.example.net/resource"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.CLIENT_ID, "l238j323ds-23ij4")
|
||||
.containsEntry(EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.containsEntry(SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(SUBJECT, "Z5O3upPC88QrAjx00dis").containsEntry(USERNAME, "jdoe")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SUBJECT, "Z5O3upPC88QrAjx00dis")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.USERNAME, "jdoe")
|
||||
.containsEntry("extension_field", "twenty-seven");
|
||||
|
||||
assertThat(result.getAuthorities()).extracting("authority").containsExactly("SCOPE_read", "SCOPE_write",
|
||||
@@ -93,7 +87,7 @@ public class OpaqueTokenAuthenticationProviderTests {
|
||||
assertThat(result.getPrincipal()).isInstanceOf(OAuth2AuthenticatedPrincipal.class);
|
||||
|
||||
Map<String, Object> attributes = ((OAuth2AuthenticatedPrincipal) result.getPrincipal()).getAttributes();
|
||||
assertThat(attributes).isNotNull().doesNotContainKey(SCOPE);
|
||||
assertThat(attributes).isNotNull().doesNotContainKey(OAuth2IntrospectionClaimNames.SCOPE);
|
||||
|
||||
assertThat(result.getAuthorities()).isEmpty();
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.security.authentication.AuthenticationServiceException;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
|
||||
import org.springframework.security.oauth2.core.TestOAuth2AuthenticatedPrincipals;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionAuthenticatedPrincipal;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames;
|
||||
@@ -39,15 +40,6 @@ import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.security.oauth2.core.TestOAuth2AuthenticatedPrincipals.active;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ACTIVE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUER;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.NOT_BEFORE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SUBJECT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.USERNAME;
|
||||
|
||||
/**
|
||||
* Tests for {@link OpaqueTokenReactiveAuthenticationManager}
|
||||
@@ -58,8 +50,8 @@ public class OpaqueTokenReactiveAuthenticationManagerTests {
|
||||
|
||||
@Test
|
||||
public void authenticateWhenActiveTokenThenOk() throws Exception {
|
||||
OAuth2AuthenticatedPrincipal authority = active(
|
||||
attributes -> attributes.put("extension_field", "twenty-seven"));
|
||||
OAuth2AuthenticatedPrincipal authority = TestOAuth2AuthenticatedPrincipals
|
||||
.active(attributes -> attributes.put("extension_field", "twenty-seven"));
|
||||
ReactiveOpaqueTokenIntrospector introspector = mock(ReactiveOpaqueTokenIntrospector.class);
|
||||
given(introspector.introspect(any())).willReturn(Mono.just(authority));
|
||||
OpaqueTokenReactiveAuthenticationManager provider = new OpaqueTokenReactiveAuthenticationManager(introspector);
|
||||
@@ -69,14 +61,16 @@ public class OpaqueTokenReactiveAuthenticationManagerTests {
|
||||
assertThat(result.getPrincipal()).isInstanceOf(OAuth2IntrospectionAuthenticatedPrincipal.class);
|
||||
|
||||
Map<String, Object> attributes = ((OAuth2AuthenticatedPrincipal) result.getPrincipal()).getAttributes();
|
||||
assertThat(attributes).isNotNull().containsEntry(ACTIVE, true)
|
||||
.containsEntry(AUDIENCE, Arrays.asList("https://protected.example.net/resource"))
|
||||
assertThat(attributes).isNotNull().containsEntry(OAuth2IntrospectionClaimNames.ACTIVE, true)
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.AUDIENCE,
|
||||
Arrays.asList("https://protected.example.net/resource"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.CLIENT_ID, "l238j323ds-23ij4")
|
||||
.containsEntry(EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.containsEntry(SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(SUBJECT, "Z5O3upPC88QrAjx00dis").containsEntry(USERNAME, "jdoe")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SUBJECT, "Z5O3upPC88QrAjx00dis")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.USERNAME, "jdoe")
|
||||
.containsEntry("extension_field", "twenty-seven");
|
||||
|
||||
assertThat(result.getAuthorities()).extracting("authority").containsExactly("SCOPE_read", "SCOPE_write",
|
||||
@@ -95,7 +89,7 @@ public class OpaqueTokenReactiveAuthenticationManagerTests {
|
||||
assertThat(result.getPrincipal()).isInstanceOf(OAuth2IntrospectionAuthenticatedPrincipal.class);
|
||||
|
||||
Map<String, Object> attributes = ((OAuth2AuthenticatedPrincipal) result.getPrincipal()).getAttributes();
|
||||
assertThat(attributes).isNotNull().doesNotContainKey(SCOPE);
|
||||
assertThat(attributes).isNotNull().doesNotContainKey(OAuth2IntrospectionClaimNames.SCOPE);
|
||||
|
||||
assertThat(result.getAuthorities()).isEmpty();
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ import org.springframework.security.authentication.AbstractAuthenticationToken;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveJwtAuthenticationConverterAdapter}
|
||||
@@ -44,7 +44,7 @@ public class ReactiveJwtAuthenticationConverterAdapterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
|
||||
Collection<GrantedAuthority> authorities = authentication.getAuthorities();
|
||||
@@ -55,7 +55,7 @@ public class ReactiveJwtAuthenticationConverterAdapterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scope", "").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "").build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
|
||||
|
||||
@@ -66,7 +66,7 @@ public class ReactiveJwtAuthenticationConverterAdapterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
|
||||
|
||||
@@ -78,7 +78,7 @@ public class ReactiveJwtAuthenticationConverterAdapterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList()).build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList()).build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
|
||||
|
||||
@@ -89,7 +89,7 @@ public class ReactiveJwtAuthenticationConverterAdapterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write"))
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write"))
|
||||
.claim("scope", "missive:read missive:write").build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
|
||||
@@ -102,7 +102,8 @@ public class ReactiveJwtAuthenticationConverterAdapterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities() {
|
||||
Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write")).claim("scope", "").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).claim("scope", "")
|
||||
.build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ import org.springframework.security.authentication.AbstractAuthenticationToken;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveJwtAuthenticationConverter}
|
||||
@@ -43,7 +43,7 @@ public class ReactiveJwtAuthenticationConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWhenDefaultGrantedAuthoritiesConverterSet() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
|
||||
Collection<GrantedAuthority> authorities = authentication.getAuthorities();
|
||||
@@ -61,7 +61,7 @@ public class ReactiveJwtAuthenticationConverterTests {
|
||||
|
||||
@Test
|
||||
public void convertWithOverriddenGrantedAuthoritiesConverter() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
Converter<Jwt, Flux<GrantedAuthority>> grantedAuthoritiesConverter = token -> Flux
|
||||
.just(new SimpleGrantedAuthority("blah"));
|
||||
|
||||
@@ -26,10 +26,10 @@ import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.jwt.TestJwts;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveJwtGrantedAuthoritiesConverterAdapter}
|
||||
@@ -41,7 +41,7 @@ public class ReactiveJwtGrantedAuthoritiesConverterAdapterTests {
|
||||
|
||||
@Test
|
||||
public void convertWithGrantedAuthoritiesConverter() {
|
||||
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
||||
Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
||||
|
||||
Converter<Jwt, Collection<GrantedAuthority>> grantedAuthoritiesConverter = token -> Arrays
|
||||
.asList(new SimpleGrantedAuthority("blah"));
|
||||
|
||||
@@ -50,13 +50,6 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUER;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.NOT_BEFORE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SUBJECT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.USERNAME;
|
||||
|
||||
/**
|
||||
* Tests for {@link NimbusOpaqueTokenIntrospector}
|
||||
@@ -116,12 +109,14 @@ public class NimbusOpaqueTokenIntrospectorTests {
|
||||
|
||||
OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token");
|
||||
assertThat(authority.getAttributes()).isNotNull().containsEntry(OAuth2IntrospectionClaimNames.ACTIVE, true)
|
||||
.containsEntry(AUDIENCE, Arrays.asList("https://protected.example.net/resource"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.AUDIENCE,
|
||||
Arrays.asList("https://protected.example.net/resource"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.CLIENT_ID, "l238j323ds-23ij4")
|
||||
.containsEntry(EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(SUBJECT, "Z5O3upPC88QrAjx00dis").containsEntry(USERNAME, "jdoe")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SUBJECT, "Z5O3upPC88QrAjx00dis")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.USERNAME, "jdoe")
|
||||
.containsEntry("extension_field", "twenty-seven");
|
||||
}
|
||||
}
|
||||
@@ -155,8 +150,8 @@ public class NimbusOpaqueTokenIntrospectorTests {
|
||||
public void introspectWhenActiveTokenThenParsesValuesInResponse() {
|
||||
Map<String, Object> introspectedValues = new HashMap<>();
|
||||
introspectedValues.put(OAuth2IntrospectionClaimNames.ACTIVE, true);
|
||||
introspectedValues.put(AUDIENCE, Arrays.asList("aud"));
|
||||
introspectedValues.put(NOT_BEFORE, 29348723984L);
|
||||
introspectedValues.put(OAuth2IntrospectionClaimNames.AUDIENCE, Arrays.asList("aud"));
|
||||
introspectedValues.put(OAuth2IntrospectionClaimNames.NOT_BEFORE, 29348723984L);
|
||||
|
||||
RestOperations restOperations = mock(RestOperations.class);
|
||||
OpaqueTokenIntrospector introspectionClient = new NimbusOpaqueTokenIntrospector(INTROSPECTION_URL,
|
||||
@@ -166,9 +161,10 @@ public class NimbusOpaqueTokenIntrospectorTests {
|
||||
|
||||
OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token");
|
||||
assertThat(authority.getAttributes()).isNotNull().containsEntry(OAuth2IntrospectionClaimNames.ACTIVE, true)
|
||||
.containsEntry(AUDIENCE, Arrays.asList("aud"))
|
||||
.containsEntry(NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.doesNotContainKey(OAuth2IntrospectionClaimNames.CLIENT_ID).doesNotContainKey(SCOPE);
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.AUDIENCE, Arrays.asList("aud"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.doesNotContainKey(OAuth2IntrospectionClaimNames.CLIENT_ID)
|
||||
.doesNotContainKey(OAuth2IntrospectionClaimNames.SCOPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -45,13 +45,6 @@ import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ISSUER;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.NOT_BEFORE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SUBJECT;
|
||||
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.USERNAME;
|
||||
|
||||
/**
|
||||
* Tests for {@link NimbusReactiveOpaqueTokenIntrospector}
|
||||
@@ -94,12 +87,14 @@ public class NimbusReactiveOpaqueTokenIntrospectorTests {
|
||||
|
||||
OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token").block();
|
||||
assertThat(authority.getAttributes()).isNotNull().containsEntry(OAuth2IntrospectionClaimNames.ACTIVE, true)
|
||||
.containsEntry(AUDIENCE, Arrays.asList("https://protected.example.net/resource"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.AUDIENCE,
|
||||
Arrays.asList("https://protected.example.net/resource"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.CLIENT_ID, "l238j323ds-23ij4")
|
||||
.containsEntry(EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(SUBJECT, "Z5O3upPC88QrAjx00dis").containsEntry(USERNAME, "jdoe")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.EXPIRES_AT, Instant.ofEpochSecond(1419356238))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.ISSUER, new URL("https://server.example.com/"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SCOPE, Arrays.asList("read", "write", "dolphin"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.SUBJECT, "Z5O3upPC88QrAjx00dis")
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.USERNAME, "jdoe")
|
||||
.containsEntry("extension_field", "twenty-seven");
|
||||
}
|
||||
}
|
||||
@@ -133,8 +128,8 @@ public class NimbusReactiveOpaqueTokenIntrospectorTests {
|
||||
public void authenticateWhenActiveTokenThenParsesValuesInResponse() {
|
||||
Map<String, Object> introspectedValues = new HashMap<>();
|
||||
introspectedValues.put(OAuth2IntrospectionClaimNames.ACTIVE, true);
|
||||
introspectedValues.put(AUDIENCE, Arrays.asList("aud"));
|
||||
introspectedValues.put(NOT_BEFORE, 29348723984L);
|
||||
introspectedValues.put(OAuth2IntrospectionClaimNames.AUDIENCE, Arrays.asList("aud"));
|
||||
introspectedValues.put(OAuth2IntrospectionClaimNames.NOT_BEFORE, 29348723984L);
|
||||
|
||||
WebClient webClient = mockResponse(new JSONObject(introspectedValues).toJSONString());
|
||||
NimbusReactiveOpaqueTokenIntrospector introspectionClient = new NimbusReactiveOpaqueTokenIntrospector(
|
||||
@@ -142,9 +137,10 @@ public class NimbusReactiveOpaqueTokenIntrospectorTests {
|
||||
|
||||
OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token").block();
|
||||
assertThat(authority.getAttributes()).isNotNull().containsEntry(OAuth2IntrospectionClaimNames.ACTIVE, true)
|
||||
.containsEntry(AUDIENCE, Arrays.asList("aud"))
|
||||
.containsEntry(NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.doesNotContainKey(OAuth2IntrospectionClaimNames.CLIENT_ID).doesNotContainKey(SCOPE);
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.AUDIENCE, Arrays.asList("aud"))
|
||||
.containsEntry(OAuth2IntrospectionClaimNames.NOT_BEFORE, Instant.ofEpochSecond(29348723984L))
|
||||
.doesNotContainKey(OAuth2IntrospectionClaimNames.CLIENT_ID)
|
||||
.doesNotContainKey(OAuth2IntrospectionClaimNames.SCOPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
||||
@@ -34,7 +35,6 @@ import org.springframework.security.oauth2.server.resource.web.MockExchangeFunct
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.http.HttpMethod.GET;
|
||||
|
||||
/**
|
||||
* Tests for {@link ServerBearerExchangeFilterFunction}
|
||||
@@ -60,7 +60,7 @@ public class ServerBearerExchangeFilterFunctionTests {
|
||||
|
||||
@Test
|
||||
public void filterWhenUnauthenticatedThenAuthorizationHeaderNull() {
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")).build();
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
|
||||
|
||||
this.function.filter(request, this.exchange).block();
|
||||
|
||||
@@ -69,7 +69,7 @@ public class ServerBearerExchangeFilterFunctionTests {
|
||||
|
||||
@Test
|
||||
public void filterWhenAuthenticatedThenAuthorizationHeaderNull() throws Exception {
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")).build();
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
|
||||
|
||||
this.function.filter(request, this.exchange)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(this.authentication)).block();
|
||||
@@ -81,7 +81,7 @@ public class ServerBearerExchangeFilterFunctionTests {
|
||||
// gh-7353
|
||||
@Test
|
||||
public void filterWhenAuthenticatedWithOtherTokenThenAuthorizationHeaderNull() throws Exception {
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")).build();
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
|
||||
|
||||
TestingAuthenticationToken token = new TestingAuthenticationToken("user", "pass");
|
||||
this.function.filter(request, this.exchange)
|
||||
@@ -92,7 +92,7 @@ public class ServerBearerExchangeFilterFunctionTests {
|
||||
|
||||
@Test
|
||||
public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() {
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com"))
|
||||
.header(HttpHeaders.AUTHORIZATION, "Existing").build();
|
||||
|
||||
this.function.filter(request, this.exchange)
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import reactor.util.context.Context;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
||||
@@ -37,8 +38,6 @@ import org.springframework.security.oauth2.server.resource.web.MockExchangeFunct
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.http.HttpMethod.GET;
|
||||
import static org.springframework.security.oauth2.server.resource.web.reactive.function.client.ServletBearerExchangeFilterFunction.SECURITY_REACTOR_CONTEXT_ATTRIBUTES_KEY;
|
||||
|
||||
/**
|
||||
* Tests for {@link ServletBearerExchangeFilterFunction}
|
||||
@@ -65,7 +64,7 @@ public class ServletBearerExchangeFilterFunctionTests {
|
||||
|
||||
@Test
|
||||
public void filterWhenUnauthenticatedThenAuthorizationHeaderNull() {
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")).build();
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
|
||||
|
||||
this.function.filter(request, this.exchange).block();
|
||||
|
||||
@@ -76,7 +75,7 @@ public class ServletBearerExchangeFilterFunctionTests {
|
||||
@Test
|
||||
public void filterWhenAuthenticatedWithOtherTokenThenAuthorizationHeaderNull() {
|
||||
TestingAuthenticationToken token = new TestingAuthenticationToken("user", "pass");
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")).build();
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
|
||||
|
||||
this.function.filter(request, this.exchange).subscriberContext(context(token)).block();
|
||||
|
||||
@@ -85,7 +84,7 @@ public class ServletBearerExchangeFilterFunctionTests {
|
||||
|
||||
@Test
|
||||
public void filterWhenAuthenticatedThenAuthorizationHeader() {
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")).build();
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
|
||||
|
||||
this.function.filter(request, this.exchange).subscriberContext(context(this.authentication)).block();
|
||||
|
||||
@@ -95,7 +94,7 @@ public class ServletBearerExchangeFilterFunctionTests {
|
||||
|
||||
@Test
|
||||
public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() {
|
||||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com"))
|
||||
.header(HttpHeaders.AUTHORIZATION, "Existing").build();
|
||||
|
||||
this.function.filter(request, this.exchange).subscriberContext(context(this.authentication)).block();
|
||||
@@ -107,7 +106,8 @@ public class ServletBearerExchangeFilterFunctionTests {
|
||||
private Context context(Authentication authentication) {
|
||||
Map<Class<?>, Object> contextAttributes = new HashMap<>();
|
||||
contextAttributes.put(Authentication.class, authentication);
|
||||
return Context.of(SECURITY_REACTOR_CONTEXT_ATTRIBUTES_KEY, contextAttributes);
|
||||
return Context.of(ServletBearerExchangeFilterFunction.SECURITY_REACTOR_CONTEXT_ATTRIBUTES_KEY,
|
||||
contextAttributes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user