Skip to content

Commit 37f9ed5

Browse files
Mark PhillipsMark Phillips
authored andcommitted
Added support for cordova inappbrowser plugin
1 parent 5bac7f8 commit 37f9ed5

9 files changed

Lines changed: 938 additions & 104 deletions

‎dist/oidc-client.js‎

Lines changed: 341 additions & 29 deletions
Large diffs are not rendered by default.

‎dist/oidc-client.min.js‎

Lines changed: 35 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/oidc-client.js‎

Lines changed: 341 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/oidc-client.min.js‎

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/CordovaPopupNavigator.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
import Log from './Log';
5+
import CordovaPopupWindow from './CordovaPopupWindow';
6+
7+
export default class CordovaPopupNavigator {
8+
9+
prepare(params) {
10+
let popup = new CordovaPopupWindow(params);
11+
return Promise.resolve(popup);
12+
}
13+
}

‎src/CordovaPopupWindow.js‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
import Log from './Log';
5+
6+
const DefaultPopupFeatures = 'location=no,toolbar=no,width=500,height=500,left=0,top=0';
7+
const DefaultPopupTarget = "_blank";
8+
9+
export default class CordovaPopupWindow {
10+
11+
constructor(params) {
12+
Log.info("CordovaPopupWindow.ctor");
13+
14+
this._promise = new Promise((resolve, reject) => {
15+
this._resolve = resolve;
16+
this._reject = reject;
17+
});
18+
19+
if (!window.cordova) {
20+
return _promise.reject("cordova is undefined")
21+
}
22+
23+
var cordovaMetadata = window.cordova.require("cordova/plugin_list").metadata;
24+
if (this._isInAppBrowserInstalled(cordovaMetadata) === false) {
25+
return _promise.reject("InAppBrowser plugin not found")
26+
}
27+
28+
this.features = params.popupWindowFeatures || DefaultPopupFeatures;
29+
this.target = params.popupWindowTarget || DefaultPopupTarget;
30+
31+
this.redirect_uri = params.startUrl;
32+
Log.info("redirect_uri: " + this.redirect_uri);
33+
}
34+
35+
_isInAppBrowserInstalled(cordovaMetadata) {
36+
return ["cordova-plugin-inappbrowser", "org.apache.cordova.inappbrowser"].some(function (name) {
37+
return cordovaMetadata.hasOwnProperty(name)
38+
})
39+
}
40+
41+
navigate(params) {
42+
Log.info("CordovaPopupWindow.navigate");
43+
44+
if (!params || !params.url) {
45+
this._error("No url provided");
46+
} else {
47+
this._popup = cordova.InAppBrowser.open(params.url, this.target, this.features);
48+
if (this._popup) {
49+
Log.info("popup successfully created");
50+
51+
this._exitCallbackEvent = this._exitCallback.bind(this);
52+
this._loadStartCallbackEvent = this._loadStartCallback.bind(this);
53+
54+
this._popup.addEventListener("exit", this._exitCallbackEvent, false);
55+
this._popup.addEventListener("loadstart", this._loadStartCallbackEvent, false);
56+
} else {
57+
this._error("Error opening popup window");
58+
}
59+
}
60+
return this.promise;
61+
}
62+
63+
get promise() {
64+
return this._promise;
65+
}
66+
67+
_loadStartCallback(event) {
68+
if (event.url.indexOf(this.redirect_uri) === 0) {
69+
this._success({ url: event.url });
70+
}
71+
}
72+
_exitCallback(message) {
73+
this._error(message);
74+
}
75+
76+
_success(data) {
77+
this._cleanup();
78+
79+
Log.info("Successful response from cordova popup window");
80+
this._resolve(data);
81+
}
82+
_error(message) {
83+
this._cleanup();
84+
85+
Log.error(message);
86+
this._reject(new Error(message));
87+
}
88+
89+
_cleanup() {
90+
Log.info("CordovaPopupWindow._cleanup");
91+
92+
this._popup.removeEventListener("exit", this._exitCallbackEvent, false);
93+
this._popup.removeEventListener("loadstart", this._loadStartCallbackEvent, false);
94+
95+
if (this._popup){
96+
this._popup.close();
97+
}
98+
this._popup = null;
99+
}
100+
}

‎src/CordovaSilentRenewService.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
import Log from './Log';
5+
6+
export default class CordovaSilentRenewService {
7+
8+
constructor(userManager) {
9+
this._userManager = userManager;
10+
this._userManager.events.addAccessTokenExpiring(this._tokenExpiring.bind(this));
11+
}
12+
13+
_tokenExpiring() {
14+
Log.info("CordovaSilentRenewService automatically renewing access token");
15+
16+
this._userManager.signinCordovaSilent().then(user => {
17+
Log.info("Cordova silent token renewal successful");
18+
}, err => {
19+
Log.error("Error from signinCordovaSilent:", err.message);
20+
this._userManager.events._raiseSilentRenewError(err);
21+
});
22+
}
23+
}

‎src/UserManager.js‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import UserManagerSettings from './UserManagerSettings';
77
import User from './User';
88
import UserManagerEvents from './UserManagerEvents';
99
import SilentRenewService from './SilentRenewService';
10+
import CordovaSilentRenewService from './CordovaSilentRenewService';
1011

1112
export default class UserManager extends OidcClient {
1213
constructor(settings = {}) {
@@ -22,11 +23,18 @@ export default class UserManager extends OidcClient {
2223
Log.info("automaticSilentRenew is configured, setting up silent renew")
2324
this._silentRenewService = new SilentRenewService(this);
2425
}
26+
if (this.settings.automaticCordovaSilentRenew) {
27+
Log.info("automaticCordovaSilentRenew is configured, setting up cordova silent renew")
28+
this._cordovaSilentRenewService = new CordovaSilentRenewService(this);
29+
}
2530
}
2631

2732
get _redirectNavigator() {
2833
return this.settings.redirectNavigator;
2934
}
35+
get _cordovaPopupNavigator() {
36+
return this.settings.cordovaPopupNavigator;
37+
}
3038
get _popupNavigator() {
3139
return this.settings.popupNavigator;
3240
}
@@ -47,6 +55,7 @@ export default class UserManager extends OidcClient {
4755
return this._loadUser().then(user => {
4856
if (user) {
4957
Log.info("user loaded");
58+
5059
return user;
5160
}
5261
else {
@@ -66,6 +75,60 @@ export default class UserManager extends OidcClient {
6675
});
6776
}
6877

78+
signinCordovaPopup(args = {}) {
79+
Log.info("UserManager.signinCordovaPopup");
80+
81+
let url = args.redirect_uri || this.settings.popup_redirect_uri || this.settings.redirect_uri;
82+
if (!url) {
83+
Log.error("No popup_redirect_uri or redirect_uri configured");
84+
return Promise.reject(new Error("No popup_redirect_uri or redirect_uri configured"));
85+
}
86+
87+
args.redirect_uri = url;
88+
args.display = "popup";
89+
90+
return this._signin(args, this._cordovaPopupNavigator, {
91+
startUrl: url,
92+
popupWindowFeatures: args.popupWindowFeatures || this.settings.popupWindowFeatures,
93+
popupWindowTarget: args.popupWindowTarget || this.settings.popupWindowTarget
94+
});
95+
}
96+
signoutCordovaPopup(args = {}) {
97+
Log.info("UserManager.signoutCordovaPopup");
98+
99+
let url = args.redirect_uri || this.settings.popup_redirect_uri || this.settings.redirect_uri;
100+
if (!url) {
101+
Log.error("No popup_redirect_uri or redirect_uri configured");
102+
return Promise.reject(new Error("No popup_redirect_uri or redirect_uri configured"));
103+
}
104+
105+
args.redirect_uri = url;
106+
args.display = "popup";
107+
108+
return this._signout(args, this._cordovaPopupNavigator, {
109+
startUrl: url,
110+
popupWindowFeatures: args.popupWindowFeatures || this.settings.popupWindowFeatures,
111+
popupWindowTarget: args.popupWindowTarget || this.settings.popupWindowTarget
112+
});
113+
}
114+
signinCordovaSilent(args = {}) {
115+
Log.info("UserManager.signinCordovaSilent");
116+
117+
let url = args.redirect_uri || this.settings.silent_redirect_uri;
118+
if (!url) {
119+
Log.error("No silent_redirect_uri configured");
120+
return Promise.reject(new Error("No silent_redirect_uri configured"));
121+
}
122+
123+
args.redirect_uri = url;
124+
args.prompt = "none";
125+
126+
return this._signin(args, this._cordovaPopupNavigator, {
127+
startUrl: url,
128+
popupWindowFeatures: 'hidden=yes',
129+
popupWindowTarget: args.popupWindowTarget || this.settings.popupWindowTarget
130+
});
131+
}
69132
signinPopup(args = {}) {
70133
Log.info("UserManager.signinPopup");
71134

@@ -88,7 +151,6 @@ export default class UserManager extends OidcClient {
88151
Log.info("UserManager.signinPopupCallback");
89152
return this._signinCallback(url, this._popupNavigator);
90153
}
91-
92154
signinSilent(args = {}) {
93155
Log.info("UserManager.signinSilent");
94156

‎src/UserManagerSettings.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Log from './Log';
55
import OidcClientSettings from './OidcClientSettings';
66
import RedirectNavigator from './RedirectNavigator';
7+
import CordovaPopupNavigator from './CordovaPopupNavigator';
78
import PopupNavigator from './PopupNavigator';
89
import IFrameNavigator from './IFrameNavigator';
910
import WebStorageStateStore from './WebStorageStateStore';
@@ -18,8 +19,10 @@ export default class UserManagerSettings extends OidcClientSettings {
1819
popupWindowTarget,
1920
silent_redirect_uri,
2021
automaticSilentRenew = false,
22+
automaticCordovaSilentRenew = false,
2123
accessTokenExpiringNotificationTime = DefaultAccessTokenExpiringNotificationTime,
2224
redirectNavigator = new RedirectNavigator(),
25+
cordovaPopupNavigator = new CordovaPopupNavigator(),
2326
popupNavigator = new PopupNavigator(),
2427
iframeNavigator = new IFrameNavigator(),
2528
userStore = new WebStorageStateStore({ store: Global.sessionStorage })
@@ -32,9 +35,11 @@ export default class UserManagerSettings extends OidcClientSettings {
3235

3336
this._silent_redirect_uri = silent_redirect_uri;
3437
this._automaticSilentRenew = !!automaticSilentRenew;
38+
this._automaticCordovaSilentRenew = !!automaticCordovaSilentRenew;
3539
this._accessTokenExpiringNotificationTime = accessTokenExpiringNotificationTime;
3640

3741
this._redirectNavigator = redirectNavigator;
42+
this._cordovaPopupNavigator = cordovaPopupNavigator;
3843
this._popupNavigator = popupNavigator;
3944
this._iframeNavigator = iframeNavigator;
4045

@@ -57,14 +62,19 @@ export default class UserManagerSettings extends OidcClientSettings {
5762
get automaticSilentRenew() {
5863
return !!(this.silent_redirect_uri && this._automaticSilentRenew);
5964
}
60-
65+
get automaticCordovaSilentRenew() {
66+
return !!(this.silent_redirect_uri && this._automaticCordovaSilentRenew);
67+
}
6168
get accessTokenExpiringNotificationTime() {
6269
return this._accessTokenExpiringNotificationTime;
6370
}
6471

6572
get redirectNavigator() {
6673
return this._redirectNavigator;
6774
}
75+
get cordovaPopupNavigator() {
76+
return this._cordovaPopupNavigator;
77+
}
6878
get popupNavigator() {
6979
return this._popupNavigator;
7080
}

0 commit comments

Comments
 (0)