|
16 | 16 | package org.springframework.security.oauth2.server.authorization;
|
17 | 17 |
|
18 | 18 | import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
| 19 | +import org.springframework.util.Assert; |
19 | 20 |
|
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
20 | 23 | import java.util.Map;
|
| 24 | +import java.util.Objects; |
| 25 | +import java.util.function.Consumer; |
21 | 26 |
|
22 | 27 | /**
|
| 28 | + * Represents a collection of attributes which describe an OAuth 2.0 authorization context. |
| 29 | + * |
23 | 30 | * @author Joe Grandja
|
| 31 | + * @author Krisztian Toth |
24 | 32 | */
|
25 | 33 | public class OAuth2Authorization {
|
26 | 34 | private String registeredClientId;
|
27 | 35 | private String principalName;
|
28 | 36 | private OAuth2AccessToken accessToken;
|
29 | 37 | private Map<String, Object> attributes;
|
30 | 38 |
|
| 39 | + protected OAuth2Authorization() { |
| 40 | + } |
| 41 | + |
| 42 | + public String getRegisteredClientId() { |
| 43 | + return this.registeredClientId; |
| 44 | + } |
| 45 | + |
| 46 | + public String getPrincipalName() { |
| 47 | + return this.principalName; |
| 48 | + } |
| 49 | + |
| 50 | + public OAuth2AccessToken getAccessToken() { |
| 51 | + return this.accessToken; |
| 52 | + } |
| 53 | + |
| 54 | + public Map<String, Object> getAttributes() { |
| 55 | + return this.attributes; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns an attribute with the provided name or {@code null} if not found. |
| 60 | + * |
| 61 | + * @param name the name of the attribute |
| 62 | + * @param <T> the type of the attribute |
| 63 | + * @return the found attribute or {@code null} |
| 64 | + */ |
| 65 | + public <T> T getAttribute(String name) { |
| 66 | + Assert.hasText(name, "name cannot be empty"); |
| 67 | + return (T) this.attributes.get(name); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean equals(Object o) { |
| 72 | + if (this == o) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + if (o == null || getClass() != o.getClass()) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + OAuth2Authorization that = (OAuth2Authorization) o; |
| 79 | + return Objects.equals(this.registeredClientId, that.registeredClientId) && |
| 80 | + Objects.equals(this.principalName, that.principalName) && |
| 81 | + Objects.equals(this.accessToken, that.accessToken) && |
| 82 | + Objects.equals(this.attributes, that.attributes); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int hashCode() { |
| 87 | + return Objects.hash(this.registeredClientId, this.principalName, this.accessToken, this.attributes); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns an empty {@link Builder}. |
| 92 | + * |
| 93 | + * @return the {@link Builder} |
| 94 | + */ |
| 95 | + public static Builder builder() { |
| 96 | + return new Builder(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns a new {@link Builder}, initialized with the provided {@link OAuth2Authorization}. |
| 101 | + * |
| 102 | + * @param authorization the {@link OAuth2Authorization} to copy from |
| 103 | + * @return the {@link Builder} |
| 104 | + */ |
| 105 | + public static Builder withAuthorization(OAuth2Authorization authorization) { |
| 106 | + Assert.notNull(authorization, "authorization cannot be null"); |
| 107 | + return new Builder(authorization); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Builder class for {@link OAuth2Authorization}. |
| 112 | + */ |
| 113 | + public static class Builder { |
| 114 | + private String registeredClientId; |
| 115 | + private String principalName; |
| 116 | + private OAuth2AccessToken accessToken; |
| 117 | + private Map<String, Object> attributes = new HashMap<>(); |
| 118 | + |
| 119 | + protected Builder() { |
| 120 | + } |
| 121 | + |
| 122 | + protected Builder(OAuth2Authorization authorization) { |
| 123 | + this.registeredClientId = authorization.registeredClientId; |
| 124 | + this.principalName = authorization.principalName; |
| 125 | + this.accessToken = authorization.accessToken; |
| 126 | + this.attributes = authorization.attributes; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Sets the registered client identifier. |
| 131 | + * |
| 132 | + * @param registeredClientId the client id |
| 133 | + * @return the {@link Builder} |
| 134 | + */ |
| 135 | + public Builder registeredClientId(String registeredClientId) { |
| 136 | + this.registeredClientId = registeredClientId; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Sets the principal name. |
| 142 | + * |
| 143 | + * @param principalName the principal name |
| 144 | + * @return the {@link Builder} |
| 145 | + */ |
| 146 | + public Builder principalName(String principalName) { |
| 147 | + this.principalName = principalName; |
| 148 | + return this; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Sets the {@link OAuth2AccessToken}. |
| 153 | + * |
| 154 | + * @param accessToken the {@link OAuth2AccessToken} |
| 155 | + * @return the {@link Builder} |
| 156 | + */ |
| 157 | + public Builder accessToken(OAuth2AccessToken accessToken) { |
| 158 | + this.accessToken = accessToken; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Adds the attribute with the specified name and {@link String} value to the attributes map. |
| 164 | + * |
| 165 | + * @param name the name of the attribute |
| 166 | + * @param value the value of the attribute |
| 167 | + * @return the {@link Builder} |
| 168 | + */ |
| 169 | + public Builder attribute(String name, String value) { |
| 170 | + Assert.hasText(name, "name cannot be empty"); |
| 171 | + Assert.hasText(value, "value cannot be empty"); |
| 172 | + this.attributes.put(name, value); |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * A {@code Consumer} of the attributes map allowing to access or modify its content. |
| 178 | + * |
| 179 | + * @param attributesConsumer a {@link Consumer} of the attributes map |
| 180 | + * @return the {@link Builder} |
| 181 | + */ |
| 182 | + public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) { |
| 183 | + attributesConsumer.accept(this.attributes); |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Builds a new {@link OAuth2Authorization}. |
| 189 | + * |
| 190 | + * @return the {@link OAuth2Authorization} |
| 191 | + */ |
| 192 | + public OAuth2Authorization build() { |
| 193 | + Assert.hasText(this.registeredClientId, "registeredClientId cannot be empty"); |
| 194 | + Assert.hasText(this.principalName, "principalName cannot be empty"); |
| 195 | + if (this.accessToken == null && this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()) == null) { |
| 196 | + throw new IllegalArgumentException("either accessToken has to be set or the authorization code with key '" |
| 197 | + + TokenType.AUTHORIZATION_CODE.getValue() + "' must be provided in the attributes map"); |
| 198 | + } |
| 199 | + return create(); |
| 200 | + } |
| 201 | + |
| 202 | + private OAuth2Authorization create() { |
| 203 | + OAuth2Authorization oAuth2Authorization = new OAuth2Authorization(); |
| 204 | + oAuth2Authorization.registeredClientId = this.registeredClientId; |
| 205 | + oAuth2Authorization.principalName = this.principalName; |
| 206 | + oAuth2Authorization.accessToken = this.accessToken; |
| 207 | + oAuth2Authorization.attributes = Collections.unmodifiableMap(this.attributes); |
| 208 | + return oAuth2Authorization; |
| 209 | + } |
| 210 | + } |
31 | 211 | }
|
0 commit comments