|
@@ -16,8 +16,10 @@
|
|
|
package org.springframework.security.oauth2.server.authorization;
|
|
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
+import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
+import java.io.Serializable;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
@@ -25,12 +27,18 @@ import java.util.Objects;
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
/**
|
|
|
- * Represents a collection of attributes which describe an OAuth 2.0 authorization context.
|
|
|
+ * A representation of an OAuth 2.0 Authorization,
|
|
|
+ * which holds state related to the authorization granted to the {@link #getRegisteredClientId() client}
|
|
|
+ * by the {@link #getPrincipalName() resource owner}.
|
|
|
*
|
|
|
* @author Joe Grandja
|
|
|
* @author Krisztian Toth
|
|
|
+ * @since 0.0.1
|
|
|
+ * @see RegisteredClient
|
|
|
+ * @see OAuth2AccessToken
|
|
|
*/
|
|
|
-public class OAuth2Authorization {
|
|
|
+public class OAuth2Authorization implements Serializable {
|
|
|
+ private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
|
|
|
private String registeredClientId;
|
|
|
private String principalName;
|
|
|
private OAuth2AccessToken accessToken;
|
|
@@ -39,43 +47,64 @@ public class OAuth2Authorization {
|
|
|
protected OAuth2Authorization() {
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the identifier for the {@link RegisteredClient#getId() registered client}.
|
|
|
+ *
|
|
|
+ * @return the {@link RegisteredClient#getId()}
|
|
|
+ */
|
|
|
public String getRegisteredClientId() {
|
|
|
return this.registeredClientId;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the resource owner's {@code Principal} name.
|
|
|
+ *
|
|
|
+ * @return the resource owner's {@code Principal} name
|
|
|
+ */
|
|
|
public String getPrincipalName() {
|
|
|
return this.principalName;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the {@link OAuth2AccessToken access token} credential.
|
|
|
+ *
|
|
|
+ * @return the {@link OAuth2AccessToken}
|
|
|
+ */
|
|
|
public OAuth2AccessToken getAccessToken() {
|
|
|
return this.accessToken;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the attribute(s) associated to the authorization.
|
|
|
+ *
|
|
|
+ * @return a {@code Map} of the attribute(s)
|
|
|
+ */
|
|
|
public Map<String, Object> getAttributes() {
|
|
|
return this.attributes;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns an attribute with the provided name or {@code null} if not found.
|
|
|
+ * Returns the value of an attribute associated to the authorization.
|
|
|
*
|
|
|
* @param name the name of the attribute
|
|
|
- * @param <T> the type of the attribute
|
|
|
- * @return the found attribute or {@code null}
|
|
|
+ * @param <T> the type of the attribute
|
|
|
+ * @return the value of the attribute associated to the authorization, or {@code null} if not available
|
|
|
*/
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
public <T> T getAttribute(String name) {
|
|
|
Assert.hasText(name, "name cannot be empty");
|
|
|
return (T) this.attributes.get(name);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean equals(Object o) {
|
|
|
- if (this == o) {
|
|
|
+ public boolean equals(Object obj) {
|
|
|
+ if (this == obj) {
|
|
|
return true;
|
|
|
}
|
|
|
- if (o == null || getClass() != o.getClass()) {
|
|
|
+ if (obj == null || getClass() != obj.getClass()) {
|
|
|
return false;
|
|
|
}
|
|
|
- OAuth2Authorization that = (OAuth2Authorization) o;
|
|
|
+ OAuth2Authorization that = (OAuth2Authorization) obj;
|
|
|
return Objects.equals(this.registeredClientId, that.registeredClientId) &&
|
|
|
Objects.equals(this.principalName, that.principalName) &&
|
|
|
Objects.equals(this.accessToken, that.accessToken) &&
|
|
@@ -88,59 +117,34 @@ public class OAuth2Authorization {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns an empty {@link Builder}.
|
|
|
+ * Returns a new {@link Builder}, initialized with the provided {@link RegisteredClient#getId()}.
|
|
|
*
|
|
|
+ * @param registeredClient the {@link RegisteredClient}
|
|
|
* @return the {@link Builder}
|
|
|
*/
|
|
|
- public static Builder builder() {
|
|
|
- return new Builder();
|
|
|
+ public static Builder withRegisteredClient(RegisteredClient registeredClient) {
|
|
|
+ Assert.notNull(registeredClient, "registeredClient cannot be null");
|
|
|
+ return new Builder(registeredClient.getId());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns a new {@link Builder}, initialized with the provided {@link OAuth2Authorization}.
|
|
|
- *
|
|
|
- * @param authorization the {@link OAuth2Authorization} to copy from
|
|
|
- * @return the {@link Builder}
|
|
|
+ * A builder for {@link OAuth2Authorization}.
|
|
|
*/
|
|
|
- public static Builder withAuthorization(OAuth2Authorization authorization) {
|
|
|
- Assert.notNull(authorization, "authorization cannot be null");
|
|
|
- return new Builder(authorization);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Builder class for {@link OAuth2Authorization}.
|
|
|
- */
|
|
|
- public static class Builder {
|
|
|
+ public static class Builder implements Serializable {
|
|
|
+ private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
|
|
|
private String registeredClientId;
|
|
|
private String principalName;
|
|
|
private OAuth2AccessToken accessToken;
|
|
|
private Map<String, Object> attributes = new HashMap<>();
|
|
|
|
|
|
- protected Builder() {
|
|
|
- }
|
|
|
-
|
|
|
- protected Builder(OAuth2Authorization authorization) {
|
|
|
- this.registeredClientId = authorization.registeredClientId;
|
|
|
- this.principalName = authorization.principalName;
|
|
|
- this.accessToken = authorization.accessToken;
|
|
|
- this.attributes = authorization.attributes;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Sets the registered client identifier.
|
|
|
- *
|
|
|
- * @param registeredClientId the client id
|
|
|
- * @return the {@link Builder}
|
|
|
- */
|
|
|
- public Builder registeredClientId(String registeredClientId) {
|
|
|
+ protected Builder(String registeredClientId) {
|
|
|
this.registeredClientId = registeredClientId;
|
|
|
- return this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the principal name.
|
|
|
+ * Sets the resource owner's {@code Principal} name.
|
|
|
*
|
|
|
- * @param principalName the principal name
|
|
|
+ * @param principalName the resource owner's {@code Principal} name
|
|
|
* @return the {@link Builder}
|
|
|
*/
|
|
|
public Builder principalName(String principalName) {
|
|
@@ -149,7 +153,7 @@ public class OAuth2Authorization {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the {@link OAuth2AccessToken}.
|
|
|
+ * Sets the {@link OAuth2AccessToken access token} credential.
|
|
|
*
|
|
|
* @param accessToken the {@link OAuth2AccessToken}
|
|
|
* @return the {@link Builder}
|
|
@@ -160,23 +164,24 @@ public class OAuth2Authorization {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Adds the attribute with the specified name and {@link String} value to the attributes map.
|
|
|
+ * Adds an attribute associated to the authorization.
|
|
|
*
|
|
|
- * @param name the name of the attribute
|
|
|
+ * @param name the name of the attribute
|
|
|
* @param value the value of the attribute
|
|
|
* @return the {@link Builder}
|
|
|
*/
|
|
|
- public Builder attribute(String name, String value) {
|
|
|
+ public Builder attribute(String name, Object value) {
|
|
|
Assert.hasText(name, "name cannot be empty");
|
|
|
- Assert.hasText(value, "value cannot be empty");
|
|
|
+ Assert.notNull(value, "value cannot be null");
|
|
|
this.attributes.put(name, value);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * A {@code Consumer} of the attributes map allowing to access or modify its content.
|
|
|
+ * A {@code Consumer} of the attributes {@code Map}
|
|
|
+ * allowing the ability to add, replace, or remove.
|
|
|
*
|
|
|
- * @param attributesConsumer a {@link Consumer} of the attributes map
|
|
|
+ * @param attributesConsumer a {@link Consumer} of the attributes {@code Map}
|
|
|
* @return the {@link Builder}
|
|
|
*/
|
|
|
public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) {
|
|
@@ -190,22 +195,15 @@ public class OAuth2Authorization {
|
|
|
* @return the {@link OAuth2Authorization}
|
|
|
*/
|
|
|
public OAuth2Authorization build() {
|
|
|
- Assert.hasText(this.registeredClientId, "registeredClientId cannot be empty");
|
|
|
Assert.hasText(this.principalName, "principalName cannot be empty");
|
|
|
- if (this.accessToken == null && this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()) == null) {
|
|
|
- throw new IllegalArgumentException("either accessToken has to be set or the authorization code with key '"
|
|
|
- + TokenType.AUTHORIZATION_CODE.getValue() + "' must be provided in the attributes map");
|
|
|
- }
|
|
|
- return create();
|
|
|
- }
|
|
|
-
|
|
|
- private OAuth2Authorization create() {
|
|
|
- OAuth2Authorization oAuth2Authorization = new OAuth2Authorization();
|
|
|
- oAuth2Authorization.registeredClientId = this.registeredClientId;
|
|
|
- oAuth2Authorization.principalName = this.principalName;
|
|
|
- oAuth2Authorization.accessToken = this.accessToken;
|
|
|
- oAuth2Authorization.attributes = Collections.unmodifiableMap(this.attributes);
|
|
|
- return oAuth2Authorization;
|
|
|
+ Assert.notNull(this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()), "authorization code cannot be null");
|
|
|
+
|
|
|
+ OAuth2Authorization authorization = new OAuth2Authorization();
|
|
|
+ authorization.registeredClientId = this.registeredClientId;
|
|
|
+ authorization.principalName = this.principalName;
|
|
|
+ authorization.accessToken = this.accessToken;
|
|
|
+ authorization.attributes = Collections.unmodifiableMap(this.attributes);
|
|
|
+ return authorization;
|
|
|
}
|
|
|
}
|
|
|
}
|