Skip to content

Commit

Permalink
move selector to auth strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
p3dr0rv committed Jan 16, 2025
1 parent 6aabd68 commit fdabc57
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public static void fillBuilderWithBasicImplementations(
.context(activity.getApplicationContext())
.activity(activity)
.fragment(fragment)
.browserSelector(new AndroidBrowserSelector(context))
.build())
.stateGenerator(new AndroidTaskStateGenerator(activity.getTaskId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,14 @@ private AuthorizationResult performAuthorizationRequest(@NonNull final OAuth2Str
.getPlatformUtil()
.throwIfNetworkNotAvailable(parameters.isPowerOptCheckEnabled());

final Browser browser = parameters.getPlatformComponents().getBrowserSelector().selectBrowser(
parameters.getBrowserSafeList(),
parameters.getPreferredBrowser()
);
mAuthorizationStrategy = parameters.getPlatformComponents()
.getAuthorizationStrategyFactory()
.getAuthorizationStrategy(parameters.getAuthorizationAgent(),browser, false);
.getAuthorizationStrategy(
parameters.getAuthorizationAgent(),
parameters.getBrowserSafeList(),
parameters.getPreferredBrowser(),
false
);
mAuthorizationRequest = getAuthorizationRequest(strategy, parameters);

// Suppressing unchecked warnings due to casting of AuthorizationRequest to GenericAuthorizationRequest and AuthorizationStrategy to GenericAuthorizationStrategy in the arguments of call to requestAuthorization method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.microsoft.identity.common.java.browser.Browser;
import com.microsoft.identity.common.internal.ui.browser.DefaultBrowserAuthorizationStrategy;
import com.microsoft.identity.common.java.WarningType;
import com.microsoft.identity.common.java.browser.IBrowserSelector;
import com.microsoft.identity.common.java.configuration.LibraryConfiguration;
import com.microsoft.identity.common.java.providers.oauth2.IAuthorizationStrategy;
import com.microsoft.identity.common.internal.ui.webview.EmbeddedWebViewAuthorizationStrategy;
import com.microsoft.identity.common.java.ui.AuthorizationAgent;
import com.microsoft.identity.common.java.ui.BrowserDescriptor;
import com.microsoft.identity.common.logging.Logger;
import com.microsoft.identity.common.java.strategies.IAuthorizationStrategyFactory;


import java.util.List;

import edu.umd.cs.findbugs.annotations.Nullable;
import lombok.Builder;
import lombok.experimental.Accessors;

Expand All @@ -53,6 +57,7 @@ public class AndroidAuthorizationStrategyFactory implements IAuthorizationStrate
private final Context mContext;
private final Activity mActivity;
private final Fragment mFragment;
private final IBrowserSelector mBrowserSelector;

/**
* Get the authorization strategy based on the authorization agent and browser.
Expand All @@ -61,18 +66,22 @@ public class AndroidAuthorizationStrategyFactory implements IAuthorizationStrate
* Otherwise, return the browser authorization strategy.
*
* @param authorizationAgent The authorization agent provided by the caller.
* @param browser The browser to use for authorization.
* @param browserSafeList The browser safe list provided by the caller.
* @param preferredBrowserDescriptor The preferred browser descriptor provided by the caller.
* @param isBrokerRequest True if the request is from broker.
* @return The authorization strategy.
*/
@Override
@NonNull
public IAuthorizationStrategy getAuthorizationStrategy(
@NonNull final AuthorizationAgent authorizationAgent,
@Nullable final Browser browser,
@NonNull final List<BrowserDescriptor> browserSafeList,
@Nullable final BrowserDescriptor preferredBrowserDescriptor,
final boolean isBrokerRequest) {
final String methodTag = TAG + ":getAuthorizationStrategy";

final Browser browser = mBrowserSelector.selectBrowser(browserSafeList, preferredBrowserDescriptor);

if (authorizationAgent == AuthorizationAgent.WEBVIEW || browser == null) {
Logger.info(methodTag, "WebView authorization, browser: " + browser);
return getGenericAuthorizationStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public interface IBrowserSelector {
*/
@Nullable
Browser selectBrowser(
@NonNull List<BrowserDescriptor> browserSafeList,
@Nullable BrowserDescriptor preferredBrowserDescriptor);
@NonNull final List<BrowserDescriptor> browserSafeList,
@Nullable final BrowserDescriptor preferredBrowserDescriptor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public interface IPlatformComponents extends IPopManagerSupplier {
@NonNull
IStorageSupplier getStorageSupplier();

/**
* Returns a implementation of browser selector.
*/
@NonNull
IBrowserSelector getBrowserSelector();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import com.microsoft.identity.common.java.WarningType;
import com.microsoft.identity.common.java.browser.Browser;import com.microsoft.identity.common.java.providers.oauth2.IAuthorizationStrategy;
import com.microsoft.identity.common.java.ui.AuthorizationAgent;
import com.microsoft.identity.common.java.ui.BrowserDescriptor;

import java.util.List;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand All @@ -35,7 +38,8 @@ public interface IAuthorizationStrategyFactory<GenericAuthorizationStrategy exte

GenericAuthorizationStrategy getAuthorizationStrategy(
@NonNull final AuthorizationAgent authorizationAgent,
@Nullable final Browser browser,
@NonNull final List<BrowserDescriptor> browserSafeList,
@Nullable final BrowserDescriptor preferredBrowserDescriptor,
final boolean isBrokerRequest
);
}

0 comments on commit fdabc57

Please sign in to comment.