Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flutter_appauth/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
<activity android:name="net.openid.appauth.RedirectUriReceiverActivity"
android:theme="@style/Theme.AppCompat.Translucent.NoTitleBar"
tools:replace="android:theme" />
<activity android:name="AppAuthIntentLauncherActivity"
android:theme="@style/Theme.AppCompat.Translucent.NoTitleBar"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.crossingthestreams.flutterappauth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class AppAuthIntentLauncherActivity extends Activity {
static class IntentExtraKey {
static String INTENT = "intent";
static String REQUEST_CODE = "requestCode";
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent().getParcelableExtra(IntentExtraKey.INTENT);
if (intent != null) {
int requestCode = getIntent().getIntExtra(IntentExtraKey.REQUEST_CODE, 0);
startActivityForResult(intent, requestCode);
} else {
finish();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

finish();

FlutterAppauthPlugin flutterAppauthPlugin = FlutterAppauthPlugin.getInstance();
if (flutterAppauthPlugin != null) {
flutterAppauthPlugin.onActivityResult(requestCode, resultCode, data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public class FlutterAppauthPlugin
private AuthorizationService defaultAuthorizationService;
private AuthorizationService insecureAuthorizationService;

private static FlutterAppauthPlugin instance = null;

@Nullable static FlutterAppauthPlugin getInstance() {
return instance;
}

private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) {
this.applicationContext = context;
createAuthorizationServices();
Expand All @@ -104,11 +110,13 @@ private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
instance = this;
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
disposeAuthorizationServices();
instance = null;
}

@Override
Expand Down Expand Up @@ -466,15 +474,21 @@ private void performAuthorization(
try {
Intent authIntent =
authorizationService.getAuthorizationRequestIntent(authRequestBuilder.build());
mainActivity.startActivityForResult(
authIntent, exchangeCode ? RC_AUTH_EXCHANGE_CODE : RC_AUTH);
startIntentLauncherActivityForResult(authIntent, exchangeCode ? RC_AUTH_EXCHANGE_CODE : RC_AUTH);
} catch (ActivityNotFoundException ex) {
finishWithError(NO_BROWSER_AVAILABLE_ERROR_CODE, NO_BROWSER_AVAILABLE_ERROR_FORMAT, ex);
} catch (NullPointerException ex) {
finishWithError(NULL_ACTIVITY_ERROR_CODE, NULL_ACTIVITY_ERROR_FORMAT, ex);
}
}

private void startIntentLauncherActivityForResult(Intent intent, int requestCode) {
Intent launcherIntent = new Intent(mainActivity, AppAuthIntentLauncherActivity.class);
launcherIntent.putExtra(AppAuthIntentLauncherActivity.IntentExtraKey.INTENT, intent);
launcherIntent.putExtra(AppAuthIntentLauncherActivity.IntentExtraKey.REQUEST_CODE, requestCode);
mainActivity.startActivity(launcherIntent);
}

private void performTokenRequest(
AuthorizationServiceConfiguration serviceConfiguration,
TokenRequestParameters tokenRequestParameters) {
Expand Down Expand Up @@ -584,7 +598,7 @@ private void performEndSessionRequest(
Intent endSessionIntent = authorizationService.getEndSessionRequestIntent(endSessionRequest);

try {
mainActivity.startActivityForResult(endSessionIntent, RC_END_SESSION);
startIntentLauncherActivityForResult(endSessionIntent, RC_END_SESSION);
} catch (NullPointerException ex) {
finishWithError(NULL_ACTIVITY_ERROR_CODE, NULL_ACTIVITY_ERROR_FORMAT, ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ @implementation AppAuthIOSAuthorization
codeChallenge:codeChallenge
codeChallengeMethod:OIDOAuthorizationRequestCodeChallengeMethodS256
additionalParameters:additionalParameters];
UIViewController *rootViewController = [self rootViewController];
UIViewController *presentingViewController = [self topMostViewController];
if (exchangeCode) {
id<OIDExternalUserAgent> agent =
[self userAgentWithViewController:rootViewController
[self userAgentWithViewController:presentingViewController
externalUserAgent:externalUserAgent];
return [OIDAuthState
authStateByPresentingAuthorizationRequest:request
Expand Down Expand Up @@ -67,7 +67,7 @@ @implementation AppAuthIOSAuthorization
}];
} else {
id<OIDExternalUserAgent> agent =
[self userAgentWithViewController:rootViewController
[self userAgentWithViewController:presentingViewController
externalUserAgent:externalUserAgent];
return [OIDAuthorizationService
presentAuthorizationRequest:request
Expand Down Expand Up @@ -133,9 +133,9 @@ @implementation AppAuthIOSAuthorization
postLogoutRedirectURL:postLogoutRedirectURL
additionalParameters:requestParameters.additionalParameters];

UIViewController *rootViewController = [self rootViewController];
UIViewController *presentingViewController = [self topMostViewController];
id<OIDExternalUserAgent> externalUserAgent =
[self userAgentWithViewController:rootViewController
[self userAgentWithViewController:presentingViewController
externalUserAgent:requestParameters.externalUserAgent];

return [OIDAuthorizationService
Expand Down Expand Up @@ -163,32 +163,56 @@ @implementation AppAuthIOSAuthorization
}

- (id<OIDExternalUserAgent>)
userAgentWithViewController:(UIViewController *)rootViewController
userAgentWithViewController:(UIViewController *)presentingViewController
externalUserAgent:(NSNumber *)externalUserAgent {
if ([externalUserAgent integerValue] == EphemeralASWebAuthenticationSession) {
return [[OIDExternalUserAgentIOSNoSSO alloc]
initWithPresentingViewController:rootViewController];
initWithPresentingViewController:presentingViewController];
}
if ([externalUserAgent integerValue] == SafariViewController) {
return [[OIDExternalUserAgentIOSSafariViewController alloc]
initWithPresentingViewController:rootViewController];
initWithPresentingViewController:presentingViewController];
}
return [[OIDExternalUserAgentIOS alloc]
initWithPresentingViewController:rootViewController];
initWithPresentingViewController:presentingViewController];
}

- (UIViewController *)topMostViewController {
return [self topMostViewControllerWithRootViewController:[self rootViewController]];
}

- (UIViewController *)topMostViewControllerWithRootViewController:(UIViewController *)viewController {
if (viewController.presentedViewController && !viewController.presentedViewController.isBeingDismissed) {
return [self topMostViewControllerWithRootViewController:viewController.presentedViewController];
}
if ([viewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)viewController;
return [self topMostViewControllerWithRootViewController:tabBarController.selectedViewController];
}
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)viewController;
return [self topMostViewControllerWithRootViewController:navController.visibleViewController];
}

return viewController;
}

- (UIViewController *)rootViewController {
if (@available(iOS 13, *)) {
return [[UIApplication sharedApplication].windows
filteredArrayUsingPredicate:[NSPredicate
predicateWithBlock:^BOOL(
id window,
NSDictionary *bindings) {
return [window isKeyWindow];
}]]
.firstObject.rootViewController;
}
return [UIApplication sharedApplication].delegate.window.rootViewController;
if (@available(iOS 13.0, *)) {
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
for (UIWindow *window in windowScene.windows) {
if (window.isKeyWindow) {
return window.rootViewController;
}
}
}
}
return nil;
} else {
return [UIApplication sharedApplication].delegate.window.rootViewController;
}
}

@end