1 package org.springframework.security.oauth2.provider;
2
3 import java.io.Serializable;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.springframework.security.core.GrantedAuthority;
11 import org.springframework.security.oauth2.common.util.OAuth2Utils;
12
13
14
15
16
17
18
19
20
21
22 public class OAuth2Request extends BaseRequest implements Serializable {
23
24 private static final long serialVersionUID = 1L;
25
26
27
28
29 private Set<String> resourceIds = new HashSet<String>();
30
31
32
33
34 private Collection<? extends GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
35
36
37
38
39
40 private boolean approved = false;
41
42
43
44
45
46 private TokenRequest refresh = null;
47
48
49
50
51
52
53 private String redirectUri;
54
55
56
57
58
59 private Set<String> responseTypes = new HashSet<String>();
60
61
62
63
64
65 private Map<String, Serializable> extensions = new HashMap<String, Serializable>();
66
67 public OAuth2Request(Map<String, String> requestParameters, String clientId,
68 Collection<? extends GrantedAuthority> authorities, boolean approved, Set<String> scope,
69 Set<String> resourceIds, String redirectUri, Set<String> responseTypes,
70 Map<String, Serializable> extensionProperties) {
71 setClientId(clientId);
72 setRequestParameters(requestParameters);
73 setScope(scope);
74 if (resourceIds != null) {
75 this.resourceIds = new HashSet<String>(resourceIds);
76 }
77 if (authorities != null) {
78 this.authorities = new HashSet<GrantedAuthority>(authorities);
79 }
80 this.approved = approved;
81 if (responseTypes != null) {
82 this.responseTypes = new HashSet<String>(responseTypes);
83 }
84 this.redirectUri = redirectUri;
85 if (extensionProperties != null) {
86 this.extensions = extensionProperties;
87 }
88 }
89
90 protected OAuth2Requestf="../../../../../org/springframework/security/oauth2/provider/OAuth2Request.html#OAuth2Request">OAuth2Request(OAuth2Request other) {
91 this(other.getRequestParameters(), other.getClientId(), other.getAuthorities(), other.isApproved(), other
92 .getScope(), other.getResourceIds(), other.getRedirectUri(), other.getResponseTypes(), other
93 .getExtensions());
94 }
95
96 protected OAuth2Request(String clientId) {
97 setClientId(clientId);
98 }
99
100 protected OAuth2Request() {
101 super();
102 }
103
104 public String getRedirectUri() {
105 return redirectUri;
106 }
107
108 public Set<String> getResponseTypes() {
109 return responseTypes;
110 }
111
112 public Collection<? extends GrantedAuthority> getAuthorities() {
113 return authorities;
114 }
115
116 public boolean isApproved() {
117 return approved;
118 }
119
120 public Set<String> getResourceIds() {
121 return resourceIds;
122 }
123
124 public Map<String, Serializable> getExtensions() {
125 return extensions;
126 }
127
128
129
130
131
132
133 public OAuth2Request createOAuth2Request(Map<String, String> parameters) {
134 return new OAuth2Request(parameters, getClientId(), authorities, approved, getScope(), resourceIds,
135 redirectUri, responseTypes, extensions);
136 }
137
138
139
140
141
142
143
144
145 public OAuth2Request narrowScope(Set<String> scope) {
146 OAuth2Request request = new OAuth2Request(getRequestParameters(), getClientId(), authorities, approved, scope,
147 resourceIds, redirectUri, responseTypes, extensions);
148 request.refresh = this.refresh;
149 return request;
150 }
151
152 public OAuth2Request refresh(TokenRequest tokenRequest) {
153 OAuth2Request request = new OAuth2Request(getRequestParameters(), getClientId(), authorities, approved,
154 getScope(), resourceIds, redirectUri, responseTypes, extensions);
155 request.refresh = tokenRequest;
156 return request;
157 }
158
159
160
161
162 public boolean isRefresh() {
163 return refresh != null;
164 }
165
166
167
168
169
170
171
172 public TokenRequest getRefreshTokenRequest() {
173 return refresh;
174 }
175
176
177
178
179
180
181 public String getGrantType() {
182 if (getRequestParameters().containsKey(OAuth2Utils.GRANT_TYPE)) {
183 return getRequestParameters().get(OAuth2Utils.GRANT_TYPE);
184 }
185 if (getRequestParameters().containsKey(OAuth2Utils.RESPONSE_TYPE)) {
186 String response = getRequestParameters().get(OAuth2Utils.RESPONSE_TYPE);
187 if (response.contains("token")) {
188 return "implicit";
189 }
190 }
191 return null;
192 }
193
194 @Override
195 public int hashCode() {
196 final int prime = 31;
197 int result = super.hashCode();
198 result = prime * result + (approved ? 1231 : 1237);
199 result = prime * result + ((authorities == null) ? 0 : authorities.hashCode());
200 result = prime * result + ((extensions == null) ? 0 : extensions.hashCode());
201 result = prime * result + ((redirectUri == null) ? 0 : redirectUri.hashCode());
202 result = prime * result + ((resourceIds == null) ? 0 : resourceIds.hashCode());
203 result = prime * result + ((responseTypes == null) ? 0 : responseTypes.hashCode());
204 return result;
205 }
206
207 @Override
208 public boolean equals(Object obj) {
209 if (this == obj)
210 return true;
211 if (!super.equals(obj))
212 return false;
213 if (getClass() != obj.getClass())
214 return false;
215 OAuth2Request other = (OAuth2Request) obj;
216 if (approved != other.approved)
217 return false;
218 if (authorities == null) {
219 if (other.authorities != null)
220 return false;
221 }
222 else if (!authorities.equals(other.authorities))
223 return false;
224 if (extensions == null) {
225 if (other.extensions != null)
226 return false;
227 }
228 else if (!extensions.equals(other.extensions))
229 return false;
230 if (redirectUri == null) {
231 if (other.redirectUri != null)
232 return false;
233 }
234 else if (!redirectUri.equals(other.redirectUri))
235 return false;
236 if (resourceIds == null) {
237 if (other.resourceIds != null)
238 return false;
239 }
240 else if (!resourceIds.equals(other.resourceIds))
241 return false;
242 if (responseTypes == null) {
243 if (other.responseTypes != null)
244 return false;
245 }
246 else if (!responseTypes.equals(other.responseTypes))
247 return false;
248 return true;
249 }
250
251 }