Skip to content

Commit

Permalink
[flutter_appauth] Catch native ActivityNotFoundException (#465)
Browse files Browse the repository at this point in the history
* catch ActivityNotFoundException to avoid crashes

* send more specific exception
  • Loading branch information
NikHomann authored Jan 28, 2024
1 parent 26d55a9 commit 9f21e02
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.crossingthestreams.flutterappauth;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
Expand Down Expand Up @@ -56,13 +57,15 @@ public class FlutterAppauthPlugin implements FlutterPlugin, MethodCallHandler, P
private static final String END_SESSION_ERROR_CODE = "end_session_failed";
private static final String NULL_INTENT_ERROR_CODE = "null_intent";
private static final String INVALID_CLAIMS_ERROR_CODE = "invalid_claims";
private static final String NO_BROWSER_AVAILABLE_ERROR_CODE = "no_browser_available";

private static final String DISCOVERY_ERROR_MESSAGE_FORMAT = "Error retrieving discovery document: [error: %s, description: %s]";
private static final String TOKEN_ERROR_MESSAGE_FORMAT = "Failed to get token: [error: %s, description: %s]";
private static final String AUTHORIZE_ERROR_MESSAGE_FORMAT = "Failed to authorize: [error: %s, description: %s]";
private static final String END_SESSION_ERROR_MESSAGE_FORMAT = "Failed to end session: [error: %s, description: %s]";

private static final String NULL_INTENT_ERROR_FORMAT = "Failed to authorize: Null intent received";
private static final String NO_BROWSER_AVAILABLE_ERROR_FORMAT = "Failed to authorize: No suitable browser is available";

private final int RC_AUTH_EXCHANGE_CODE = 65030;
private final int RC_AUTH = 65031;
Expand Down Expand Up @@ -348,8 +351,13 @@ private void performAuthorization(AuthorizationServiceConfiguration serviceConfi
}

AuthorizationService authorizationService = allowInsecureConnections ? insecureAuthorizationService : defaultAuthorizationService;
Intent authIntent = authorizationService.getAuthorizationRequestIntent(authRequestBuilder.build());
mainActivity.startActivityForResult(authIntent, exchangeCode ? RC_AUTH_EXCHANGE_CODE : RC_AUTH);

try {
Intent authIntent = authorizationService.getAuthorizationRequestIntent(authRequestBuilder.build());
mainActivity.startActivityForResult(authIntent, exchangeCode ? RC_AUTH_EXCHANGE_CODE : RC_AUTH);
} catch (ActivityNotFoundException ex) {
finishWithError(NO_BROWSER_AVAILABLE_ERROR_CODE, NO_BROWSER_AVAILABLE_ERROR_FORMAT, getCauseFromException(ex));
}
}

private void performTokenRequest(AuthorizationServiceConfiguration serviceConfiguration, TokenRequestParameters tokenRequestParameters) {
Expand Down

0 comments on commit 9f21e02

Please sign in to comment.