|
@@ -30,43 +30,90 @@ import org.springframework.security.oauth2.server.authorization.client.Registere
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * A context that holds information associated to an OAuth 2.0 Token
|
|
|
|
+ * and is used by an {@link OAuth2TokenCustomizer} for customizing the token attributes.
|
|
|
|
+ *
|
|
* @author Joe Grandja
|
|
* @author Joe Grandja
|
|
* @since 0.1.0
|
|
* @since 0.1.0
|
|
* @see Context
|
|
* @see Context
|
|
|
|
+ * @see OAuth2TokenCustomizer
|
|
*/
|
|
*/
|
|
public interface OAuth2TokenContext extends Context {
|
|
public interface OAuth2TokenContext extends Context {
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns the {@link RegisteredClient registered client}.
|
|
|
|
+ *
|
|
|
|
+ * @return the {@link RegisteredClient}
|
|
|
|
+ */
|
|
default RegisteredClient getRegisteredClient() {
|
|
default RegisteredClient getRegisteredClient() {
|
|
return get(RegisteredClient.class);
|
|
return get(RegisteredClient.class);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns the {@link Authentication} representing the {@code Principal} resource owner (or client).
|
|
|
|
+ *
|
|
|
|
+ * @param <T> the type of the {@code Authentication}
|
|
|
|
+ * @return the {@link Authentication} representing the {@code Principal} resource owner (or client)
|
|
|
|
+ */
|
|
default <T extends Authentication> T getPrincipal() {
|
|
default <T extends Authentication> T getPrincipal() {
|
|
return get(AbstractBuilder.PRINCIPAL_AUTHENTICATION_KEY);
|
|
return get(AbstractBuilder.PRINCIPAL_AUTHENTICATION_KEY);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns the {@link OAuth2Authorization authorization}.
|
|
|
|
+ *
|
|
|
|
+ * @return the {@link OAuth2Authorization}, or {@code null} if not available
|
|
|
|
+ */
|
|
@Nullable
|
|
@Nullable
|
|
default OAuth2Authorization getAuthorization() {
|
|
default OAuth2Authorization getAuthorization() {
|
|
return get(OAuth2Authorization.class);
|
|
return get(OAuth2Authorization.class);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns the authorized scope(s).
|
|
|
|
+ *
|
|
|
|
+ * @return the authorized scope(s)
|
|
|
|
+ */
|
|
default Set<String> getAuthorizedScopes() {
|
|
default Set<String> getAuthorizedScopes() {
|
|
return hasKey(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) ?
|
|
return hasKey(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) ?
|
|
get(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) :
|
|
get(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) :
|
|
Collections.emptySet();
|
|
Collections.emptySet();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns the {@link OAuth2TokenType token type}.
|
|
|
|
+ *
|
|
|
|
+ * @return the {@link OAuth2TokenType}
|
|
|
|
+ */
|
|
default OAuth2TokenType getTokenType() {
|
|
default OAuth2TokenType getTokenType() {
|
|
return get(OAuth2TokenType.class);
|
|
return get(OAuth2TokenType.class);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns the {@link AuthorizationGrantType authorization grant type}.
|
|
|
|
+ *
|
|
|
|
+ * @return the {@link AuthorizationGrantType}
|
|
|
|
+ */
|
|
default AuthorizationGrantType getAuthorizationGrantType() {
|
|
default AuthorizationGrantType getAuthorizationGrantType() {
|
|
return get(AuthorizationGrantType.class);
|
|
return get(AuthorizationGrantType.class);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Returns the {@link Authentication} representing the authorization grant.
|
|
|
|
+ *
|
|
|
|
+ * @param <T> the type of the {@code Authentication}
|
|
|
|
+ * @return the {@link Authentication} representing the authorization grant
|
|
|
|
+ */
|
|
default <T extends Authentication> T getAuthorizationGrant() {
|
|
default <T extends Authentication> T getAuthorizationGrant() {
|
|
return get(AbstractBuilder.AUTHORIZATION_GRANT_AUTHENTICATION_KEY);
|
|
return get(AbstractBuilder.AUTHORIZATION_GRANT_AUTHENTICATION_KEY);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Base builder for implementations of {@link OAuth2TokenContext}.
|
|
|
|
+ *
|
|
|
|
+ * @param <T> the type of the context
|
|
|
|
+ * @param <B> the type of the builder
|
|
|
|
+ */
|
|
abstract class AbstractBuilder<T extends OAuth2TokenContext, B extends AbstractBuilder<T, B>> {
|
|
abstract class AbstractBuilder<T extends OAuth2TokenContext, B extends AbstractBuilder<T, B>> {
|
|
private static final String PRINCIPAL_AUTHENTICATION_KEY =
|
|
private static final String PRINCIPAL_AUTHENTICATION_KEY =
|
|
Authentication.class.getName().concat(".PRINCIPAL");
|
|
Authentication.class.getName().concat(".PRINCIPAL");
|
|
@@ -74,34 +121,83 @@ public interface OAuth2TokenContext extends Context {
|
|
Authentication.class.getName().concat(".AUTHORIZATION_GRANT");
|
|
Authentication.class.getName().concat(".AUTHORIZATION_GRANT");
|
|
private final Map<Object, Object> context = new HashMap<>();
|
|
private final Map<Object, Object> context = new HashMap<>();
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link RegisteredClient registered client}.
|
|
|
|
+ *
|
|
|
|
+ * @param registeredClient the {@link RegisteredClient}
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B registeredClient(RegisteredClient registeredClient) {
|
|
public B registeredClient(RegisteredClient registeredClient) {
|
|
return put(RegisteredClient.class, registeredClient);
|
|
return put(RegisteredClient.class, registeredClient);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link Authentication} representing the {@code Principal} resource owner (or client).
|
|
|
|
+ *
|
|
|
|
+ * @param principal the {@link Authentication} representing the {@code Principal} resource owner (or client)
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B principal(Authentication principal) {
|
|
public B principal(Authentication principal) {
|
|
return put(PRINCIPAL_AUTHENTICATION_KEY, principal);
|
|
return put(PRINCIPAL_AUTHENTICATION_KEY, principal);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link OAuth2Authorization authorization}.
|
|
|
|
+ *
|
|
|
|
+ * @param authorization the {@link OAuth2Authorization}
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B authorization(OAuth2Authorization authorization) {
|
|
public B authorization(OAuth2Authorization authorization) {
|
|
return put(OAuth2Authorization.class, authorization);
|
|
return put(OAuth2Authorization.class, authorization);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the authorized scope(s).
|
|
|
|
+ *
|
|
|
|
+ * @param authorizedScopes the authorized scope(s)
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B authorizedScopes(Set<String> authorizedScopes) {
|
|
public B authorizedScopes(Set<String> authorizedScopes) {
|
|
return put(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes);
|
|
return put(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link OAuth2TokenType token type}.
|
|
|
|
+ *
|
|
|
|
+ * @param tokenType the {@link OAuth2TokenType}
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B tokenType(OAuth2TokenType tokenType) {
|
|
public B tokenType(OAuth2TokenType tokenType) {
|
|
return put(OAuth2TokenType.class, tokenType);
|
|
return put(OAuth2TokenType.class, tokenType);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link AuthorizationGrantType authorization grant type}.
|
|
|
|
+ *
|
|
|
|
+ * @param authorizationGrantType the {@link AuthorizationGrantType}
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B authorizationGrantType(AuthorizationGrantType authorizationGrantType) {
|
|
public B authorizationGrantType(AuthorizationGrantType authorizationGrantType) {
|
|
return put(AuthorizationGrantType.class, authorizationGrantType);
|
|
return put(AuthorizationGrantType.class, authorizationGrantType);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link Authentication} representing the authorization grant.
|
|
|
|
+ *
|
|
|
|
+ * @param authorizationGrant the {@link Authentication} representing the authorization grant
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B authorizationGrant(Authentication authorizationGrant) {
|
|
public B authorizationGrant(Authentication authorizationGrant) {
|
|
return put(AUTHORIZATION_GRANT_AUTHENTICATION_KEY, authorizationGrant);
|
|
return put(AUTHORIZATION_GRANT_AUTHENTICATION_KEY, authorizationGrant);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Associates an attribute.
|
|
|
|
+ *
|
|
|
|
+ * @param key the key for the attribute
|
|
|
|
+ * @param value the value of the attribute
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B put(Object key, Object value) {
|
|
public B put(Object key, Object value) {
|
|
Assert.notNull(key, "key cannot be null");
|
|
Assert.notNull(key, "key cannot be null");
|
|
Assert.notNull(value, "value cannot be null");
|
|
Assert.notNull(value, "value cannot be null");
|
|
@@ -109,6 +205,13 @@ public interface OAuth2TokenContext extends Context {
|
|
return getThis();
|
|
return getThis();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * A {@code Consumer} of the attributes {@code Map}
|
|
|
|
+ * allowing the ability to add, replace, or remove.
|
|
|
|
+ *
|
|
|
|
+ * @param contextConsumer a {@link Consumer} of the attributes {@code Map}
|
|
|
|
+ * @return the {@link AbstractBuilder} for further configuration
|
|
|
|
+ */
|
|
public B context(Consumer<Map<Object, Object>> contextConsumer) {
|
|
public B context(Consumer<Map<Object, Object>> contextConsumer) {
|
|
contextConsumer.accept(this.context);
|
|
contextConsumer.accept(this.context);
|
|
return getThis();
|
|
return getThis();
|
|
@@ -128,7 +231,13 @@ public interface OAuth2TokenContext extends Context {
|
|
return (B) this;
|
|
return (B) this;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Builds a new {@link OAuth2TokenContext}.
|
|
|
|
+ *
|
|
|
|
+ * @return the {@link OAuth2TokenContext}
|
|
|
|
+ */
|
|
public abstract T build();
|
|
public abstract T build();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|