Skip to content

Commit 9c92bb7

Browse files
Mark Phillipsbrockallen
authored andcommitted
Added support for cordova inappbrowser plugin
Removed cordova specific apis from UserManager Force no zoom on Android devices Removed commented code Removed cordova specific parameter
1 parent 5bac7f8 commit 9c92bb7

File tree

10 files changed

+736
-114
lines changed

10 files changed

+736
-114
lines changed

dist/oidc-client.js

Lines changed: 299 additions & 56 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.

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const InMemoryWebStorage = require('./src/InMemoryWebStorage');
88
export const UserManager = require('./src/UserManager');
99
export const AccessTokenEvents = require('./src/AccessTokenEvents');
1010
export const MetadataService = require('./src/MetadataService');
11+
export const CordovaPopupNavigator = require('./src/CordovaPopupNavigator');
12+
export const CordovaIFrameNavigator = require('./src/CordovaIFrameNavigator');
1113

1214
export default {
1315
Log,
@@ -16,5 +18,7 @@ export default {
1618
InMemoryWebStorage,
1719
UserManager,
1820
AccessTokenEvents,
19-
MetadataService
21+
MetadataService,
22+
CordovaPopupNavigator,
23+
CordovaIFrameNavigator
2024
};

lib/oidc-client.js

Lines changed: 242 additions & 10 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/CordovaIFrameNavigator.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 CordovaIFrameNavigator {
8+
9+
prepare(params) {
10+
params.popupWindowFeatures = 'hidden=yes';
11+
let popup = new CordovaPopupWindow(params);
12+
return Promise.resolve(popup);
13+
}
14+
}

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,zoom=no';
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/UserManager.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default class UserManager extends OidcClient {
4747
return this._loadUser().then(user => {
4848
if (user) {
4949
Log.info("user loaded");
50+
5051
return user;
5152
}
5253
else {
@@ -88,7 +89,6 @@ export default class UserManager extends OidcClient {
8889
Log.info("UserManager.signinPopupCallback");
8990
return this._signinCallback(url, this._popupNavigator);
9091
}
91-
9292
signinSilent(args = {}) {
9393
Log.info("UserManager.signinSilent");
9494

@@ -101,7 +101,7 @@ export default class UserManager extends OidcClient {
101101
args.redirect_uri = url;
102102
args.prompt = "none";
103103

104-
return this._signin(args, this._iframeNavigator);
104+
return this._signin(args, this._iframeNavigator, { startUrl: url });
105105
}
106106
signinSilentCallback(url) {
107107
Log.info("UserManager.signinSilentCallback");
@@ -141,6 +141,21 @@ export default class UserManager extends OidcClient {
141141
Log.info("UserManager.signoutRedirect");
142142
return this._signoutStart(args, this._redirectNavigator);
143143
}
144+
signoutPopup(args = {}) {
145+
Log.info("UserManager.signoutPopup");
146+
147+
let url = args.redirect_uri || this.settings.popup_redirect_uri || this.settings.redirect_uri;
148+
if (!url) {
149+
Log.error("No popup_redirect_uri or redirect_uri configured");
150+
return Promise.reject(new Error("No popup_redirect_uri or redirect_uri configured"));
151+
}
152+
153+
return this._signout(args, this._popupNavigator, {
154+
startUrl: url,
155+
popupWindowFeatures: args.popupWindowFeatures || this.settings.popupWindowFeatures,
156+
popupWindowTarget: args.popupWindowTarget || this.settings.popupWindowTarget
157+
});
158+
}
144159
signoutRedirectCallback(url) {
145160
Log.info("UserManager.signoutRedirectCallback");
146161
return this._signoutEnd(url || this._redirectNavigator.url);

src/UserManagerSettings.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export default class UserManagerSettings extends OidcClientSettings {
5757
get automaticSilentRenew() {
5858
return !!(this.silent_redirect_uri && this._automaticSilentRenew);
5959
}
60-
6160
get accessTokenExpiringNotificationTime() {
6261
return this._accessTokenExpiringNotificationTime;
6362
}

0 commit comments

Comments
 (0)