2020-04-24 03:45:34 +07:00
|
|
|
/*
|
2021-02-06 01:20:17 +07:00
|
|
|
* Copyright 2020-2021 the original author or authors.
|
2020-04-24 03:45:34 +07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.springframework.security.oauth2.server.authorization;
|
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
import java.io.Serializable;
|
2020-04-27 16:52:09 +07:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2020-04-24 03:45:34 +07:00
|
|
|
import java.util.Map;
|
2020-04-27 16:52:09 +07:00
|
|
|
import java.util.Objects;
|
2021-02-10 02:12:21 +07:00
|
|
|
import java.util.UUID;
|
2020-04-27 16:52:09 +07:00
|
|
|
import java.util.function.Consumer;
|
2020-04-24 03:45:34 +07:00
|
|
|
|
2021-02-06 01:20:17 +07:00
|
|
|
import org.springframework.lang.Nullable;
|
2021-02-12 02:36:18 +07:00
|
|
|
import org.springframework.security.oauth2.core.Version;
|
2021-02-06 01:20:17 +07:00
|
|
|
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
|
2021-02-09 02:57:15 +07:00
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
2021-02-06 01:20:17 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken2;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
|
import org.springframework.util.Assert;
|
2021-02-10 02:12:21 +07:00
|
|
|
import org.springframework.util.StringUtils;
|
2021-02-06 01:20:17 +07:00
|
|
|
|
2020-04-24 03:45:34 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* A representation of an OAuth 2.0 Authorization, which holds state related to the authorization granted
|
|
|
|
* to a {@link #getRegisteredClientId() client}, by the {@link #getPrincipalName() resource owner}
|
|
|
|
* or itself in the case of the {@code client_credentials} grant type.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
2020-04-24 03:45:34 +07:00
|
|
|
* @author Joe Grandja
|
2020-04-27 16:52:09 +07:00
|
|
|
* @author Krisztian Toth
|
2020-05-16 15:27:22 +07:00
|
|
|
* @since 0.0.1
|
|
|
|
* @see RegisteredClient
|
2021-02-09 02:57:15 +07:00
|
|
|
* @see AuthorizationGrantType
|
2021-02-06 01:20:17 +07:00
|
|
|
* @see AbstractOAuth2Token
|
|
|
|
* @see OAuth2AccessToken
|
|
|
|
* @see OAuth2RefreshToken
|
2020-04-24 03:45:34 +07:00
|
|
|
*/
|
2020-05-16 15:27:22 +07:00
|
|
|
public class OAuth2Authorization implements Serializable {
|
2020-09-19 19:46:14 +07:00
|
|
|
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
|
2021-02-09 15:25:21 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the {@link #getAttribute(String) attribute} used for the authorized scope(s).
|
|
|
|
* The value of the attribute is of type {@code Set<String>}.
|
|
|
|
*/
|
|
|
|
public static final String AUTHORIZED_SCOPE_ATTRIBUTE_NAME =
|
|
|
|
OAuth2Authorization.class.getName().concat(".AUTHORIZED_SCOPE");
|
|
|
|
|
2021-02-10 02:12:21 +07:00
|
|
|
private String id;
|
2020-04-24 03:45:34 +07:00
|
|
|
private String registeredClientId;
|
|
|
|
private String principalName;
|
2021-02-09 02:57:15 +07:00
|
|
|
private AuthorizationGrantType authorizationGrantType;
|
2021-02-06 01:20:17 +07:00
|
|
|
private Map<Class<? extends AbstractOAuth2Token>, Token<?>> tokens;
|
2020-04-24 03:45:34 +07:00
|
|
|
private Map<String, Object> attributes;
|
|
|
|
|
2020-04-27 16:52:09 +07:00
|
|
|
protected OAuth2Authorization() {
|
|
|
|
}
|
|
|
|
|
2021-02-10 02:12:21 +07:00
|
|
|
/**
|
|
|
|
* Returns the identifier for the authorization.
|
|
|
|
*
|
|
|
|
* @return the identifier for the authorization
|
|
|
|
*/
|
|
|
|
public String getId() {
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
/**
|
|
|
|
* Returns the identifier for the {@link RegisteredClient#getId() registered client}.
|
|
|
|
*
|
|
|
|
* @return the {@link RegisteredClient#getId()}
|
|
|
|
*/
|
2020-04-27 16:52:09 +07:00
|
|
|
public String getRegisteredClientId() {
|
|
|
|
return this.registeredClientId;
|
|
|
|
}
|
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* Returns the {@code Principal} name of the resource owner (or client).
|
2020-05-16 15:27:22 +07:00
|
|
|
*
|
2021-02-06 01:20:17 +07:00
|
|
|
* @return the {@code Principal} name of the resource owner (or client)
|
2020-05-16 15:27:22 +07:00
|
|
|
*/
|
2020-04-27 16:52:09 +07:00
|
|
|
public String getPrincipalName() {
|
|
|
|
return this.principalName;
|
|
|
|
}
|
|
|
|
|
2021-02-09 02:57:15 +07:00
|
|
|
/**
|
|
|
|
* Returns the {@link AuthorizationGrantType authorization grant type} used for the authorization.
|
|
|
|
*
|
|
|
|
* @return the {@link AuthorizationGrantType} used for the authorization
|
|
|
|
*/
|
|
|
|
public AuthorizationGrantType getAuthorizationGrantType() {
|
|
|
|
return this.authorizationGrantType;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:12:45 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* Returns the {@link Token} of type {@link OAuth2AccessToken}.
|
2020-10-19 19:12:45 +07:00
|
|
|
*
|
2021-02-06 01:20:17 +07:00
|
|
|
* @return the {@link Token} of type {@link OAuth2AccessToken}
|
2020-10-19 19:12:45 +07:00
|
|
|
*/
|
2021-02-06 01:20:17 +07:00
|
|
|
public Token<OAuth2AccessToken> getAccessToken() {
|
|
|
|
return getToken(OAuth2AccessToken.class);
|
2020-10-19 19:12:45 +07:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* Returns the {@link Token} of type {@link OAuth2RefreshToken}.
|
2020-05-16 15:27:22 +07:00
|
|
|
*
|
2021-02-06 01:20:17 +07:00
|
|
|
* @return the {@link Token} of type {@link OAuth2RefreshToken}, or {@code null} if not available
|
2020-05-16 15:27:22 +07:00
|
|
|
*/
|
2021-02-06 01:20:17 +07:00
|
|
|
@Nullable
|
|
|
|
public Token<OAuth2RefreshToken> getRefreshToken() {
|
|
|
|
return getToken(OAuth2RefreshToken.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the {@link Token} of type {@code tokenType}.
|
|
|
|
*
|
|
|
|
* @param tokenType the token type
|
|
|
|
* @param <T> the type of the token
|
|
|
|
* @return the {@link Token}, or {@code null} if not available
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T extends AbstractOAuth2Token> Token<T> getToken(Class<T> tokenType) {
|
|
|
|
Assert.notNull(tokenType, "tokenType cannot be null");
|
|
|
|
Token<?> token = this.tokens.get(tokenType);
|
|
|
|
return token != null ? (Token<T>) token : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the {@link Token} matching the {@code tokenValue}.
|
|
|
|
*
|
|
|
|
* @param tokenValue the token value
|
|
|
|
* @param <T> the type of the token
|
|
|
|
* @return the {@link Token}, or {@code null} if not available
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T extends AbstractOAuth2Token> Token<T> getToken(String tokenValue) {
|
|
|
|
Assert.hasText(tokenValue, "tokenValue cannot be empty");
|
|
|
|
Token<?> token = this.tokens.values().stream()
|
|
|
|
.filter(t -> t.getToken().getTokenValue().equals(tokenValue))
|
|
|
|
.findFirst()
|
|
|
|
.orElse(null);
|
|
|
|
return token != null ? (Token<T>) token : null;
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
/**
|
|
|
|
* Returns the attribute(s) associated to the authorization.
|
|
|
|
*
|
|
|
|
* @return a {@code Map} of the attribute(s)
|
|
|
|
*/
|
2020-04-27 16:52:09 +07:00
|
|
|
public Map<String, Object> getAttributes() {
|
|
|
|
return this.attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* Returns the value of an attribute associated to the authorization.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
|
|
|
* @param name the name of the attribute
|
2020-05-16 15:27:22 +07:00
|
|
|
* @param <T> the type of the attribute
|
2021-02-06 01:20:17 +07:00
|
|
|
* @return the value of an attribute associated to the authorization, or {@code null} if not available
|
2020-04-27 16:52:09 +07:00
|
|
|
*/
|
2021-02-06 01:20:17 +07:00
|
|
|
@Nullable
|
2020-05-16 15:27:22 +07:00
|
|
|
@SuppressWarnings("unchecked")
|
2020-04-27 16:52:09 +07:00
|
|
|
public <T> T getAttribute(String name) {
|
|
|
|
Assert.hasText(name, "name cannot be empty");
|
|
|
|
return (T) this.attributes.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-16 15:27:22 +07:00
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) {
|
2020-04-27 16:52:09 +07:00
|
|
|
return true;
|
|
|
|
}
|
2020-05-16 15:27:22 +07:00
|
|
|
if (obj == null || getClass() != obj.getClass()) {
|
2020-04-27 16:52:09 +07:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-16 15:27:22 +07:00
|
|
|
OAuth2Authorization that = (OAuth2Authorization) obj;
|
2021-02-10 02:12:21 +07:00
|
|
|
return Objects.equals(this.id, that.id) &&
|
|
|
|
Objects.equals(this.registeredClientId, that.registeredClientId) &&
|
2020-04-27 16:52:09 +07:00
|
|
|
Objects.equals(this.principalName, that.principalName) &&
|
2021-02-09 02:57:15 +07:00
|
|
|
Objects.equals(this.authorizationGrantType, that.authorizationGrantType) &&
|
2020-10-19 19:12:45 +07:00
|
|
|
Objects.equals(this.tokens, that.tokens) &&
|
2020-04-27 16:52:09 +07:00
|
|
|
Objects.equals(this.attributes, that.attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2021-02-10 02:12:21 +07:00
|
|
|
return Objects.hash(this.id, this.registeredClientId, this.principalName,
|
2021-02-09 02:57:15 +07:00
|
|
|
this.authorizationGrantType, this.tokens, this.attributes);
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* Returns a new {@link Builder}, initialized with the provided {@link RegisteredClient#getId()}.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
2020-05-16 15:27:22 +07:00
|
|
|
* @param registeredClient the {@link RegisteredClient}
|
2020-04-27 16:52:09 +07:00
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
2020-05-16 15:27:22 +07:00
|
|
|
public static Builder withRegisteredClient(RegisteredClient registeredClient) {
|
|
|
|
Assert.notNull(registeredClient, "registeredClient cannot be null");
|
|
|
|
return new Builder(registeredClient.getId());
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
|
2020-06-04 03:08:52 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* Returns a new {@link Builder}, initialized with the values from the provided {@code OAuth2Authorization}.
|
2020-06-04 03:08:52 +07:00
|
|
|
*
|
2021-02-06 01:20:17 +07:00
|
|
|
* @param authorization the {@code OAuth2Authorization} used for initializing the {@link Builder}
|
2020-06-04 03:08:52 +07:00
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
|
|
|
public static Builder from(OAuth2Authorization authorization) {
|
|
|
|
Assert.notNull(authorization, "authorization cannot be null");
|
|
|
|
return new Builder(authorization.getRegisteredClientId())
|
2021-02-10 02:12:21 +07:00
|
|
|
.id(authorization.getId())
|
2020-06-04 03:08:52 +07:00
|
|
|
.principalName(authorization.getPrincipalName())
|
2021-02-09 02:57:15 +07:00
|
|
|
.authorizationGrantType(authorization.getAuthorizationGrantType())
|
2021-02-06 01:20:17 +07:00
|
|
|
.tokens(authorization.tokens)
|
2020-06-04 03:08:52 +07:00
|
|
|
.attributes(attrs -> attrs.putAll(authorization.getAttributes()));
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:20:17 +07:00
|
|
|
/**
|
|
|
|
* A holder of an OAuth 2.0 Token and it's associated metadata.
|
|
|
|
*
|
|
|
|
* @author Joe Grandja
|
|
|
|
* @since 0.1.0
|
|
|
|
*/
|
|
|
|
public static class Token<T extends AbstractOAuth2Token> implements Serializable {
|
|
|
|
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
|
|
|
|
protected static final String TOKEN_METADATA_BASE = "metadata.token.";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the metadata that indicates if the token has been invalidated.
|
|
|
|
*/
|
|
|
|
public static final String INVALIDATED_METADATA_NAME = TOKEN_METADATA_BASE.concat("invalidated");
|
|
|
|
|
2021-02-09 08:33:17 +07:00
|
|
|
/**
|
|
|
|
* The name of the metadata used for the claims of the token.
|
|
|
|
*/
|
|
|
|
public static final String CLAIMS_METADATA_NAME = TOKEN_METADATA_BASE.concat("claims");
|
|
|
|
|
2021-02-06 01:20:17 +07:00
|
|
|
private final T token;
|
|
|
|
private final Map<String, Object> metadata;
|
|
|
|
|
|
|
|
protected Token(T token) {
|
|
|
|
this(token, defaultMetadata());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Token(T token, Map<String, Object> metadata) {
|
|
|
|
this.token = token;
|
|
|
|
this.metadata = Collections.unmodifiableMap(metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the token of type {@link AbstractOAuth2Token}.
|
|
|
|
*
|
|
|
|
* @return the token of type {@link AbstractOAuth2Token}
|
|
|
|
*/
|
|
|
|
public T getToken() {
|
|
|
|
return this.token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if the token has been invalidated (e.g. revoked).
|
|
|
|
* The default is {@code false}.
|
|
|
|
*
|
|
|
|
* @return {@code true} if the token has been invalidated, {@code false} otherwise
|
|
|
|
*/
|
|
|
|
public boolean isInvalidated() {
|
|
|
|
return Boolean.TRUE.equals(getMetadata(INVALIDATED_METADATA_NAME));
|
|
|
|
}
|
|
|
|
|
2021-02-09 08:33:17 +07:00
|
|
|
/**
|
|
|
|
* Returns the claims associated to the token.
|
|
|
|
*
|
|
|
|
* @return a {@code Map} of the claims, or {@code null} if not available
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public Map<String, Object> getClaims() {
|
|
|
|
return getMetadata(CLAIMS_METADATA_NAME);
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:20:17 +07:00
|
|
|
/**
|
|
|
|
* Returns the value of the metadata associated to the token.
|
|
|
|
*
|
|
|
|
* @param name the name of the metadata
|
|
|
|
* @param <V> the value type of the metadata
|
|
|
|
* @return the value of the metadata, or {@code null} if not available
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <V> V getMetadata(String name) {
|
|
|
|
Assert.hasText(name, "name cannot be empty");
|
|
|
|
return (V) this.metadata.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the metadata associated to the token.
|
|
|
|
*
|
|
|
|
* @return a {@code Map} of the metadata
|
|
|
|
*/
|
|
|
|
public Map<String, Object> getMetadata() {
|
|
|
|
return this.metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static Map<String, Object> defaultMetadata() {
|
|
|
|
Map<String, Object> metadata = new HashMap<>();
|
|
|
|
metadata.put(INVALIDATED_METADATA_NAME, false);
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (obj == null || getClass() != obj.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Token<?> that = (Token<?>) obj;
|
|
|
|
return Objects.equals(this.token, that.token) &&
|
|
|
|
Objects.equals(this.metadata, that.metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return Objects.hash(this.token, this.metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:52:09 +07:00
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* A builder for {@link OAuth2Authorization}.
|
2020-04-27 16:52:09 +07:00
|
|
|
*/
|
2020-05-16 15:27:22 +07:00
|
|
|
public static class Builder implements Serializable {
|
2020-09-19 19:46:14 +07:00
|
|
|
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
|
2021-02-10 02:12:21 +07:00
|
|
|
private String id;
|
2021-02-06 01:20:17 +07:00
|
|
|
private final String registeredClientId;
|
2020-04-27 16:52:09 +07:00
|
|
|
private String principalName;
|
2021-02-09 02:57:15 +07:00
|
|
|
private AuthorizationGrantType authorizationGrantType;
|
2021-02-06 01:20:17 +07:00
|
|
|
private Map<Class<? extends AbstractOAuth2Token>, Token<?>> tokens = new HashMap<>();
|
|
|
|
private final Map<String, Object> attributes = new HashMap<>();
|
2020-04-27 16:52:09 +07:00
|
|
|
|
2020-05-16 15:27:22 +07:00
|
|
|
protected Builder(String registeredClientId) {
|
2020-04-27 16:52:09 +07:00
|
|
|
this.registeredClientId = registeredClientId;
|
|
|
|
}
|
|
|
|
|
2021-02-10 02:12:21 +07:00
|
|
|
/**
|
|
|
|
* Sets the identifier for the authorization.
|
|
|
|
*
|
|
|
|
* @param id the identifier for the authorization
|
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
|
|
|
public Builder id(String id) {
|
|
|
|
this.id = id;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:52:09 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* Sets the {@code Principal} name of the resource owner (or client).
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
2021-02-06 01:20:17 +07:00
|
|
|
* @param principalName the {@code Principal} name of the resource owner (or client)
|
2020-04-27 16:52:09 +07:00
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
|
|
|
public Builder principalName(String principalName) {
|
|
|
|
this.principalName = principalName;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-02-09 02:57:15 +07:00
|
|
|
/**
|
|
|
|
* Sets the {@link AuthorizationGrantType authorization grant type} used for the authorization.
|
|
|
|
*
|
|
|
|
* @param authorizationGrantType the {@link AuthorizationGrantType}
|
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
|
|
|
public Builder authorizationGrantType(AuthorizationGrantType authorizationGrantType) {
|
|
|
|
this.authorizationGrantType = authorizationGrantType;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:12:45 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* Sets the {@link OAuth2AccessToken access token}.
|
2020-10-19 19:12:45 +07:00
|
|
|
*
|
2021-02-06 01:20:17 +07:00
|
|
|
* @param accessToken the {@link OAuth2AccessToken}
|
2020-10-19 19:12:45 +07:00
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
2021-02-06 01:20:17 +07:00
|
|
|
public Builder accessToken(OAuth2AccessToken accessToken) {
|
|
|
|
return token(accessToken);
|
2020-10-19 19:12:45 +07:00
|
|
|
}
|
|
|
|
|
2020-04-27 16:52:09 +07:00
|
|
|
/**
|
2021-02-06 01:20:17 +07:00
|
|
|
* Sets the {@link OAuth2RefreshToken refresh token}.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
2021-02-06 01:20:17 +07:00
|
|
|
* @param refreshToken the {@link OAuth2RefreshToken}
|
2020-04-27 16:52:09 +07:00
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
2021-02-06 01:20:17 +07:00
|
|
|
public Builder refreshToken(OAuth2RefreshToken refreshToken) {
|
|
|
|
return token(refreshToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the {@link AbstractOAuth2Token token}.
|
|
|
|
*
|
|
|
|
* @param token the token
|
|
|
|
* @param <T> the type of the token
|
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
|
|
|
public <T extends AbstractOAuth2Token> Builder token(T token) {
|
|
|
|
return token(token, (metadata) -> {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the {@link AbstractOAuth2Token token} and associated metadata.
|
|
|
|
*
|
|
|
|
* @param token the token
|
|
|
|
* @param metadataConsumer a {@code Consumer} of the metadata {@code Map}
|
|
|
|
* @param <T> the type of the token
|
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
|
|
|
public <T extends AbstractOAuth2Token> Builder token(T token,
|
|
|
|
Consumer<Map<String, Object>> metadataConsumer) {
|
|
|
|
|
|
|
|
Assert.notNull(token, "token cannot be null");
|
|
|
|
Map<String, Object> metadata = Token.defaultMetadata();
|
2021-02-09 16:55:31 +07:00
|
|
|
Token<?> existingToken = this.tokens.get(token.getClass());
|
|
|
|
if (existingToken != null) {
|
|
|
|
metadata.putAll(existingToken.getMetadata());
|
|
|
|
}
|
2021-02-06 01:20:17 +07:00
|
|
|
metadataConsumer.accept(metadata);
|
|
|
|
Class<? extends AbstractOAuth2Token> tokenClass = token.getClass();
|
|
|
|
if (tokenClass.equals(OAuth2RefreshToken2.class)) {
|
|
|
|
tokenClass = OAuth2RefreshToken.class;
|
|
|
|
}
|
|
|
|
this.tokens.put(tokenClass, new Token<>(token, metadata));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final Builder tokens(Map<Class<? extends AbstractOAuth2Token>, Token<?>> tokens) {
|
|
|
|
this.tokens = new HashMap<>(tokens);
|
2020-04-27 16:52:09 +07:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* Adds an attribute associated to the authorization.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
2020-05-16 15:27:22 +07:00
|
|
|
* @param name the name of the attribute
|
2020-04-27 16:52:09 +07:00
|
|
|
* @param value the value of the attribute
|
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
2020-05-16 15:27:22 +07:00
|
|
|
public Builder attribute(String name, Object value) {
|
2020-04-27 16:52:09 +07:00
|
|
|
Assert.hasText(name, "name cannot be empty");
|
2020-05-16 15:27:22 +07:00
|
|
|
Assert.notNull(value, "value cannot be null");
|
2020-04-27 16:52:09 +07:00
|
|
|
this.attributes.put(name, value);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-16 15:27:22 +07:00
|
|
|
* A {@code Consumer} of the attributes {@code Map}
|
|
|
|
* allowing the ability to add, replace, or remove.
|
2020-04-27 16:52:09 +07:00
|
|
|
*
|
2020-05-16 15:27:22 +07:00
|
|
|
* @param attributesConsumer a {@link Consumer} of the attributes {@code Map}
|
2020-04-27 16:52:09 +07:00
|
|
|
* @return the {@link Builder}
|
|
|
|
*/
|
|
|
|
public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) {
|
|
|
|
attributesConsumer.accept(this.attributes);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a new {@link OAuth2Authorization}.
|
|
|
|
*
|
|
|
|
* @return the {@link OAuth2Authorization}
|
|
|
|
*/
|
|
|
|
public OAuth2Authorization build() {
|
|
|
|
Assert.hasText(this.principalName, "principalName cannot be empty");
|
2021-02-09 02:57:15 +07:00
|
|
|
Assert.notNull(this.authorizationGrantType, "authorizationGrantType cannot be null");
|
2020-05-16 15:27:22 +07:00
|
|
|
|
|
|
|
OAuth2Authorization authorization = new OAuth2Authorization();
|
2021-02-10 02:12:21 +07:00
|
|
|
if (!StringUtils.hasText(this.id)) {
|
|
|
|
this.id = UUID.randomUUID().toString();
|
|
|
|
}
|
|
|
|
authorization.id = this.id;
|
2020-05-16 15:27:22 +07:00
|
|
|
authorization.registeredClientId = this.registeredClientId;
|
|
|
|
authorization.principalName = this.principalName;
|
2021-02-09 02:57:15 +07:00
|
|
|
authorization.authorizationGrantType = this.authorizationGrantType;
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.tokens = Collections.unmodifiableMap(this.tokens);
|
2020-05-16 15:27:22 +07:00
|
|
|
authorization.attributes = Collections.unmodifiableMap(this.attributes);
|
|
|
|
return authorization;
|
2020-04-27 16:52:09 +07:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|