Skip to content

Commit 0355683

Browse files
Better redirect URI error handling and dependency upgrade (#633)
* Better error handling for redirect URIs * Update oauth2-oidc-sdk dependency * Address review comments Co-authored-by: Bogdan Gavril <[email protected]> --------- Co-authored-by: Bogdan Gavril <[email protected]>
1 parent d345e61 commit 0355683

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

msal4j-sdk/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<dependency>
3737
<groupId>com.nimbusds</groupId>
3838
<artifactId>oauth2-oidc-sdk</artifactId>
39-
<version>9.35</version>
39+
<version>10.7.1</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>net.minidev</groupId>

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/InteractiveRequest.java

+25-17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.net.InetAddress;
1111
import java.net.URI;
1212
import java.net.URL;
13+
import java.net.UnknownHostException;
1314
import java.security.SecureRandom;
1415
import java.util.Base64;
1516
import java.util.UUID;
@@ -55,24 +56,31 @@ URL authorizationUrl() {
5556
}
5657

5758
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
5871
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);
7684
}
7785
}
7886

0 commit comments

Comments
 (0)