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