-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
login-dialog.js
332 lines (293 loc) · 9.17 KB
/
login-dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {getMode} from '../../../src/mode';
import {listen} from '../../../src/event-helper';
import {dev, user} from '../../../src/log';
import {openWindowDialog} from '../../../src/dom';
import {parseUrl} from '../../../src/url';
import {viewerForDoc} from '../../../src/services';
import {urls} from '../../../src/config';
/** @const */
const TAG = 'amp-access-login';
/** @const {!RegExp} */
const RETURN_URL_REGEX = new RegExp('RETURN_URL');
/**
* Opens the login dialog for the specified URL. If the login dialog succeeds,
* the returned promised is resolved with the dialog's response. Otherwise, the
* returned promise is rejected.
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
* @param {string|!Promise<string>} urlOrPromise
* @return {!Promise<string>}
*/
export function openLoginDialog(ampdoc, urlOrPromise) {
const viewer = viewerForDoc(ampdoc);
const overrideDialog = parseInt(viewer.getParam('dialog'), 10);
if (overrideDialog) {
return new ViewerLoginDialog(viewer, urlOrPromise).open();
}
return new WebLoginDialog(ampdoc.win, viewer, urlOrPromise).open();
}
export function getLoginUrl(ampdoc, urlOrPromise) {
const viewer = viewerForDoc(ampdoc);
const overrideDialog = parseInt(viewer.getParam('dialog'), 10);
if (overrideDialog) {
return new ViewerLoginDialog(viewer, urlOrPromise).getLoginUrl();
}
return new WebLoginDialog(ampdoc.win, viewer, urlOrPromise).getLoginUrl();
}
/**
* The implementation of the Login Dialog delegated via Viewer.
*/
class ViewerLoginDialog {
/**
* @param {!Viewer} viewer
* @param {string|!Promise<string>} urlOrPromise
*/
constructor(viewer, urlOrPromise) {
/** @const {!Viewer} */
this.viewer = viewer;
/** @const {string|!Promise<string>} */
this.urlOrPromise = urlOrPromise;
}
getLoginUrl() {
let urlPromise;
if (typeof this.urlOrPromise == 'string') {
urlPromise = Promise.resolve(this.urlOrPromise);
} else {
urlPromise = this.urlOrPromise;
}
return urlPromise.then(url => {
return buildLoginUrl(url, 'RETURN_URL');
});
}
/**
* Opens the dialog. Returns the promise that will yield with the dialog's
* result or will be rejected if dialog fails. The dialog's result is
* typically a hash string from the return URL.
* @return {!Promise<string>}
*/
open() {
return this.getLoginUrl().then(loginUrl => {
dev().fine(TAG, 'Open viewer dialog: ', loginUrl);
return this.viewer.sendMessageAwaitResponse('openDialog', {
'url': loginUrl,
});
});
}
}
/**
* Web-based implementation of the Login Dialog.
* @visibleForTesting
*/
export class WebLoginDialog {
/**
* @param {!Window} win
* @param {!Viewer} viewer
* @param {string|!Promise<string>} urlOrPromise
*/
constructor(win, viewer, urlOrPromise) {
/** @const {!Window} */
this.win = win;
/** @const {!Viewer} */
this.viewer = viewer;
/** @const {string|!Promise<string>} */
this.urlOrPromise = urlOrPromise;
/** @private {?function(string)} */
this.resolve_ = null;
/** @private {?function(*)} */
this.reject_ = null;
/** @private {?Window} */
this.dialog_ = null;
/** @private {?Promise} */
this.dialogReadyPromise_ = null;
/** @private {?number} */
this.heartbeatInterval_ = null;
/** @private {?Unlisten} */
this.messageUnlisten_ = null;
}
/**
* Opens the dialog. Returns the promise that will yield with the dialog's
* result or will be rejected if dialog fails.
* @return {!Promise<string>}
*/
open() {
user().assert(!this.resolve_, 'Dialog already opened');
return new Promise((resolve, reject) => {
this.resolve_ = resolve;
this.reject_ = reject;
// Must always be called synchronously.
this.openInternal_();
}).then(result => {
this.cleanup_();
return result;
}, error => {
this.cleanup_();
throw error;
});
}
/** @private */
cleanup_() {
this.resolve_ = null;
this.reject_ = null;
if (this.dialog_) {
try {
this.dialog_.close();
} catch (e) {
// Ignore.
}
this.dialog_ = null;
}
if (this.heartbeatInterval_) {
this.win.clearInterval(this.heartbeatInterval_);
this.heartbeatInterval_ = null;
}
if (this.messageUnlisten_) {
this.messageUnlisten_();
this.messageUnlisten_ = null;
}
}
getLoginUrl() {
const returnUrl = this.getReturnUrl_();
return buildLoginUrl(this.urlOrPromise, returnUrl);
}
/** @private */
openInternal_() {
const screen = this.win.screen;
const w = Math.floor(Math.min(700, screen.width * 0.9));
const h = Math.floor(Math.min(450, screen.height * 0.9));
const x = Math.floor((screen.width - w) / 2);
const y = Math.floor((screen.height - h) / 2);
const sizing = `height=${h},width=${w},left=${x},top=${y}`;
const options = `${sizing},resizable=yes,scrollbars=yes`;
const returnUrl = this.getReturnUrl_();
this.dialogReadyPromise_ = null;
if (typeof this.urlOrPromise == 'string') {
const loginUrl = buildLoginUrl(this.urlOrPromise, returnUrl);
dev().fine(TAG, 'Open dialog: ', loginUrl, returnUrl, w, h, x, y);
this.dialog_ = openWindowDialog(this.win, loginUrl, '_blank', options);
if (this.dialog_) {
this.dialogReadyPromise_ = Promise.resolve();
}
} else {
dev().fine(TAG, 'Open dialog: ', 'about:blank', returnUrl, w, h, x, y);
this.dialog_ = openWindowDialog(this.win, '', '_blank', options);
if (this.dialog_) {
this.dialogReadyPromise_ = this.urlOrPromise.then(url => {
const loginUrl = buildLoginUrl(url, returnUrl);
dev().fine(TAG, 'Set dialog url: ', loginUrl);
this.dialog_.location.replace(loginUrl);
}, error => {
throw new Error('failed to resolve url: ' + error);
});
}
}
if (this.dialogReadyPromise_) {
this.dialogReadyPromise_.then(() => {
this.setupDialog_(returnUrl);
}, error => {
this.loginDone_(/* result */ null, error);
});
} else {
this.loginDone_(/* result */ null, new Error('failed to open dialog'));
}
}
/**
* @param {string} returnUrl
* @private
*/
setupDialog_(returnUrl) {
const returnOrigin = parseUrl(returnUrl).origin;
this.heartbeatInterval_ = this.win.setInterval(() => {
if (this.dialog_.closed) {
this.win.clearInterval(this.heartbeatInterval_);
this.heartbeatInterval_ = null;
// Give a chance for the result to arrive, but otherwise consider the
// responce to be empty.
this.win.setTimeout(() => {
this.loginDone_('');
}, 3000);
}
}, 500);
this.messageUnlisten_ = listen(this.win, 'message', e => {
dev().fine(TAG, 'MESSAGE:', e);
if (e.origin != returnOrigin) {
return;
}
if (!e.data || e.data.sentinel != 'amp') {
return;
}
dev().fine(TAG, 'Received message from dialog: ', e.data);
if (e.data.type == 'result') {
if (this.dialog_) {
this.dialog_./*OK*/postMessage({
sentinel: 'amp',
type: 'result-ack',
}, returnOrigin);
}
this.loginDone_(e.data.result);
}
});
}
/**
* @param {?string} result
* @param {*=} opt_error
* @private
*/
loginDone_(result, opt_error) {
if (!this.resolve_) {
return;
}
dev().fine(TAG, 'Login done: ', result, opt_error);
if (opt_error) {
this.reject_(opt_error);
} else {
this.resolve_(result);
}
this.cleanup_();
}
/**
* @return {string}
* @private
*/
getReturnUrl_() {
const currentUrl = this.viewer.getResolvedViewerUrl();
let returnUrl;
if (getMode().localDev) {
const loc = this.win.location;
returnUrl = loc.protocol + '//' + loc.host +
'/extensions/amp-access/0.1/amp-login-done.html';
} else {
returnUrl = `${urls.cdn}/v0/amp-login-done-0.1.html`;
}
return returnUrl + '?url=' + encodeURIComponent(currentUrl);
}
}
/**
* @param {string} url
* @param {string} returnUrl
* @return {string}
*/
function buildLoginUrl(url, returnUrl) {
// RETURN_URL has to arrive here unreplaced by UrlReplacements for two
// reasons: (1) sync replacement and (2) if we need to propagate this
// replacement to the viewer.
if (RETURN_URL_REGEX.test(url)) {
return url.replace(RETURN_URL_REGEX, encodeURIComponent(returnUrl));
}
return url +
(url.indexOf('?') == -1 ? '?' : '&') +
'return=' + encodeURIComponent(returnUrl);
}