Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash immediately after starting on iOS in SwiftFlutterWebAuthPlugin.handle #182

Open
dlatikaynen opened this issue Feb 9, 2024 · 3 comments

Comments

@dlatikaynen
Copy link

dlatikaynen commented Feb 9, 2024

Works perfectly well on Android.

0   flutter_web_auth_2            	0x00000001023852dc Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value + 0 (<compiler-generated>:0)
1   flutter_web_auth_2            	0x00000001023852dc closure #1 in SwiftFlutterWebAuth2Plugin.handle(_:result:) + 1452 (<compiler-generated>:0)
2   flutter_web_auth_2            	0x0000000102385788 thunk for @escaping @callee_guaranteed (@in_guaranteed URL?, @guaranteed Error?) -> () + 192 (<compiler-generated>:0)
3   AuthenticationServices        	0x0000000202097d88 -[ASWebAuthenticationSession _startDryRun:] + 592 (ASWebAuthenticationSessionIOS.m:177)
4   flutter_web_auth_2            	0x0000000102386fb4 specialized SwiftFlutterWebAuth2Plugin.handle(_:result:) + 3492 (SwiftFlutterWebAuth2Plugin.swift:100)
5   flutter_web_auth_2            	0x0000000102385338 specialized SwiftFlutterWebAuth2Plugin.handle(_:result:) + 16 (<compiler-generated>:0)
6   flutter_web_auth_2            	0x0000000102385338 @objc SwiftFlutterWebAuth2Plugin.handle(_:result:) + 84

crashlog.txt

oauth2_client: ^3.2.2

class CAATSOAuth2Client extends OAuth2Client {
  CAATSOAuth2Client({required String authorizeUrl, required String tokenUrl, required String refreshUrl, required String revokeUrl})
      : super(
            authorizeUrl: authorizeUrl,
            tokenUrl: tokenUrl,
            refreshUrl: refreshUrl,
            revokeUrl: revokeUrl,
            redirectUri: AuthConstants.loginRedirectUri,
            customUriScheme: AuthConstants.uriScheme,
            credentialsLocation: CredentialsLocation.body);
}
  late String authorizationEndpoint = "";
  late String tokenEndpoint = "";
  late String refreshEndpoint = "";
  late String revokeEndpoint = "";
  late String endSessionEndpoint = "";

  late CAATSOAuth2Client client;
  late OAuth2Helper helper;
  late ConnectedSystem connectedSystem;

  Future<void> setup(ConnectedSystem connectedSystem, String clientId, String clientSecret) async {
    final storageKey = uuid.v1();
    this.connectedSystem = connectedSystem;

    client = CAATSOAuth2Client(
        authorizeUrl: authorizationEndpoint,
        tokenUrl: tokenEndpoint,
        refreshUrl: refreshEndpoint,
        revokeUrl: revokeEndpoint);

    helper = OAuth2Helper(client,
        clientId: clientId,
        clientSecret: clientSecret,
        scopes: [...AuthConstants.scopes],
        webAuthOpts: {"preferEphemeral": true},
        enablePKCE: true,
        enableState: true,
        grantType: OAuth2Helper.authorizationCode,
        tokenStorage: TokenStorage(storageKey));

    await fetchConfiguration(connectedSystem.host.idpAuthority);
    client.authorizeUrl = authorizationEndpoint;
    client.tokenUrl = tokenEndpoint;
    client.refreshUrl = refreshEndpoint;
    client.revokeUrl = revokeEndpoint;
  }
class TokenCubit extends Cubit<AccessTokenResponse?> {
  TokenCubit() : super(null);

  Future<AccessTokenResponse?> login(
    ConnectedSystem connectedSystem,
    String clientId,
    String clientSecret,
  ) async {
    if (await SessionManager.startSession(connectedSystem, clientId, clientSecret)) {
      debugPrint('#*#*# before -- fetchtoken');

      final tokenRes = await SessionManager.currentSession!.helper.fetchToken();

      if (!isClosed) {
        emit(tokenRes);
      }

      return tokenRes;
    }

    debugPrint('#*#*# before -- login return null');

    return null;
  }
}

Since this is some kind of null reference exception (or "unpacking-asserted-not-null value which is actually null") kind of error, maybe there is now a version mismatch between this and the underlying plugin, and we feed it something which is not a mandatory input in oauth2_client but (now) is in one of the underlying functions?

@mibos-kremer
Copy link

mibos-kremer commented Apr 5, 2024

Experiencing the same issue here.

0   flutter_web_auth_2            	0x0000000102cfd6bc Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value + 0 (SwiftFlutterWebAuth2Plugin.swift:0)
1   flutter_web_auth_2            	0x0000000102cfd6bc closure #1 in SwiftFlutterWebAuth2Plugin.handle(_:result:) + 1312 (SwiftFlutterWebAuth2Plugin.swift:29)
2   flutter_web_auth_2            	0x0000000102cfdb64 thunk for @escaping @callee_guaranteed (@in_guaranteed URL?, @guaranteed Error?) -> () + 192 (<compiler-generated>:0)
3   AuthenticationServices        	0x00000002065cbb70 -[ASWebAuthenticationSession _cancelWithError:] + 40 (ASWebAuthenticationSessionIOS.m:312)
4   AuthenticationServices        	0x00000002065cb9f4 -[ASWebAuthenticationSession _startDryRun:] + 644 (ASWebAuthenticationSessionIOS.m:228)
5   flutter_web_auth_2            	0x0000000102cff0e8 specialized SwiftFlutterWebAuth2Plugin.handle(_:result:) + 2904 (SwiftFlutterWebAuth2Plugin.swift:100)
6   flutter_web_auth_2            	0x0000000102cfd714 specialized SwiftFlutterWebAuth2Plugin.handle(_:result:) + 16 (<compiler-generated>:0)
7   flutter_web_auth_2            	0x0000000102cfd714 @objc SwiftFlutterWebAuth2Plugin.handle(_:result:) + 84
8   Flutter                       	0x00000001037c9a38 0x1031e8000 + 6167096
9   Flutter                       	0x000000010322bc00 0x1031e8000 + 277504

We partially worked around the crash by adding a login button instead of automatically logging in on startup, but it still crashes occasionally.

@ghSingleOps
Copy link

ghSingleOps commented Jun 25, 2024

I am seeing this issue on app startup too.

The code that is generated for SwiftFlutterWebAuth2Plugin specifies a completionHandler that assumes a session exists. It crashes on this line:

(sessionToKeepAlive as! ASWebAuthenticationSession).cancel()

The problem is that a prior session was NOT established yet. The completionHandler is being called with an error related to app window management:

Error Domain=com.apple.AuthenticationServices.WebAuthenticationSession Code=3 "The UIWindowScene for the returned window was not in the foreground active state." UserInfo={NSDebugDescription=The UIWindowScene for the returned window was not in the foreground active state.}

A fix that works for me is to determine when the app actually reports that it is in foreground, and only attempt a network operation at that point. I used a solution that is specified here: https://stackoverflow.com/questions/51835039/how-do-i-check-if-the-flutter-application-is-in-the-foreground-or-not

(UPDATE: This fix does not work on all devices, still looking for a workaround for this limitation)

(SECOND UPDATE: I was able to fix it following suggestions here, but implementing sceneDidBecomeActive instead of willConnectTo: https://stackoverflow.com/questions/63631338/how-to-have-a-flutter-method-handler-in-scenedelegate)

@CesarRN
Copy link

CesarRN commented Sep 10, 2024

Similar issue

Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 flutter_web_auth_2 0x1055850f4 closure #1 in SwiftFlutterWebAuth2Plugin.handle(_:result:) + 1312
1 flutter_web_auth_2 0x10558559c thunk for @escaping @callee_guaranteed (@in_guaranteed URL?, @guaranteed Error?) -> () + 192
2 AuthenticationServices 0x21d49f554 -[ASWebAuthenticationSession cancelWithError:] + 40
3 AuthenticationServices 0x21d49f3d8 -[ASWebAuthenticationSession startDryRun:] + 644
4 flutter_web_auth_2 0x105586c0c specialized SwiftFlutterWebAuth2Plugin.handle(
:result:) + 3048
5 flutter_web_auth_2 0x10558514c @objc SwiftFlutterWebAuth2Plugin.handle(
:result:) + 84
6 Flutter 0x105e4191c 0x105860000 + 6166812
7 Flutter 0x1058a3a44 0x105860000 + 277060
8 libdispatch.dylib 0x1ac45d13c _dispatch_call_block_and_release + 32
9 libdispatch.dylib 0x1ac45edd4 _dispatch_client_callout + 20
10 libdispatch.dylib 0x1ac46d5a4 _dispatch_main_queue_drain + 988
11 libdispatch.dylib 0x1ac46d1b8 _dispatch_main_queue_callback_4CF + 44
12 CoreFoundation 0x1a458b710 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 16
13 CoreFoundation 0x1a4588914 __CFRunLoopRun + 1996
14 CoreFoundation 0x1a4587cd8 CFRunLoopRunSpecific + 608
15 GraphicsServices 0x1e8fd51a8 GSEventRunModal + 164
16 UIKitCore 0x1a6bc1ae8 -[UIApplication _run] + 888
17 UIKitCore 0x1a6c75d98 UIApplicationMain + 340
18 Home Komfort 0x102e708dc 0x102e68000 + 35036
19 dyld 0x1c7d5f154 start + 2356

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants