31
31
32
32
/**
33
33
* Default implementation for a redirect resolver.
34
- *
34
+ *
35
35
* @author Ryan Heaton
36
36
* @author Dave Syer
37
37
*/
38
38
public class DefaultRedirectResolver implements RedirectResolver {
39
39
40
40
private Collection <String > redirectGrantTypes = Arrays .asList ("implicit" , "authorization_code" );
41
41
42
+ private boolean matchSubdomains = true ;
43
+
44
+ /**
45
+ * Flag to indicate that requested URIs will match if they are a subdomain of the registered value.
46
+ *
47
+ * @param matchSubdomains the flag value to set (deafult true)
48
+ */
49
+ public void setMatchSubdomains (boolean matchSubdomains ) {
50
+ this .matchSubdomains = matchSubdomains ;
51
+ }
52
+
42
53
/**
43
54
* Grant types that are permitted to have a redirect uri.
44
- *
55
+ *
45
56
* @param redirectGrantTypes the redirect grant types to set
46
57
*/
47
58
public void setRedirectGrantTypes (Collection <String > redirectGrantTypes ) {
@@ -55,7 +66,8 @@ public String resolveRedirect(String requestedRedirect, ClientDetails client) th
55
66
throw new InvalidGrantException ("A client must have at least one authorized grant type." );
56
67
}
57
68
if (!containsRedirectGrantType (authorizedGrantTypes )) {
58
- throw new InvalidGrantException ("A redirect_uri can only be used by implicit or authorization_code grant types." );
69
+ throw new InvalidGrantException (
70
+ "A redirect_uri can only be used by implicit or authorization_code grant types." );
59
71
}
60
72
61
73
Set <String > redirectUris = client .getRegisteredRedirectUri ();
@@ -91,7 +103,7 @@ private boolean containsRedirectGrantType(Set<String> grantTypes) {
91
103
* it is an HTTP URL.
92
104
* <p>
93
105
* For other (non-URL) cases, such as for some implicit clients, the redirect_uri must be an exact match.
94
- *
106
+ *
95
107
* @param requestedRedirect The requested redirect URI.
96
108
* @param redirectUri The registered redirect URI.
97
109
* @return Whether the requested redirect URI "matches" the specified redirect URI.
@@ -101,18 +113,32 @@ protected boolean redirectMatches(String requestedRedirect, String redirectUri)
101
113
URL req = new URL (requestedRedirect );
102
114
URL reg = new URL (redirectUri );
103
115
104
- if (reg .getProtocol ().equals (req .getProtocol ()) && reg .getHost (). equals ( req .getHost ())) {
105
- return requestedRedirect . startsWith (redirectUri );
116
+ if (reg .getProtocol ().equals (req .getProtocol ()) && hostMatches ( reg .getHost (), req .getHost ())) {
117
+ return req . getPath (). startsWith (reg . getPath () );
106
118
}
107
119
}
108
120
catch (MalformedURLException e ) {
109
121
}
110
122
return requestedRedirect .equals (redirectUri );
111
123
}
112
124
125
+ /**
126
+ * Check if host matches the registered value.
127
+ *
128
+ * @param registered the registered host
129
+ * @param requested the requested host
130
+ * @return true if they match
131
+ */
132
+ protected boolean hostMatches (String registered , String requested ) {
133
+ if (matchSubdomains ) {
134
+ return requested .endsWith (registered );
135
+ }
136
+ return registered .equals (requested );
137
+ }
138
+
113
139
/**
114
140
* Attempt to match one of the registered URIs to the that of the requested one.
115
- *
141
+ *
116
142
* @param redirectUris the set of the registered URIs to try and find a match. This cannot be null or empty.
117
143
* @param requestedRedirect the URI used as part of the request
118
144
* @return the matching URI
0 commit comments