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

Fix auth redirection logic #100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ in case of vulnerabilities.
- Complete migration to ns8 and fix build issues by [@rigor789](https://github.com/rigor789) ([#92](https://github.com/proyecto26/nativescript-inappbrowser/pull/92)).
- Fix support for `Metadata Filtering` from Android by [@jcassidyav](https://github.com/jcassidyav) ([#93](https://github.com/proyecto26/nativescript-inappbrowser/pull/93)).
- Avoid stringifying null redirect url by [@rmartin48](https://github.com/rmartin48) ([#99](https://github.com/proyecto26/nativescript-inappbrowser/pull/99)).
- Fix auth redirection logic by [@jdnichollsc](https://github.com/jdnichollsc) ([#100](https://github.com/proyecto26/nativescript-inappbrowser/pull/100)).

### Removed
- Remove `QUERY_ALL_PACKAGES` permission by [@edusperoni](https://github.com/edusperoni) ([#87](https://github.com/proyecto26/nativescript-inappbrowser/pull/87)).
Expand Down
5 changes: 3 additions & 2 deletions src/ChromeTabsManagerActivity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Context = android.content.Context;
import Intent = android.content.Intent;
import Bundle = android.os.Bundle;
import Log = android.util.Log;

import { Observable } from "@nativescript/core";
import { BROWSER_TYPES } from "./InAppBrowser.common";
import { DISMISSED_EVENT } from "./utils.android";
import { log } from "./utils.common";

class ChromeTabsEvent extends Observable {
public message: string;
Expand All @@ -18,6 +18,7 @@ const BROWSER_ACTIVITY_EVENTS = new ChromeTabsEvent();
const KEY_BROWSER_INTENT = "browserIntent";
const BROWSER_RESULT_TYPE = "browserResultType";
const DEFAULT_RESULT_TYPE = BROWSER_TYPES.DISMISS;
const TAG = "ChromeTabsManagerActivity";

const notifyMessage = (
message: string,
Expand Down Expand Up @@ -74,7 +75,7 @@ class ChromeTabsManagerActivity extends android.app.Activity {
this.isError = true;
notifyMessage("Unable to open url.", this.resultType, this.isError);
this.finish();
log(`InAppBrowser: ${error}`);
Log.e(TAG, error.message);
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/InAppBrowser.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,15 @@ function setup() {
redirectUrl: string,
options?: InAppBrowserOptions
) {
let response = null;
try {
response = await openAuthSessionPolyfillAsync(
(startUrl, opt) => this.open(startUrl, opt),
url,
redirectUrl,
options
return await openAuthSessionPolyfillAsync(
() => this.open(url, options),
redirectUrl
);
} finally {
closeAuthSessionPolyfillAsync();
this.close();
}
return response;
}

public closeAuth(): void {
Expand Down
10 changes: 6 additions & 4 deletions src/InAppBrowser.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
setModalInPresentation,
dismissWithoutAnimation,
InAppBrowserOpenAuthErrorMessage,
getWindow,
} from "./utils.ios";

const DEFAULT_PROTOCOLS = [
Expand Down Expand Up @@ -142,8 +143,8 @@ function setup() {
}
}

const ctrl =
UIApplication.sharedApplication.keyWindow.rootViewController;
const window = getWindow();
const ctrl = window.rootViewController;
if (modalEnabled) {
// This is a hack to present the SafariViewController modally
const safariHackVC =
Expand Down Expand Up @@ -198,7 +199,8 @@ function setup() {
});
}
public close() {
const ctrl = UIApplication.sharedApplication.keyWindow.rootViewController;
const window = getWindow();
const ctrl = window.rootViewController;
ctrl.dismissViewControllerAnimatedCompletion(this.animated, () => {
if (this.redirectResolve) {
this.redirectResolve({
Expand Down Expand Up @@ -286,7 +288,7 @@ function setup() {
public presentationAnchorForWebAuthenticationSession(
_: ASWebAuthenticationSession
): UIWindow {
return UIApplication.sharedApplication.keyWindow;
return getWindow();
}
public safariViewControllerDidFinish(
controller: SFSafariViewController
Expand Down
33 changes: 14 additions & 19 deletions src/utils.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Intent = android.content.Intent;
import NfcAdapter = android.nfc.NfcAdapter;
import Context = android.content.Context;
import ResolveInfo = android.content.pm.ResolveInfo;
import Color = android.graphics.Color;
import List = java.util.List;
import Arrays = java.util.Arrays;

Expand All @@ -16,9 +15,8 @@ import {
} from "@nativescript/core";
import {
AuthSessionResult,
BrowserResult,
BROWSER_TYPES,
InAppBrowserOptions,
OpenBrowserAsync,
RedirectResult,
} from "./InAppBrowser.common";

Expand Down Expand Up @@ -61,6 +59,7 @@ export const DISMISSED_EVENT = "DismissedEvent";
* Save the handler of the redirection event in order to removes listener later.
*/
let _redirectHandler: (args: ApplicationEventData) => void;

/**
* Save the previous url in order to avoid loading the same data for a new Authentication flow.
*/
Expand Down Expand Up @@ -109,17 +108,15 @@ function waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {
function handleAppStateActiveOnce(): Promise<Activity> {
return new Promise(function (resolve) {
// Browser can be closed before handling AppState change
if (!Application.android.paused) {
const activity =
Application.android.foregroundActivity ||
Application.android.startActivity;
return resolve(activity);
if (!Application.android.paused && Application.android.foregroundActivity) {
resolve(Application.android.foregroundActivity);
}
function handleAppStateChange(args: AndroidActivityEventData) {
resolve(args.activity);
}
Application.android.once(
AndroidApplication.activityResumedEvent,
function (args: AndroidActivityEventData) {
resolve(args.activity);
}
handleAppStateChange,
);
});
}
Expand All @@ -143,18 +140,16 @@ async function checkResultAndReturnUrl(
}

/* Android polyfill for AuthenticationSession flow */
export function openAuthSessionPolyfillAsync(
open: OpenBrowserAsync,
startUrl: string,
export async function openAuthSessionPolyfillAsync(
open: () => Promise<BrowserResult>,
returnUrl: string,
options?: InAppBrowserOptions
): Promise<AuthSessionResult> {
return Promise.race([
waitForRedirectAsync(returnUrl),
open(startUrl, options).then(function (result) {
return await Promise.race([
open().then(function (result) {
return checkResultAndReturnUrl(returnUrl, result);
}),
]);
waitForRedirectAsync(returnUrl),
])
}

export function closeAuthSessionPolyfillAsync(): void {
Expand Down
5 changes: 4 additions & 1 deletion src/utils.common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Color } from "@nativescript/core";
import { Application, Color } from "@nativescript/core";

export function parseColor(color: string | Color) {
if (color && !(color instanceof Color)) {
Expand All @@ -23,5 +23,8 @@ export function log(message: string, ...optionalParams: any[]): void {
if (nglog) {
nglog(message, ...optionalParams);
}
if (Application.android) {
android.util.Log.d("JS", message);
}
console.log(message, ...optionalParams);
}
11 changes: 10 additions & 1 deletion src/utils.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export function getTransitionStyle(styleKey: string): UIModalTransitionStyle {
: UIModalTransitionStyle.CoverVertical;
}

export function getWindow(): UIWindow {
const sharedApplication = UIApplication.sharedApplication;
if (sharedApplication.windows.count > 0 && sharedApplication.windows[0]) {
return sharedApplication.windows[0];
}
return sharedApplication.keyWindow;
}

export function dismissWithoutAnimation(
controller: SFSafariViewController
): void {
Expand All @@ -54,7 +62,8 @@ export function dismissWithoutAnimation(
controller.view.alpha = 0.05;
controller.view.frame = CGRectMake(0.0, 0.0, 0.5, 0.5);

const ctrl = UIApplication.sharedApplication.keyWindow.rootViewController;
const window = getWindow();
const ctrl = window.rootViewController;
ctrl.view.layer.addAnimationForKey(transition, animationKey);
ctrl.dismissViewControllerAnimatedCompletion(false, () => {
ctrl.view.layer.removeAnimationForKey(animationKey);
Expand Down