Skip to content

Commit 744d212

Browse files
committed
create snippets
1 parent 6178335 commit 744d212

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

common/src/main/java/com/microsoft/identity/common/adal/internal/AuthenticationConstants.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
// THE SOFTWARE.
2323
package com.microsoft.identity.common.adal.internal;
2424

25+
import android.net.Uri;
26+
27+
import androidx.annotation.Nullable;
2528
import androidx.annotation.VisibleForTesting;
2629

2730
import com.microsoft.identity.common.internal.logging.Logger;
2831
import com.microsoft.identity.common.java.broker.BrokerAccountDataName;
2932

3033
import java.nio.charset.Charset;
34+
import java.util.Set;
3135

3236
import lombok.AccessLevel;
3337
import lombok.AllArgsConstructor;
@@ -549,6 +553,23 @@ public static final class SWITCH_BROWSER {
549553
* String Query parameter key for the endpoint.
550554
*/
551555
public static final String ACTION = "action";
556+
557+
/**
558+
* Check if the request is to switch the browser.
559+
* <p>
560+
* The request is considered "switch_browser" if the URL contains
561+
* the action URI, code, and action parameters.
562+
*
563+
* @param uri The URI of the request.
564+
* @return True if the request contains the required parameters, false otherwise.
565+
*/
566+
public static boolean isSwitchBrowserRequest(@Nullable final Uri uri) {
567+
if (uri == null) {
568+
return false;
569+
}
570+
final Set<String> requiredParams = Set.of(ACTION_URI, CODE, ACTION);
571+
return uri.getQueryParameterNames().containsAll(requiredParams);
572+
}
552573
}
553574

554575
/**

common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/WebViewAuthorizationFragment.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,23 @@ public boolean launchWebBrowserIntent(@NonNull final Uri uri) {
476476
return false;
477477
}
478478

479+
public Uri constructSwithBrowserUri(@NonNull final String action_uri, @Nullable final HashMap<String, String> params) {
480+
final String[] paths = action_uri.split("/");
481+
final String authority = paths[0];
482+
final Uri.Builder uriBuilder = new Uri.Builder()
483+
.scheme("https")
484+
.encodedAuthority(authority);
485+
for (int i = 1; i < paths.length; i++) {
486+
uriBuilder.appendPath(paths[i]);
487+
}
488+
if (params != null) {
489+
for (HashMap.Entry<String, String> entry : params.entrySet()) {
490+
uriBuilder.appendQueryParameter(entry.getKey(), entry.getValue());
491+
}
492+
}
493+
return uriBuilder.build();
494+
}
495+
479496
/**
480497
* Helper method to check if the authorization request is being made through broker.
481498
* Done by checking for broker version key in the url

common/src/main/java/com/microsoft/identity/common/internal/ui/webview/AzureActiveDirectoryWebViewClient.java

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@
7070
import java.util.HashMap;
7171
import java.util.Locale;
7272
import java.util.Map;
73-
import java.util.Set;
7473

7574
import static com.microsoft.identity.common.adal.internal.AuthenticationConstants.Broker.AMAZON_APP_REDIRECT_PREFIX;
7675
import static com.microsoft.identity.common.adal.internal.AuthenticationConstants.Broker.COMPANY_PORTAL_APP_PACKAGE_NAME;
7776
import static com.microsoft.identity.common.adal.internal.AuthenticationConstants.Broker.IPPHONE_APP_PACKAGE_NAME;
7877
import static com.microsoft.identity.common.adal.internal.AuthenticationConstants.Broker.IPPHONE_APP_SIGNATURE;
7978
import static com.microsoft.identity.common.adal.internal.AuthenticationConstants.Broker.PLAY_STORE_INSTALL_PREFIX;
79+
import static com.microsoft.identity.common.adal.internal.AuthenticationConstants.SWITCH_BROWSER.isSwitchBrowserRequest;
8080
import static com.microsoft.identity.common.java.AuthenticationConstants.AAD.APP_LINK_KEY;
8181

8282
import io.opentelemetry.api.trace.SpanContext;
@@ -312,26 +312,6 @@ private boolean isHeaderForwardingRequiredUri(@NonNull final String url) {
312312
return urlIsTrustedToReceiveHeaders && originalRequestHasHeaders;
313313
}
314314

315-
/**
316-
* Check if the request is to switch the browser.
317-
* <p>
318-
* The request is considered "switch_browser" if the URL contains the action URI, code, and action parameters.
319-
*
320-
* @param uri The URI of the request.
321-
* @return True if the request contains the required parameters, false otherwise.
322-
*/
323-
private boolean isSwitchBrowserRequest(@Nullable final Uri uri) {
324-
final Set<String> requiredParams = Set.of(
325-
AuthenticationConstants.SWITCH_BROWSER.ACTION_URI,
326-
AuthenticationConstants.SWITCH_BROWSER.CODE,
327-
AuthenticationConstants.SWITCH_BROWSER.ACTION
328-
);
329-
if (uri == null) {
330-
return false;
331-
}
332-
return uri.getQueryParameterNames().containsAll(requiredParams);
333-
}
334-
335315
// This function is only called when the client received a redirect that starts with the apps
336316
// redirect uri.
337317
protected void processRedirectUrl(@NonNull final WebView view, @NonNull final String url) {
@@ -362,17 +342,11 @@ protected boolean processSwitchToBrowserRequest(@NonNull final Uri uri) {
362342
Logger.error(TAG, "Switch browser action URI or code is null. Cannot switch browser.", null);
363343
return false;
364344
}
365-
final String[] paths = action_uri.split("/");
366-
final String authority = paths[0];
367-
final Uri.Builder uriBuilder = new Uri.Builder()
368-
.scheme("https")
369-
.encodedAuthority(authority)
370-
.appendQueryParameter(AuthenticationConstants.SWITCH_BROWSER.CODE, code)
371-
.appendQueryParameter(AuthenticationConstants.OAuth2.REDIRECT_URI, mRedirectUrl);
372-
for (int i = 1; i < paths.length; i++) {
373-
uriBuilder.appendPath(paths[i]);
374-
}
375-
return fragment.launchWebBrowserIntent(uriBuilder.build());
345+
final HashMap<String, String> queryParams = new HashMap<>();
346+
queryParams.put(AuthenticationConstants.SWITCH_BROWSER.CODE, code);
347+
queryParams.put(AuthenticationConstants.OAuth2.REDIRECT_URI, mRedirectUrl);
348+
final Uri swithBrowserUri = fragment.constructSwithBrowserUri(action_uri, queryParams);
349+
return fragment.launchWebBrowserIntent(swithBrowserUri);
376350
}
377351

378352
private void processWebsiteRequest(@NonNull final WebView view, @NonNull final String url) {

0 commit comments

Comments
 (0)