-
Notifications
You must be signed in to change notification settings - Fork 473
feat: allow to resume the request if androdi OS kills the process #1146
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
Open
Francesco-Voto
wants to merge
1
commit into
main
Choose a base branch
from
feat/resume-android-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -65,11 +65,20 @@ | |||||
| import java.util.Map; | ||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||
| import java.util.concurrent.CountDownLatch; | ||||||
| import java.util.concurrent.atomic.AtomicReference; | ||||||
|
|
||||||
| public class RNAppAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener { | ||||||
|
|
||||||
| public static final String CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome"; | ||||||
|
|
||||||
| public static final int AUTHORIZATION_REQUEST_CODE = 52; | ||||||
|
|
||||||
| private static final AtomicReference<Intent> sStashedAuthorizationResult = new AtomicReference<>(); | ||||||
|
|
||||||
| public static void stashAuthorizationResult(Intent data) { | ||||||
| sStashedAuthorizationResult.set(data); | ||||||
| } | ||||||
|
|
||||||
| private final ReactApplicationContext reactContext; | ||||||
| private Promise promise; | ||||||
| private boolean dangerouslyAllowInsecureHttpRequests; | ||||||
|
|
@@ -484,13 +493,73 @@ public void onFetchConfigurationCompleted( | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| * Called when the OAuth browser activity completes | ||||||
| */ | ||||||
| @ReactMethod | ||||||
| public void resumePendingAuthorize( | ||||||
| final ReadableMap additionalParameters, | ||||||
| final Double connectionTimeoutMillis, | ||||||
| final ReadableMap customHeaders, | ||||||
| final boolean dangerouslyAllowInsecureHttpRequests, | ||||||
| final Promise promise) { | ||||||
| final Intent data = sStashedAuthorizationResult.getAndSet(null); | ||||||
|
|
||||||
| if (data == null) { | ||||||
| promise.resolve(null); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| try { | ||||||
| final AuthorizationException authException = AuthorizationException.fromIntent(data); | ||||||
| if (authException != null) { | ||||||
| handleAuthorizationException("authentication_error", authException, promise); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| final AuthorizationResponse response = AuthorizationResponse.fromIntent(data); | ||||||
| if (response == null) { | ||||||
| promise.resolve(null); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| this.parseHeaderMap(customHeaders); | ||||||
| final AppAuthConfiguration configuration = createAppAuthConfiguration( | ||||||
| createConnectionBuilder(dangerouslyAllowInsecureHttpRequests, this.tokenRequestHeaders, | ||||||
| connectionTimeoutMillis), | ||||||
| dangerouslyAllowInsecureHttpRequests, | ||||||
| null | ||||||
| ); | ||||||
|
|
||||||
| final AuthorizationService authService = new AuthorizationService(this.reactContext, configuration); | ||||||
|
|
||||||
| final Map<String, String> additionalParametersMap = MapUtil.readableMapToHashMap(additionalParameters); | ||||||
| final TokenRequest tokenRequest = additionalParametersMap.isEmpty() | ||||||
| ? response.createTokenExchangeRequest() | ||||||
| : response.createTokenExchangeRequest(additionalParametersMap); | ||||||
|
|
||||||
| authService.performTokenRequest(tokenRequest, new AuthorizationService.TokenResponseCallback() { | ||||||
| @Override | ||||||
| public void onTokenRequestCompleted(TokenResponse resp, AuthorizationException ex) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| authService.dispose(); | ||||||
| if (resp != null) { | ||||||
| promise.resolve(TokenResponseFactory.tokenResponseToMap(resp, response)); | ||||||
| } else { | ||||||
| handleAuthorizationException("token_exchange_failed", ex, promise); | ||||||
| } | ||||||
| } | ||||||
| }); | ||||||
| } catch (Exception e) { | ||||||
| promise.reject("run_time_exception", e.getMessage()); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { | ||||||
| try { | ||||||
| if (requestCode == 52) { | ||||||
| if (requestCode == AUTHORIZATION_REQUEST_CODE) { | ||||||
| if (this.promise == null) { | ||||||
| return; | ||||||
| } | ||||||
| sStashedAuthorizationResult.set(null); | ||||||
|
|
||||||
| if (data == null) { | ||||||
| if (promise != null) { | ||||||
| promise.reject("authentication_error", "Data intent is null" ); | ||||||
|
|
@@ -737,21 +806,52 @@ private void authorizeWithConfiguration( | |||||
| AuthorizationRequest authRequest = authRequestBuilder.build(); | ||||||
|
|
||||||
| if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { | ||||||
| AuthorizationService authService = new AuthorizationService(context, appAuthConfiguration); | ||||||
| final Promise authorizePromise = this.promise; | ||||||
|
|
||||||
| CustomTabsIntent.Builder intentBuilder = authService.createCustomTabsIntentBuilder(); | ||||||
| CustomTabsIntent customTabsIntent = intentBuilder.setEphemeralBrowsingEnabled(androidPrefersEphemeralSession).build(); | ||||||
|
|
||||||
| if (androidTrustedWebActivity) { | ||||||
| customTabsIntent.intent.putExtra(TrustedWebUtils.EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY, true); | ||||||
| } | ||||||
| // AuthorizationService construction and createCustomTabsIntentBuilder() are @WorkerThread: | ||||||
| // the latter blocks on CustomTabManager's 1s browser-connection latch. Running them on the | ||||||
| // main thread freezes the UI for up to a second on every authorize(). | ||||||
| new Thread(new Runnable() { | ||||||
| @Override | ||||||
| public void run() { | ||||||
| try { | ||||||
| AuthorizationService authService = new AuthorizationService(context, appAuthConfiguration); | ||||||
|
|
||||||
| CustomTabsIntent.Builder intentBuilder = authService.createCustomTabsIntentBuilder(); | ||||||
| CustomTabsIntent customTabsIntent = intentBuilder.setEphemeralBrowsingEnabled(androidPrefersEphemeralSession).build(); | ||||||
|
|
||||||
| Intent authIntent = authService.getAuthorizationRequestIntent(authRequest, customTabsIntent); | ||||||
| if (androidTrustedWebActivity) { | ||||||
| customTabsIntent.intent.putExtra(TrustedWebUtils.EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY, true); | ||||||
| } | ||||||
|
|
||||||
| currentActivity.startActivityForResult(authIntent, 52); | ||||||
| final Intent authIntent = authService.getAuthorizationRequestIntent(authRequest, customTabsIntent); | ||||||
|
|
||||||
| currentActivity.runOnUiThread(new Runnable() { | ||||||
| @Override | ||||||
| public void run() { | ||||||
| try { | ||||||
| currentActivity.startActivityForResult(authIntent, AUTHORIZATION_REQUEST_CODE); | ||||||
| } catch (ActivityNotFoundException e) { | ||||||
| if (authorizePromise != null) { | ||||||
| authorizePromise.reject("browser_not_found", e.getMessage()); | ||||||
| } | ||||||
| } catch (Exception e) { | ||||||
| if (authorizePromise != null) { | ||||||
| authorizePromise.reject("authentication_failed", e.getMessage()); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| }); | ||||||
| } catch (Exception e) { | ||||||
| if (authorizePromise != null) { | ||||||
| authorizePromise.reject("authentication_failed", e.getMessage()); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| }, "RNAppAuth-authorize").start(); | ||||||
| } else { | ||||||
| AuthorizationService authService = new AuthorizationService(currentActivity, appAuthConfiguration); | ||||||
| PendingIntent pendingIntent = currentActivity.createPendingResult(52, new Intent(), 0); | ||||||
| PendingIntent pendingIntent = currentActivity.createPendingResult(AUTHORIZATION_REQUEST_CODE, new Intent(), 0); | ||||||
|
|
||||||
| authService.performAuthorizationRequest(authRequest, pendingIntent); | ||||||
| } | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -274,6 +274,29 @@ export const authorize = ({ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| return wrapNativeAuthPromise(RNAppAuth.authorize(...nativeMethodArguments)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const resumePendingAuthorize = ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| additionalParameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dangerouslyAllowInsecureHttpRequests = false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| customHeaders, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| connectionTimeoutSeconds, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } = {}) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Platform.OS !== 'android') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Promise.resolve(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| validateHeaders(customHeaders); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| validateConnectionTimeoutSeconds(connectionTimeoutSeconds); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return wrapNativeAuthPromise( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| RNAppAuth.resumePendingAuthorize( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| additionalParameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| convertTimeoutForPlatform(Platform.OS, connectionTimeoutSeconds), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+282
to
+293
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| customHeaders, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dangerouslyAllowInsecureHttpRequests | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const refresh = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| issuer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.