|
15 | 15 | */
|
16 | 16 | package org.springframework.security.oauth2.server.authorization;
|
17 | 17 |
|
18 |
| -import org.springframework.lang.Nullable; |
19 | 18 | import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
20 | 19 | import org.springframework.util.Assert;
|
| 20 | +import org.springframework.util.CollectionUtils; |
21 | 21 |
|
22 | 22 | import java.util.Collections;
|
23 | 23 | import java.util.Map;
|
|
30 | 30 | * @author Krisztian Toth
|
31 | 31 | */
|
32 | 32 | public class OAuth2Authorization {
|
33 |
| - private final String registeredClientId; |
34 |
| - private final String principalName; |
35 |
| - private final OAuth2AccessToken accessToken; |
36 |
| - private final Map<String, Object> attributes; |
| 33 | + private String registeredClientId; |
| 34 | + private String principalName; |
| 35 | + private OAuth2AccessToken accessToken; |
| 36 | + private Map<String, Object> attributes; |
37 | 37 |
|
38 |
| - /** |
39 |
| - * Creates a {@link OAuth2Authorization} object with the provided params and an empty, unmodifiable |
40 |
| - * {@code attributes} map. |
41 |
| - * |
42 |
| - * @see #OAuth2Authorization(String, String, OAuth2AccessToken, Map) |
43 |
| - */ |
44 |
| - public OAuth2Authorization(String registeredClientId, String principalName, OAuth2AccessToken accessToken) { |
45 |
| - this(registeredClientId, principalName, accessToken, Collections.emptyMap()); |
46 |
| - } |
47 |
| - |
48 |
| - /** |
49 |
| - * Creates a {@link OAuth2Authorization} object without an {@link OAuth2AccessToken}. |
50 |
| - * |
51 |
| - * @see #OAuth2Authorization(String, String, OAuth2AccessToken, Map) |
52 |
| - */ |
53 |
| - public OAuth2Authorization(String registeredClientId, String principalName, Map<String, Object> attributes) { |
54 |
| - this(registeredClientId, principalName, null, attributes); |
55 |
| - } |
56 |
| - |
57 |
| - /** |
58 |
| - * Creates an {@link OAuth2Authorization} object with the provided params. |
59 |
| - * |
60 |
| - * @param registeredClientId the client's identifier which issued the authorization |
61 |
| - * @param principalName the name of the principal the client wants to authorize |
62 |
| - * @param accessToken the access token of the authorization |
63 |
| - * @param attributes additional attributes associated with the authorization |
64 |
| - */ |
65 |
| - public OAuth2Authorization(String registeredClientId, String principalName, |
66 |
| - @Nullable OAuth2AccessToken accessToken, @Nullable Map<String, Object> attributes) { |
67 |
| - Assert.hasText(registeredClientId, "registeredClientId cannot be empty"); |
68 |
| - Assert.hasText(principalName, "principalName cannot be empty"); |
69 |
| - this.registeredClientId = registeredClientId; |
70 |
| - this.principalName = principalName; |
71 |
| - this.accessToken = accessToken; |
72 |
| - this.attributes = attributes == null ? |
73 |
| - Collections.emptyMap() : |
74 |
| - Collections.unmodifiableMap(attributes); |
| 38 | + private OAuth2Authorization() { |
75 | 39 | }
|
76 | 40 |
|
77 | 41 | public String getRegisteredClientId() {
|
@@ -119,4 +83,115 @@ public String toString() {
|
119 | 83 | ", attributes=" + attributes +
|
120 | 84 | '}';
|
121 | 85 | }
|
| 86 | + |
| 87 | + /** |
| 88 | + * Returns an empty {@link Builder}. |
| 89 | + * |
| 90 | + * @return the {@link Builder} |
| 91 | + */ |
| 92 | + public static Builder builder() { |
| 93 | + return new Builder(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns a new {@link Builder}, initialized with the provided {@link OAuth2Authorization}. |
| 98 | + * |
| 99 | + * @param authorization the {@link OAuth2Authorization} to copy from |
| 100 | + * @return the {@link Builder} |
| 101 | + */ |
| 102 | + public static Builder withOAuth2Authorization(OAuth2Authorization authorization) { |
| 103 | + Assert.notNull(authorization, "authorization cannot be null"); |
| 104 | + return new Builder(authorization); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Builder class for {@link OAuth2Authorization}. |
| 109 | + */ |
| 110 | + public static final class Builder { |
| 111 | + private String registeredClientId; |
| 112 | + private String principalName; |
| 113 | + private OAuth2AccessToken accessToken; |
| 114 | + private Map<String, Object> attributes; |
| 115 | + |
| 116 | + protected Builder() { |
| 117 | + } |
| 118 | + |
| 119 | + protected Builder(OAuth2Authorization authorization) { |
| 120 | + this.registeredClientId = authorization.registeredClientId; |
| 121 | + this.principalName = authorization.principalName; |
| 122 | + this.accessToken = authorization.accessToken; |
| 123 | + this.attributes = authorization.attributes; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets the registered client identifier. |
| 128 | + * |
| 129 | + * @param registeredClientId the client id |
| 130 | + * @return the {@link Builder} |
| 131 | + */ |
| 132 | + public Builder registeredClientId(String registeredClientId) { |
| 133 | + this.registeredClientId = registeredClientId; |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sets the principal name. |
| 139 | + * |
| 140 | + * @param principalName the principal name |
| 141 | + * @return the {@link Builder} |
| 142 | + */ |
| 143 | + public Builder principalName(String principalName) { |
| 144 | + this.principalName = principalName; |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Sets the {@link OAuth2AccessToken}. |
| 150 | + * |
| 151 | + * @param accessToken the {@link OAuth2AccessToken} |
| 152 | + * @return the {@link Builder} |
| 153 | + */ |
| 154 | + public Builder accessToken(OAuth2AccessToken accessToken) { |
| 155 | + this.accessToken = accessToken; |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Sets the attributes map. |
| 161 | + * |
| 162 | + * @param attributes the attributes map |
| 163 | + * @return the {@link Builder} |
| 164 | + */ |
| 165 | + public Builder attributes(Map<String, Object> attributes) { |
| 166 | + this.attributes = attributes; |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Builds a new {@link OAuth2Authorization}. |
| 172 | + * |
| 173 | + * @return the {@link OAuth2Authorization} |
| 174 | + */ |
| 175 | + public OAuth2Authorization build() { |
| 176 | + Assert.hasText(this.registeredClientId, "registeredClientId cannot be empty"); |
| 177 | + Assert.hasText(this.principalName, "principalName cannot be empty"); |
| 178 | + if (CollectionUtils.isEmpty(this.attributes)) { |
| 179 | + this.attributes = Collections.emptyMap(); |
| 180 | + } |
| 181 | + if (this.accessToken == null && this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()) == null) { |
| 182 | + throw new IllegalArgumentException("either accessToken has to be set or the authorization code with key '" |
| 183 | + + TokenType.AUTHORIZATION_CODE.getValue() + "' must be provided in the attributes map"); |
| 184 | + } |
| 185 | + return create(); |
| 186 | + } |
| 187 | + |
| 188 | + private OAuth2Authorization create() { |
| 189 | + OAuth2Authorization oAuth2Authorization = new OAuth2Authorization(); |
| 190 | + oAuth2Authorization.registeredClientId = this.registeredClientId; |
| 191 | + oAuth2Authorization.principalName = this.principalName; |
| 192 | + oAuth2Authorization.accessToken = this.accessToken; |
| 193 | + oAuth2Authorization.attributes = this.attributes; |
| 194 | + return oAuth2Authorization; |
| 195 | + } |
| 196 | + } |
122 | 197 | }
|
0 commit comments