| | |
| | | |
| | | import javax.ws.rs.core.UriInfo; |
| | | import java.net.URI; |
| | | import java.net.URISyntaxException; |
| | | import java.util.Collection; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | |
| | | |
| | | public static Set<String> resolveValidRedirects(UriInfo uriInfo, String rootUrl, Set<String> validRedirects) { |
| | | // If the valid redirect URI is relative (no scheme, host, port) then use the request's scheme, host, and port |
| | | Set<String> resolveValidRedirects = new HashSet<String>(); |
| | | Set<String> resolveValidRedirects = new HashSet<>(); |
| | | for (String validRedirect : validRedirects) { |
| | | resolveValidRedirects.add(validRedirect); // add even relative urls. |
| | | if (validRedirect.startsWith("/")) { |
| | |
| | | private static Set<String> getValidateRedirectUris(UriInfo uriInfo, RealmModel realm) { |
| | | Set<String> redirects = new HashSet<>(); |
| | | for (ClientModel client : realm.getClients()) { |
| | | redirects.addAll(resolveValidRedirects(uriInfo, client.getRootUrl(), client.getRedirectUris())); |
| | | if (client.isEnabled()) { |
| | | redirects.addAll(resolveValidRedirects(uriInfo, client.getRootUrl(), client.getRedirectUris())); |
| | | } |
| | | } |
| | | return redirects; |
| | | } |
| | | |
| | | private static String verifyRedirectUri(UriInfo uriInfo, String rootUrl, String redirectUri, RealmModel realm, Set<String> validRedirects, boolean requireRedirectUri) { |
| | | |
| | | if (redirectUri != null) |
| | | redirectUri = normalizeUrl(redirectUri); |
| | | |
| | | if (redirectUri == null) { |
| | | if (!requireRedirectUri) { |
| | | redirectUri = getSingleValidRedirectUri(validRedirects); |
| | |
| | | return validRedirect; |
| | | } |
| | | |
| | | private static String normalizeUrl(String url) { |
| | | try { |
| | | URI uri = new URI(url); |
| | | return uri.normalize().toString(); |
| | | } catch (URISyntaxException e) { |
| | | throw new IllegalArgumentException("Invalid URL syntax: " + e.getMessage()); |
| | | } |
| | | } |
| | | } |