|
10 | 10 | import java.net.InetAddress;
|
11 | 11 | import java.net.URI;
|
12 | 12 | import java.net.URL;
|
| 13 | +import java.net.UnknownHostException; |
13 | 14 | import java.security.SecureRandom;
|
14 | 15 | import java.util.Base64;
|
15 | 16 | import java.util.UUID;
|
@@ -55,24 +56,31 @@ URL authorizationUrl() {
|
55 | 56 | }
|
56 | 57 |
|
57 | 58 | private void validateRedirectUrl(URI redirectUri) {
|
| 59 | + String host = redirectUri.getHost(); |
| 60 | + String scheme = redirectUri.getScheme(); |
| 61 | + InetAddress address; |
| 62 | + |
| 63 | + //Validate URI scheme. Only http is valid, as determined by the HttpListener created in AcquireTokenByInteractiveFlowSupplier.startHttpListener() |
| 64 | + if (scheme == null || !scheme.equals("http")) { |
| 65 | + throw new MsalClientException(String.format( |
| 66 | + "Only http://localhost or http://localhost:port is supported for the redirect URI of an interactive request using a browser, but \"%s\" was found. For more information about redirect URI formats, see https://aka.ms/msal4j-interactive-request", scheme), |
| 67 | + AuthenticationErrorCode.LOOPBACK_REDIRECT_URI); |
| 68 | + } |
| 69 | + |
| 70 | + //Ensure that the given redirect URI has a known address |
58 | 71 | try {
|
59 |
| - if (!InetAddress.getByName(redirectUri.getHost()).isLoopbackAddress()) { |
60 |
| - throw new MsalClientException(String.format( |
61 |
| - "Only loopback redirect uri is supported, but %s was found " + |
62 |
| - "Configure http://localhost or http://localhost:port both during app registration" + |
63 |
| - "and when you create the create the InteractiveRequestParameters object", redirectUri.getHost()), |
64 |
| - AuthenticationErrorCode.LOOPBACK_REDIRECT_URI); |
65 |
| - } |
66 |
| - |
67 |
| - if (!redirectUri.getScheme().equals("http")) { |
68 |
| - throw new MsalClientException(String.format( |
69 |
| - "Only http uri scheme is supported but %s was found. Configure http://localhost" + |
70 |
| - "or http://localhost:port both during app registration and when you create" + |
71 |
| - " the create the InteractiveRequestParameters object", redirectUri.toString()), |
72 |
| - AuthenticationErrorCode.LOOPBACK_REDIRECT_URI); |
73 |
| - } |
74 |
| - } catch (Exception exception) { |
75 |
| - throw new MsalClientException(exception); |
| 72 | + address = InetAddress.getByName(host); |
| 73 | + } catch (UnknownHostException e) { |
| 74 | + throw new MsalClientException(String.format( |
| 75 | + "Unknown host exception for host \"%s\". For more information about redirect URI formats, see https://aka.ms/msal4j-interactive-request", host), |
| 76 | + AuthenticationErrorCode.LOOPBACK_REDIRECT_URI); |
| 77 | + } |
| 78 | + |
| 79 | + //Ensure that the redirect URI is considered a loopback address |
| 80 | + if (address == null || !address.isLoopbackAddress()) { |
| 81 | + throw new MsalClientException( |
| 82 | + "Only loopback redirect URI is supported for interactive requests. For more information about redirect URI formats, see https://aka.ms/msal4j-interactive-request", |
| 83 | + AuthenticationErrorCode.LOOPBACK_REDIRECT_URI); |
76 | 84 | }
|
77 | 85 | }
|
78 | 86 |
|
|
0 commit comments