Skip to content

Commit e16f558

Browse files
author
Dave Syer
committed
Merge SubdomainRedirectResolver into DefaultRedirectResolver
1 parent 4087fe6 commit e16f558

File tree

3 files changed

+40
-61
lines changed

3 files changed

+40
-61
lines changed

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/endpoint/DefaultRedirectResolver.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,28 @@
3131

3232
/**
3333
* Default implementation for a redirect resolver.
34-
*
34+
*
3535
* @author Ryan Heaton
3636
* @author Dave Syer
3737
*/
3838
public class DefaultRedirectResolver implements RedirectResolver {
3939

4040
private Collection<String> redirectGrantTypes = Arrays.asList("implicit", "authorization_code");
4141

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+
4253
/**
4354
* Grant types that are permitted to have a redirect uri.
44-
*
55+
*
4556
* @param redirectGrantTypes the redirect grant types to set
4657
*/
4758
public void setRedirectGrantTypes(Collection<String> redirectGrantTypes) {
@@ -55,7 +66,8 @@ public String resolveRedirect(String requestedRedirect, ClientDetails client) th
5566
throw new InvalidGrantException("A client must have at least one authorized grant type.");
5667
}
5768
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.");
5971
}
6072

6173
Set<String> redirectUris = client.getRegisteredRedirectUri();
@@ -91,7 +103,7 @@ private boolean containsRedirectGrantType(Set<String> grantTypes) {
91103
* it is an HTTP URL.
92104
* <p>
93105
* For other (non-URL) cases, such as for some implicit clients, the redirect_uri must be an exact match.
94-
*
106+
*
95107
* @param requestedRedirect The requested redirect URI.
96108
* @param redirectUri The registered redirect URI.
97109
* @return Whether the requested redirect URI "matches" the specified redirect URI.
@@ -101,18 +113,32 @@ protected boolean redirectMatches(String requestedRedirect, String redirectUri)
101113
URL req = new URL(requestedRedirect);
102114
URL reg = new URL(redirectUri);
103115

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());
106118
}
107119
}
108120
catch (MalformedURLException e) {
109121
}
110122
return requestedRedirect.equals(redirectUri);
111123
}
112124

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+
113139
/**
114140
* Attempt to match one of the registered URIs to the that of the requested one.
115-
*
141+
*
116142
* @param redirectUris the set of the registered URIs to try and find a match. This cannot be null or empty.
117143
* @param requestedRedirect the URI used as part of the request
118144
* @return the matching URI

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/endpoint/SubdomainRedirectResolver.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/provider/code/TestSubdomainRedirectResolver.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import java.util.Set;
99

1010
import org.junit.Test;
11+
import org.springframework.security.oauth2.common.exceptions.RedirectMismatchException;
1112
import org.springframework.security.oauth2.provider.BaseClientDetails;
13+
import org.springframework.security.oauth2.provider.endpoint.DefaultRedirectResolver;
1214

1315
public class TestSubdomainRedirectResolver
1416
{
15-
private final TestSubdomainRedirectResolver resolver = new TestSubdomainRedirectResolver();
17+
private final DefaultRedirectResolver resolver = new DefaultRedirectResolver();
1618
private final BaseClientDetails client = new BaseClientDetails();
1719

1820
{
@@ -23,18 +25,19 @@ public class TestSubdomainRedirectResolver
2325
@Test
2426
public void testRedirectWatchdox() throws Exception
2527
{
26-
Set<String> redirectUris = new HashSet<String>(Arrays.asList("watchdox.com"));
28+
Set<String> redirectUris = new HashSet<String>(Arrays.asList("http://watchdox.com"));
2729
client.setRegisteredRedirectUri(redirectUris);
2830
String requestedRedirect = "http://anywhere.watchdox.com/something";
2931
assertEquals(requestedRedirect, resolver.resolveRedirect(requestedRedirect, client));
3032
}
3133

32-
@Test
34+
@Test(expected=RedirectMismatchException.class)
3335
public void testRedirectBadWatchdox() throws Exception
3436
{
35-
Set<String> redirectUris = new HashSet<String>(Arrays.asList("http.*(watchdox.com).*"));
37+
Set<String> redirectUris = new HashSet<String>(Arrays.asList("http//watchdox.com"));
3638
client.setRegisteredRedirectUri(redirectUris);
3739
String requestedRedirect = "http://anywhere.google.com/something";
3840
assertEquals(requestedRedirect, resolver.resolveRedirect(requestedRedirect, client));
3941
}
42+
4043
}

0 commit comments

Comments
 (0)