1 package org.springframework.security.oauth2.provider.client;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.LinkedHashMap;
9 import java.util.LinkedHashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.springframework.security.core.GrantedAuthority;
15 import org.springframework.security.core.authority.AuthorityUtils;
16 import org.springframework.security.oauth2.provider.ClientDetails;
17 import org.springframework.util.StringUtils;
18
19
20
21
22
23
24
25
26 @SuppressWarnings("serial")
27 @com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_DEFAULT)
28 @com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)
29 public class BaseClientDetails implements ClientDetails {
30
31 @com.fasterxml.jackson.annotation.JsonProperty("client_id")
32 private String clientId;
33
34 @com.fasterxml.jackson.annotation.JsonProperty("client_secret")
35 private String clientSecret;
36
37 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
38 private Set<String> scope = Collections.emptySet();
39
40 @com.fasterxml.jackson.annotation.JsonProperty("resource_ids")
41 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
42 private Set<String> resourceIds = Collections.emptySet();
43
44 @com.fasterxml.jackson.annotation.JsonProperty("authorized_grant_types")
45 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
46 private Set<String> authorizedGrantTypes = Collections.emptySet();
47
48 @com.fasterxml.jackson.annotation.JsonProperty("redirect_uri")
49 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
50 private Set<String> registeredRedirectUris;
51
52 @com.fasterxml.jackson.annotation.JsonProperty("autoapprove")
53 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
54 private Set<String> autoApproveScopes;
55
56 private List<GrantedAuthority> authorities = Collections.emptyList();
57
58 @com.fasterxml.jackson.annotation.JsonProperty("access_token_validity")
59 private Integer accessTokenValiditySeconds;
60
61 @com.fasterxml.jackson.annotation.JsonProperty("refresh_token_validity")
62 private Integer refreshTokenValiditySeconds;
63
64 @com.fasterxml.jackson.annotation.JsonIgnore
65 private Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();
66
67 public BaseClientDetails() {
68 }
69
70 public BaseClientDetails(ClientDetails prototype) {
71 this();
72 setAccessTokenValiditySeconds(prototype.getAccessTokenValiditySeconds());
73 setRefreshTokenValiditySeconds(prototype
74 .getRefreshTokenValiditySeconds());
75 setAuthorities(prototype.getAuthorities());
76 setAuthorizedGrantTypes(prototype.getAuthorizedGrantTypes());
77 setClientId(prototype.getClientId());
78 setClientSecret(prototype.getClientSecret());
79 setRegisteredRedirectUri(prototype.getRegisteredRedirectUri());
80 setScope(prototype.getScope());
81 setResourceIds(prototype.getResourceIds());
82 }
83
84 public BaseClientDetails(String clientId, String resourceIds,
85 String scopes, String grantTypes, String authorities) {
86 this(clientId, resourceIds, scopes, grantTypes, authorities, null);
87 }
88
89 public BaseClientDetails(String clientId, String resourceIds,
90 String scopes, String grantTypes, String authorities,
91 String redirectUris) {
92
93 this.clientId = clientId;
94
95 if (StringUtils.hasText(resourceIds)) {
96 Set<String> resources = StringUtils
97 .commaDelimitedListToSet(resourceIds);
98 if (!resources.isEmpty()) {
99 this.resourceIds = resources;
100 }
101 }
102
103 if (StringUtils.hasText(scopes)) {
104 Set<String> scopeList = StringUtils.commaDelimitedListToSet(scopes);
105 if (!scopeList.isEmpty()) {
106 this.scope = scopeList;
107 }
108 }
109
110 if (StringUtils.hasText(grantTypes)) {
111 this.authorizedGrantTypes = StringUtils
112 .commaDelimitedListToSet(grantTypes);
113 } else {
114 this.authorizedGrantTypes = new HashSet<String>(Arrays.asList(
115 "authorization_code", "refresh_token"));
116 }
117
118 if (StringUtils.hasText(authorities)) {
119 this.authorities = AuthorityUtils
120 .commaSeparatedStringToAuthorityList(authorities);
121 }
122
123 if (StringUtils.hasText(redirectUris)) {
124 this.registeredRedirectUris = StringUtils
125 .commaDelimitedListToSet(redirectUris);
126 }
127 }
128
129 @com.fasterxml.jackson.annotation.JsonIgnore
130 public String getClientId() {
131 return clientId;
132 }
133
134 public void setClientId(String clientId) {
135 this.clientId = clientId;
136 }
137
138 public void setAutoApproveScopes(Collection<String> autoApproveScopes) {
139 this.autoApproveScopes = new HashSet<String>(autoApproveScopes);
140 }
141
142 @Override
143 public boolean isAutoApprove(String scope) {
144 if (autoApproveScopes == null) {
145 return false;
146 }
147 for (String auto : autoApproveScopes) {
148 if (auto.equals("true") || scope.matches(auto)) {
149 return true;
150 }
151 }
152 return false;
153 }
154
155 @com.fasterxml.jackson.annotation.JsonIgnore
156 public Set<String> getAutoApproveScopes() {
157 return autoApproveScopes;
158 }
159
160 @com.fasterxml.jackson.annotation.JsonIgnore
161 public boolean isSecretRequired() {
162 return this.clientSecret != null;
163 }
164
165 @com.fasterxml.jackson.annotation.JsonIgnore
166 public String getClientSecret() {
167 return clientSecret;
168 }
169
170 public void setClientSecret(String clientSecret) {
171 this.clientSecret = clientSecret;
172 }
173
174 @com.fasterxml.jackson.annotation.JsonIgnore
175 public boolean isScoped() {
176 return this.scope != null && !this.scope.isEmpty();
177 }
178
179 public Set<String> getScope() {
180 return scope;
181 }
182
183 public void setScope(Collection<String> scope) {
184 this.scope = scope == null ? Collections.<String> emptySet()
185 : new LinkedHashSet<String>(scope);
186 }
187
188 @com.fasterxml.jackson.annotation.JsonIgnore
189 public Set<String> getResourceIds() {
190 return resourceIds;
191 }
192
193 public void setResourceIds(Collection<String> resourceIds) {
194 this.resourceIds = resourceIds == null ? Collections
195 .<String> emptySet() : new LinkedHashSet<String>(resourceIds);
196 }
197
198 @com.fasterxml.jackson.annotation.JsonIgnore
199 public Set<String> getAuthorizedGrantTypes() {
200 return authorizedGrantTypes;
201 }
202
203 public void setAuthorizedGrantTypes(Collection<String> authorizedGrantTypes) {
204 this.authorizedGrantTypes = new LinkedHashSet<String>(
205 authorizedGrantTypes);
206 }
207
208 @com.fasterxml.jackson.annotation.JsonIgnore
209 public Set<String> getRegisteredRedirectUri() {
210 return registeredRedirectUris;
211 }
212
213 public void setRegisteredRedirectUri(Set<String> registeredRedirectUris) {
214 this.registeredRedirectUris = registeredRedirectUris == null ? null
215 : new LinkedHashSet<String>(registeredRedirectUris);
216 }
217
218 @com.fasterxml.jackson.annotation.JsonProperty("authorities")
219 private List<String> getAuthoritiesAsStrings() {
220 return new ArrayList<String>(
221 AuthorityUtils.authorityListToSet(authorities));
222 }
223
224 @com.fasterxml.jackson.annotation.JsonProperty("authorities")
225 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = Jackson2ArrayOrStringDeserializer.class)
226 private void setAuthoritiesAsStrings(Set<String> values) {
227 setAuthorities(AuthorityUtils.createAuthorityList(values
228 .toArray(new String[values.size()])));
229 }
230
231 @com.fasterxml.jackson.annotation.JsonIgnore
232 public Collection<GrantedAuthority> getAuthorities() {
233 return authorities;
234 }
235
236 @com.fasterxml.jackson.annotation.JsonIgnore
237 public void setAuthorities(
238 Collection<? extends GrantedAuthority> authorities) {
239 this.authorities = new ArrayList<GrantedAuthority>(authorities);
240 }
241
242 @com.fasterxml.jackson.annotation.JsonIgnore
243 public Integer getAccessTokenValiditySeconds() {
244 return accessTokenValiditySeconds;
245 }
246
247 public void setAccessTokenValiditySeconds(Integer accessTokenValiditySeconds) {
248 this.accessTokenValiditySeconds = accessTokenValiditySeconds;
249 }
250
251 @com.fasterxml.jackson.annotation.JsonIgnore
252 public Integer getRefreshTokenValiditySeconds() {
253 return refreshTokenValiditySeconds;
254 }
255
256 public void setRefreshTokenValiditySeconds(
257 Integer refreshTokenValiditySeconds) {
258 this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
259 }
260
261 public void setAdditionalInformation(Map<String, ?> additionalInformation) {
262 this.additionalInformation = new LinkedHashMap<String, Object>(
263 additionalInformation);
264 }
265
266 @com.fasterxml.jackson.annotation.JsonAnyGetter
267 public Map<String, Object> getAdditionalInformation() {
268 return Collections.unmodifiableMap(this.additionalInformation);
269 }
270
271 @com.fasterxml.jackson.annotation.JsonAnySetter
272 public void addAdditionalInformation(String key, Object value) {
273 this.additionalInformation.put(key, value);
274 }
275
276 @Override
277 public int hashCode() {
278 final int prime = 31;
279 int result = 1;
280 result = prime
281 * result
282 + ((accessTokenValiditySeconds == null) ? 0
283 : accessTokenValiditySeconds);
284 result = prime
285 * result
286 + ((refreshTokenValiditySeconds == null) ? 0
287 : refreshTokenValiditySeconds);
288 result = prime * result
289 + ((authorities == null) ? 0 : authorities.hashCode());
290 result = prime
291 * result
292 + ((authorizedGrantTypes == null) ? 0 : authorizedGrantTypes
293 .hashCode());
294 result = prime * result
295 + ((clientId == null) ? 0 : clientId.hashCode());
296 result = prime * result
297 + ((clientSecret == null) ? 0 : clientSecret.hashCode());
298 result = prime
299 * result
300 + ((registeredRedirectUris == null) ? 0
301 : registeredRedirectUris.hashCode());
302 result = prime * result
303 + ((resourceIds == null) ? 0 : resourceIds.hashCode());
304 result = prime * result + ((scope == null) ? 0 : scope.hashCode());
305 result = prime * result + ((additionalInformation == null) ? 0 : additionalInformation.hashCode());
306 return result;
307 }
308
309 @Override
310 public boolean equals(Object obj) {
311 if (this == obj)
312 return true;
313 if (obj == null)
314 return false;
315 if (getClass() != obj.getClass())
316 return false;
317 BaseClientDetails other = (BaseClientDetails) obj;
318 if (accessTokenValiditySeconds == null) {
319 if (other.accessTokenValiditySeconds != null)
320 return false;
321 } else if (!accessTokenValiditySeconds.equals(other.accessTokenValiditySeconds))
322 return false;
323 if (refreshTokenValiditySeconds == null) {
324 if (other.refreshTokenValiditySeconds != null)
325 return false;
326 } else if (!refreshTokenValiditySeconds.equals(other.refreshTokenValiditySeconds))
327 return false;
328 if (authorities == null) {
329 if (other.authorities != null)
330 return false;
331 } else if (!authorities.equals(other.authorities))
332 return false;
333 if (authorizedGrantTypes == null) {
334 if (other.authorizedGrantTypes != null)
335 return false;
336 } else if (!authorizedGrantTypes.equals(other.authorizedGrantTypes))
337 return false;
338 if (clientId == null) {
339 if (other.clientId != null)
340 return false;
341 } else if (!clientId.equals(other.clientId))
342 return false;
343 if (clientSecret == null) {
344 if (other.clientSecret != null)
345 return false;
346 } else if (!clientSecret.equals(other.clientSecret))
347 return false;
348 if (registeredRedirectUris == null) {
349 if (other.registeredRedirectUris != null)
350 return false;
351 } else if (!registeredRedirectUris.equals(other.registeredRedirectUris))
352 return false;
353 if (resourceIds == null) {
354 if (other.resourceIds != null)
355 return false;
356 } else if (!resourceIds.equals(other.resourceIds))
357 return false;
358 if (scope == null) {
359 if (other.scope != null)
360 return false;
361 } else if (!scope.equals(other.scope))
362 return false;
363 if (additionalInformation == null) {
364 if (other.additionalInformation != null)
365 return false;
366 } else if (!additionalInformation.equals(other.additionalInformation))
367 return false;
368 return true;
369 }
370
371 @Override
372 public String toString() {
373 return "BaseClientDetails [clientId=" + clientId + ", clientSecret="
374 + clientSecret + ", scope=" + scope + ", resourceIds="
375 + resourceIds + ", authorizedGrantTypes="
376 + authorizedGrantTypes + ", registeredRedirectUris="
377 + registeredRedirectUris + ", authorities=" + authorities
378 + ", accessTokenValiditySeconds=" + accessTokenValiditySeconds
379 + ", refreshTokenValiditySeconds="
380 + refreshTokenValiditySeconds + ", additionalInformation="
381 + additionalInformation + "]";
382 }
383
384 }