-
Notifications
You must be signed in to change notification settings - Fork 875
/
ifchandler.js
1076 lines (985 loc) · 34.5 KB
/
ifchandler.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2017 Google Inc.
*
* 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.
*/
/**
* @fileoverview Defines fireauth.iframeclient.IfcHandler used to communicate
* with the serverless widget.
*/
goog.provide('fireauth.iframeclient.IfcHandler');
goog.provide('fireauth.iframeclient.IframeUrlBuilder');
goog.provide('fireauth.iframeclient.OAuthUrlBuilder');
goog.require('fireauth.AuthError');
goog.require('fireauth.AuthEvent');
goog.require('fireauth.AuthProvider');
goog.require('fireauth.InvalidOriginError');
goog.require('fireauth.OAuthSignInHandler');
goog.require('fireauth.RpcHandler');
goog.require('fireauth.authenum.Error');
goog.require('fireauth.constants');
goog.require('fireauth.iframeclient.IframeWrapper');
goog.require('fireauth.util');
goog.require('goog.Promise');
goog.require('goog.Timer');
goog.require('goog.Uri');
goog.require('goog.array');
goog.require('goog.object');
/**
* The OAuth handler and iframe prototcol.
* @const {string}
* @suppress {const|duplicate}
*/
fireauth.iframeclient.SCHEME = 'https';
/**
* The OAuth handler and iframe port number.
* @const {?number}
* @suppress {const|duplicate}
*/
fireauth.iframeclient.PORT_NUMBER = null;
/**
* The iframe URL builder used to build the iframe widget URL.
* @param {string} authDomain The application authDomain.
* @param {string} apiKey The API key.
* @param {string} appName The App name.
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
* configuration.
* @constructor
*/
fireauth.iframeclient.IframeUrlBuilder = function(authDomain, apiKey, appName, emulatorConfig) {
/** @private {string} The application authDomain. */
this.authDomain_ = authDomain;
/** @private {string} The API key. */
this.apiKey_ = apiKey;
/** @private {string} The App name. */
this.appName_ = appName;
/**
* @private @const {?fireauth.constants.EmulatorSettings|undefined}
* The emulator configuration.
*/
this.emulatorConfig_ = emulatorConfig;
/** @private {?string|undefined} The client version. */
this.v_ = null;
let uri;
if (this.emulatorConfig_) {
const emulatorUri = goog.Uri.parse(this.emulatorConfig_.url);
uri = goog.Uri.create(
emulatorUri.getScheme(),
null,
emulatorUri.getDomain(),
emulatorUri.getPort(),
'/emulator/auth/iframe',
null,
null);
} else {
uri = goog.Uri.create(
fireauth.iframeclient.SCHEME,
null,
this.authDomain_,
fireauth.iframeclient.PORT_NUMBER,
'/__/auth/iframe',
null,
null);
}
/**
* @private @const {!goog.Uri} The URI object used to build the iframe URL.
*/
this.uri_ = uri;
this.uri_.setParameterValue('apiKey', this.apiKey_);
this.uri_.setParameterValue('appName', this.appName_);
/** @private {?string|undefined} The endpoint ID. */
this.endpointId_ = null;
/** @private {!Array<string>} The list of framework IDs. */
this.frameworks_ = [];
};
/**
* Sets the client version.
* @param {?string|undefined} v The client version.
* @return {!fireauth.iframeclient.IframeUrlBuilder} The current iframe URL
* builder instance.
*/
fireauth.iframeclient.IframeUrlBuilder.prototype.setVersion = function(v) {
this.v_ = v;
return this;
};
/**
* Sets the endpoint ID.
* @param {?string|undefined} eid The endpoint ID (staging, test Gaia, etc).
* @return {!fireauth.iframeclient.IframeUrlBuilder} The current iframe URL
* builder instance.
*/
fireauth.iframeclient.IframeUrlBuilder.prototype.setEndpointId = function(eid) {
this.endpointId_ = eid;
return this;
};
/**
* Sets the list of frameworks to pass to the iframe.
* @param {?Array<string>|undefined} frameworks The list of frameworks to log.
* @return {!fireauth.iframeclient.IframeUrlBuilder} The current iframe URL
* builder instance.
*/
fireauth.iframeclient.IframeUrlBuilder.prototype.setFrameworks =
function(frameworks) {
this.frameworks_ = goog.array.clone(frameworks || []);
return this;
};
/**
* Modifes the URI with the relevant Auth provider parameters.
* @return {string} The constructed OAuth URL string.
* @override
*/
fireauth.iframeclient.IframeUrlBuilder.prototype.toString = function() {
// Pass the client version if available.
if (this.v_) {
this.uri_.setParameterValue('v', this.v_);
} else {
this.uri_.removeParameter('v');
}
// Pass the endpoint ID if available.
if (this.endpointId_) {
this.uri_.setParameterValue('eid', this.endpointId_);
} else {
this.uri_.removeParameter('eid');
}
// Pass the list of frameworks if available.
if (this.frameworks_.length) {
this.uri_.setParameterValue('fw', this.frameworks_.join(','));
} else {
this.uri_.removeParameter('fw');
}
return this.uri_.toString();
};
/**
* The OAuth URL builder used to build the OAuth handler widget URL.
* @param {string} authDomain The application authDomain.
* @param {string} apiKey The API key.
* @param {string} appName The App name.
* @param {string} authType The Auth operation type.
* @param {!fireauth.AuthProvider} provider The Auth provider that the OAuth
* handler request is built to sign in to.
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
* configuration.
* @constructor
*/
fireauth.iframeclient.OAuthUrlBuilder =
function(authDomain, apiKey, appName, authType, provider, emulatorConfig) {
/** @private {string} The application authDomain. */
this.authDomain_ = authDomain;
/** @private {string} The API key. */
this.apiKey_ = apiKey;
/** @private {string} The App name. */
this.appName_ = appName;
/** @private {string} The Auth operation type. */
this.authType_ = authType;
/**
* @private @const {?fireauth.constants.EmulatorSettings|undefined}
* The emulator configuration.
*/
this.emulatorConfig_ = emulatorConfig;
/**
* @private {?string|undefined} The redirect URL used in redirect operations.
*/
this.redirectUrl_ = null;
/** @private {?string|undefined} The event ID. */
this.eventId_ = null;
/** @private {?string|undefined} The client version. */
this.v_ = null;
/**
* @private {!fireauth.AuthProvider} The Firebase Auth provider that the OAuth
* handler request is built to sign in to.
*/
this.provider_ = provider;
/** @private {?string|undefined} The endpoint ID. */
this.endpointId_ = null;
/** @private {?string|undefined} The tenant ID. */
this.tenantId_ = null;
};
/**
* Sets the redirect URL.
* @param {?string|undefined} redirectUrl The redirect URL used in redirect
* operations.
* @return {!fireauth.iframeclient.OAuthUrlBuilder} The current OAuth URL
* builder instance.
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.setRedirectUrl =
function(redirectUrl) {
this.redirectUrl_ = redirectUrl;
return this;
};
/**
* Sets the event ID.
* @param {?string|undefined} eventId The event ID.
* @return {!fireauth.iframeclient.OAuthUrlBuilder} The current OAuth URL
* builder instance.
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.setEventId = function(eventId) {
this.eventId_ = eventId;
return this;
};
/**
* Sets the tenant ID.
* @param {?string|undefined} tenantId The event ID.
* @return {!fireauth.iframeclient.OAuthUrlBuilder} The current OAuth URL
* builder instance.
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.setTenantId =
function(tenantId) {
this.tenantId_ = tenantId;
return this;
};
/**
* Sets the client version.
* @param {?string|undefined} v The client version.
* @return {!fireauth.iframeclient.OAuthUrlBuilder} The current OAuth URL
* builder instance.
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.setVersion = function(v) {
this.v_ = v;
return this;
};
/**
* Sets the endpoint ID.
* @param {?string|undefined} eid The endpoint ID (staging, test Gaia, etc).
* @return {!fireauth.iframeclient.OAuthUrlBuilder} The current OAuth URL
* builder instance.
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.setEndpointId = function(eid) {
this.endpointId_ = eid;
return this;
};
/**
* Sets any additional optional parameters. This will overwrite any previously
* set additional parameters.
* @param {?Object<string, string>|undefined} additionalParams The optional
* additional parameters.
* @return {!fireauth.iframeclient.OAuthUrlBuilder} The current OAuth URL
* builder instance.
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.setAdditionalParameters =
function(additionalParams) {
this.additionalParams_ = goog.object.clone(additionalParams || null);
return this;
};
/**
* Modifies the URI with the relevant Auth provider parameters.
* @return {string} The constructed OAuth URL string.
* @override
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.toString = function () {
var uri;
if (this.emulatorConfig_) {
const emulatorUri = goog.Uri.parse(this.emulatorConfig_.url);
uri = goog.Uri.create(
emulatorUri.getScheme(),
null,
emulatorUri.getDomain(),
emulatorUri.getPort(),
'/emulator/auth/handler',
null,
null);
} else {
uri = goog.Uri.create(
fireauth.iframeclient.SCHEME,
null,
this.authDomain_,
fireauth.iframeclient.PORT_NUMBER,
'/__/auth/handler',
null,
null);
}
uri.setParameterValue('apiKey', this.apiKey_);
uri.setParameterValue('appName', this.appName_);
uri.setParameterValue('authType', this.authType_);
// Add custom parameters for OAuth1/OAuth2 providers.
if (this.provider_['isOAuthProvider']) {
// Set default language if available and no language already set.
/** @type {!fireauth.FederatedProvider} */ (this.provider_)
.setDefaultLanguage(this.getAuthLanguage_());
uri.setParameterValue('providerId', this.provider_['providerId']);
var customParameters = /** @type {!fireauth.FederatedProvider} */ (
this.provider_).getCustomParameters();
if (!goog.object.isEmpty(customParameters)) {
uri.setParameterValue(
'customParameters',
/** @type {string} */ (fireauth.util.stringifyJSON(customParameters))
);
}
}
// Add scopes for OAuth2 providers.
if (typeof this.provider_.getScopes === 'function') {
var scopes = this.provider_.getScopes();
if (scopes.length) {
uri.setParameterValue('scopes', scopes.join(','));
}
}
if (this.redirectUrl_) {
uri.setParameterValue('redirectUrl', this.redirectUrl_);
} else {
uri.removeParameter('redirectUrl');
}
if (this.eventId_) {
uri.setParameterValue('eventId', this.eventId_);
} else {
uri.removeParameter('eventId');
}
// Pass the client version if available.
if (this.v_) {
uri.setParameterValue('v', this.v_);
} else {
uri.removeParameter('v');
}
if (this.additionalParams_) {
for (var key in this.additionalParams_) {
if (this.additionalParams_.hasOwnProperty(key) &&
// Don't overwrite other existing parameters.
!uri.getParameterValue(key)) {
uri.setParameterValue(key, this.additionalParams_[key]);
}
}
}
// Pass the tenant ID if available.
if (this.tenantId_) {
uri.setParameterValue('tid', this.tenantId_);
} else {
uri.removeParameter('tid');
}
// Pass the endpoint ID if available.
if (this.endpointId_) {
uri.setParameterValue('eid', this.endpointId_);
} else {
uri.removeParameter('eid');
}
// Append any framework IDs to the handler URL to log in handler RPC requests.
var frameworks = this.getAuthFrameworks_();
if (frameworks.length) {
uri.setParameterValue('fw', frameworks.join(','));
}
return uri.toString();
};
/**
* Returns the current Auth instance's language code.
* @return {?string} The corresponding language code.
* @private
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.getAuthLanguage_ = function() {
try {
// Get the Auth instance for the current App identified by the App name.
// This could fail if, for example, the App instance was deleted.
return firebase['app'](this.appName_)['auth']().getLanguageCode();
} catch (e) {
return null;
}
};
/**
* Returns the list of Firebase frameworks used for logging purposes.
* @return {!Array<string>} The list of corresponding Firebase frameworks.
* @private
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.getAuthFrameworks_ =
function() {
return fireauth.iframeclient.OAuthUrlBuilder.getAuthFrameworksForApp_(
this.appName_);
};
/**
* Returns the list of Firebase frameworks used for logging purposes
* corresponding to the Firebase App name provided.
* @param {string} appName The Firebase App name.
* @return {!Array<string>} The list of corresponding Firebase frameworks.
* @private
*/
fireauth.iframeclient.OAuthUrlBuilder.getAuthFrameworksForApp_ =
function(appName) {
try {
// Get the Auth instance's list of Firebase framework IDs for the current
// App identified by the App name.
// This could fail if, for example, the App instance was deleted.
return firebase['app'](appName)['auth']().getFramework();
} catch (e) {
return [];
}
};
/**
* Initializes the ifcHandler which provides the mechanism to listen to Auth
* events on the hidden iframe.
* @param {string} authDomain The firebase authDomain used to determine the
* OAuth helper page domain.
* @param {string} apiKey The API key for sending backend Auth requests.
* @param {string} appName The App ID for the Auth instance that triggered this
* request.
* @param {?string=} opt_clientVersion The optional client version string.
* @param {?string=} opt_endpointId The endpoint ID (staging, test Gaia, etc).
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
* configuration.
* @constructor
* @implements {fireauth.OAuthSignInHandler}
*/
fireauth.iframeclient.IfcHandler = function(authDomain, apiKey, appName,
opt_clientVersion, opt_endpointId, emulatorConfig) {
/** @private {string} The Auth domain. */
this.authDomain_ = authDomain;
/** @private {string} The API key. */
this.apiKey_ = apiKey;
/** @private {string} The App name. */
this.appName_ = appName;
/**
* @private @const {?fireauth.constants.EmulatorSettings|undefined}
* The emulator configuration.
*/
this.emulatorConfig_ = emulatorConfig;
/** @private {?string} The client version. */
this.clientVersion_ = opt_clientVersion || null;
/** @private {?string} The Auth endpoint ID. */
this.endpointId_ = opt_endpointId || null;
// Delay RPC handler and iframe URL initialization until needed to ensure
// logged frameworks are propagated to the iframe.
/** @private {?string} The full client version string. */
this.fullClientVersion_ = null;
/** @private {?string} The iframe URL. */
this.iframeUrl_ = null;
/** @private {?fireauth.RpcHandler} The RPC handler for provided API key. */
this.rpcHandler_ = null;
/**
* @private {!Array<!function(?fireauth.AuthEvent)>} The Auth event
* listeners.
*/
this.authEventListeners_ = [];
// Delay origin validator determination until needed, so the error is not
// thrown in the background. This will also prevent the getProjectConfig RPC
// until it is required.
/** @private {?goog.Promise} The origin validator. */
this.originValidator_ = null;
/** @private {?goog.Promise} The initialization promise. */
this.isInitialized_ = null;
};
/**
* Validates the provided URL.
* @param {!fireauth.RpcHandler} rpcHandler The RPC handler used to validate the
* requested origin.
* @param {string=} opt_origin The optional page origin. If not provided, the
* window.location.href value is used.
* @return {!goog.Promise} The promise that resolves if the provided origin is
* valid.
* @private
*/
fireauth.iframeclient.IfcHandler.getOriginValidator_ =
function(rpcHandler, opt_origin) {
var origin = opt_origin || fireauth.util.getCurrentUrl();
return rpcHandler.getAuthorizedDomains().then(function(authorizedDomains) {
if (!fireauth.util.isAuthorizedDomain(authorizedDomains, origin)) {
throw new fireauth.InvalidOriginError(fireauth.util.getCurrentUrl());
}
});
};
/**
* Initializes the iframe client wrapper.
* @return {!goog.Promise} The promise that resolves on initialization.
*/
fireauth.iframeclient.IfcHandler.prototype.initialize = function() {
// Already initialized.
if (this.isInitialized_) {
return this.isInitialized_;
}
var self = this;
this.isInitialized_ = fireauth.util.onDomReady().then(function() {
/**
* @private {!fireauth.iframeclient.IframeWrapper} The iframe wrapper
* instance.
*/
self.iframeWrapper_ = new fireauth.iframeclient.IframeWrapper(
self.getIframeUrl());
// Register all event listeners to Auth event messages sent from Auth
// iframe.
self.registerEvents_();
});
return this.isInitialized_;
};
/**
* Waits for popup window to close. When closed start timeout listener for popup
* pending promise. If in the process, it was detected that the iframe does not
* support web storage, the popup is closed and the web storage unsupported
* error is thrown.
* @param {!Window} popupWin The popup window.
* @param {!function(!fireauth.AuthError)} onError The on error callback.
* @param {number} timeoutDuration The time to wait in ms after the popup is
* closed before triggering the popup closed by user error.
* @return {!goog.Promise}
* @override
*/
fireauth.iframeclient.IfcHandler.prototype.startPopupTimeout =
function(popupWin, onError, timeoutDuration) {
// Expire pending timeout promise for popup operation.
var popupClosedByUserError = new fireauth.AuthError(
fireauth.authenum.Error.POPUP_CLOSED_BY_USER);
// If web storage is disabled in the iframe, expire popup timeout quickly with
// this error.
var webStorageNotSupportedError = new fireauth.AuthError(
fireauth.authenum.Error.WEB_STORAGE_UNSUPPORTED);
var self = this;
var isResolved = false;
// Wait for the iframe to be ready first.
return this.initializeAndWait().then(function() {
// We do not return isWebStorageSupported() to ensure that this is backward
// compatible.
// Pushing the following client changes before updating the iframe to
// respond to these events would continue to work.
// The downside is that the popup could be closed before this resolves.
// In that case, they would get an error that the popup was closed and not
// the error that web storage is not supported, though that is unlikely
// as isWebStorageSupported should execute faster than the popup timeout.
// If web storage is not supported in the iframe, fail quickly.
self.isWebStorageSupported().then(function(isSupported) {
if (!isSupported) {
// If not supported, close window.
if (popupWin) {
fireauth.util.closeWindow(popupWin);
}
onError(webStorageNotSupportedError);
isResolved = true;
}
});
}).thenCatch(function(error) {
// Ignore any possible error in iframe embedding.
// These types of errors will be handled in processPopup which will close
// the popup too if that happens.
return;
}).then(function() {
// Skip if already resolved.
if (isResolved) {
return;
}
// After the iframe is ready, wait for popup to close and then start timeout
// check.
return fireauth.util.onPopupClose(popupWin);
}).then(function() {
// Skip if already resolved.
if (isResolved) {
return;
}
return goog.Timer.promise(timeoutDuration).then(function() {
// If this is already resolved or rejected, this will do nothing.
onError(popupClosedByUserError);
});
});
};
/**
* @return {boolean} Whether the handler should be initialized early.
* @override
*/
fireauth.iframeclient.IfcHandler.prototype.shouldBeInitializedEarly =
function() {
var ua = fireauth.util.getUserAgentString();
// Cannot run in the background (can't wait for iframe to be embedded
// before triggering popup redirect) and is Safari (can only detect
// localStorage in iframe via change event) => embed iframe ASAP.
// Do the same for mobile browsers on iOS devices as they use the same
// Safari implementation underneath.
return !fireauth.util.runsInBackground(ua) &&
!fireauth.util.iframeCanSyncWebStorage(ua);
};
/**
* @return {boolean} Whether the sign-in handler in the current environment
* has volatile session storage.
* @override
*/
fireauth.iframeclient.IfcHandler.prototype.hasVolatileStorage = function() {
// Web environment with web storage enabled has stable sessionStorage.
return false;
};
/**
* Processes the popup request. The popup instance must be provided externally
* and on error, the requestor must close the window.
* @param {?Window} popupWin The popup window reference.
* @param {!fireauth.AuthEvent.Type} mode The Auth event type.
* @param {!fireauth.AuthProvider} provider The Auth provider to sign in with.
* @param {function()} onInitialize The function to call on initialization.
* @param {function(*)} onError The function to call on error.
* @param {string=} opt_eventId The optional event ID.
* @param {boolean=} opt_alreadyRedirected Whether popup is already redirected
* to final destination.
* @param {?string=} opt_tenantId The optional tenant ID.
* @return {!goog.Promise} The popup window promise.
* @override
*/
fireauth.iframeclient.IfcHandler.prototype.processPopup = function(
popupWin,
mode,
provider,
onInitialize,
onError,
opt_eventId,
opt_alreadyRedirected,
opt_tenantId) {
// processPopup is failing since it tries to access popup win when tab can
// not run in background. For now bypass processPopup which runs
// additional origin check not accounted above. Besides, iframe will never
// hand result to parent if origin not whitelisted.
// Error thrown by browser: Unable to establish a connection with the
// popup. It may have been blocked by the browser.
// If popup is null, startPopupTimeout will catch it without having the
// above error getting triggered due to popup access from opener.
// Reject immediately if the popup is blocked.
if (!popupWin) {
return goog.Promise.reject(
new fireauth.AuthError(fireauth.authenum.Error.POPUP_BLOCKED));
}
// Already redirected and cannot run in the background, resolve quickly while
// initializing.
if (opt_alreadyRedirected && !fireauth.util.runsInBackground()) {
// Initialize first before resolving.
this.initializeAndWait().thenCatch(function(error) {
fireauth.util.closeWindow(popupWin);
onError(error);
});
onInitialize();
// Already redirected.
return goog.Promise.resolve();
}
// If origin validator not determined yet.
if (!this.originValidator_) {
this.originValidator_ =
fireauth.iframeclient.IfcHandler.getOriginValidator_(
this.getRpcHandler_());
}
var self = this;
return this.originValidator_.then(function() {
// After origin validation, wait for iframe to be ready before redirecting.
var onReady = self.initializeAndWait().thenCatch(function(error) {
fireauth.util.closeWindow(popupWin);
onError(error);
throw error;
});
onInitialize();
return onReady;
}).then(function() {
// Popup and redirect operations work for OAuth providers only.
fireauth.AuthProvider.checkIfOAuthSupported(provider);
// Already redirected to intended destination, no need to redirect again.
if (opt_alreadyRedirected) {
return;
}
var oauthHelperWidgetUrl =
fireauth.iframeclient.IfcHandler.getOAuthHelperWidgetUrl(
self.authDomain_,
self.apiKey_,
self.appName_,
mode,
provider,
null,
opt_eventId,
self.clientVersion_,
undefined,
self.endpointId_,
opt_tenantId,
self.emulatorConfig_);
// Redirect popup to OAuth helper widget URL.
fireauth.util.goTo(oauthHelperWidgetUrl, /** @type {!Window} */ (popupWin));
}).thenCatch(function(e) {
// Force another origin validation.
if (e.code == 'auth/network-request-failed') {
self.originValidator_ = null;
}
throw e;
});
};
/**
* @return {!fireauth.RpcHandler} The RPC handler instance with the relevant
* endpoints, version and frameworks.
* @private
*/
fireauth.iframeclient.IfcHandler.prototype.getRpcHandler_ = function() {
if (!this.rpcHandler_) {
this.fullClientVersion_ = this.clientVersion_ ?
fireauth.util.getClientVersion(
fireauth.util.ClientImplementation.JSCORE,
this.clientVersion_,
fireauth.iframeclient.OAuthUrlBuilder.getAuthFrameworksForApp_(
this.appName_)) :
null;
this.rpcHandler_ = new fireauth.RpcHandler(
this.apiKey_,
// Get the client Auth endpoint used.
fireauth.constants.getEndpointConfig(this.endpointId_),
this.fullClientVersion_);
}
return this.rpcHandler_;
};
/**
* Processes the redirect request.
* @param {!fireauth.AuthEvent.Type} mode The Auth event type.
* @param {!fireauth.AuthProvider} provider The Auth provider to sign in with.
* @param {?string=} opt_eventId The optional event ID.
* @param {?string=} opt_tenantId The optional tenant ID.
* @return {!goog.Promise}
* @override
*/
fireauth.iframeclient.IfcHandler.prototype.processRedirect =
function(mode, provider, opt_eventId, opt_tenantId) {
// If origin validator not determined yet.
if (!this.originValidator_) {
this.originValidator_ =
fireauth.iframeclient.IfcHandler.getOriginValidator_(
this.getRpcHandler_());
}
var self = this;
// Make sure origin is validated.
return this.originValidator_.then(function() {
fireauth.AuthProvider.checkIfOAuthSupported(provider);
var oauthHelperWidgetUrl =
fireauth.iframeclient.IfcHandler.getOAuthHelperWidgetUrl(
self.authDomain_,
self.apiKey_,
self.appName_,
mode,
provider,
fireauth.util.getCurrentUrl(),
opt_eventId,
self.clientVersion_,
undefined,
self.endpointId_,
opt_tenantId,
self.emulatorConfig_);
// Redirect to OAuth helper widget URL.
fireauth.util.goTo(oauthHelperWidgetUrl);
}).thenCatch(function(e) {
// Force another origin validation on network errors.
if (e.code == 'auth/network-request-failed') {
self.originValidator_ = null;
}
throw e;
});
};
/** @return {string} The iframe URL. */
fireauth.iframeclient.IfcHandler.prototype.getIframeUrl = function() {
if (!this.iframeUrl_) {
this.iframeUrl_ = fireauth.iframeclient.IfcHandler.getAuthIframeUrl(
this.authDomain_,
this.apiKey_,
this.appName_,
this.clientVersion_,
this.endpointId_,
fireauth.iframeclient.OAuthUrlBuilder.getAuthFrameworksForApp_(
this.appName_),
this.emulatorConfig_);
}
return this.iframeUrl_;
};
/**
* @return {!goog.Promise} The promise that resolves when the iframe is ready.
* @override
*/
fireauth.iframeclient.IfcHandler.prototype.initializeAndWait = function() {
// Initialize if not initialized yet.
var self = this;
return this.initialize().then(function() {
return self.iframeWrapper_.onReady();
}).thenCatch(function(error) {
// Reset origin validator.
self.originValidator_ = null;
// Reject iframe ready promise with network error.
throw new fireauth.AuthError(
fireauth.authenum.Error.NETWORK_REQUEST_FAILED);
});
};
/**
* @return {boolean} Whether the handler will unload the current page on
* redirect operations.
* @override
*/
fireauth.iframeclient.IfcHandler.prototype.unloadsOnRedirect = function() {
return true;
};
/**
* @param {string} authDomain The Firebase authDomain used to determine the
* OAuth helper page domain.
* @param {string} apiKey The API key for sending backend Auth requests.
* @param {string} appName The App ID for the Auth instance that triggered this
* request.
* @param {?string=} opt_clientVersion The optional client version string.
* @param {?string=} opt_endpointId The endpoint ID (staging, test Gaia, etc).
* @param {?Array<string>=} opt_frameworks The optional list of framework IDs.
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
* configuration.
* @return {string} The data iframe src URL.
*/
fireauth.iframeclient.IfcHandler.getAuthIframeUrl = function(authDomain, apiKey,
appName, opt_clientVersion, opt_endpointId, opt_frameworks, emulatorConfig) {
// OAuth helper iframe URL.
var builder = new fireauth.iframeclient.IframeUrlBuilder(
authDomain, apiKey, appName, emulatorConfig);
return builder
.setVersion(opt_clientVersion)
.setEndpointId(opt_endpointId)
.setFrameworks(opt_frameworks)
.toString();
};
/**
* @param {string} authDomain The Firebase authDomain used to determine the
* OAuth helper page domain.
* @param {string} apiKey The API key for sending backend Auth requests.
* @param {string} appName The App ID for the Auth instance that triggered this
* request.
* @param {string} authType The type of operation that depends on OAuth sign in.
* @param {!fireauth.AuthProvider} provider The provider to sign in to.
* @param {?string=} opt_redirectUrl The optional URL to redirect to on OAuth
* sign in completion.
* @param {?string=} opt_eventId The optional event ID to identify on receipt.
* @param {?string=} opt_clientVersion The optional client version string.
* @param {?Object<string, string>=} opt_additionalParams The optional
* additional parameters.
* @param {?string=} opt_endpointId The endpoint ID (staging, test Gaia, etc).
* @param {?string=} opt_tenantId The optional tenant ID.
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
* configuration.
* @return {string} The OAuth helper widget URL.
*/
fireauth.iframeclient.IfcHandler.getOAuthHelperWidgetUrl = function(
authDomain,
apiKey,
appName,
authType,
provider,
opt_redirectUrl,
opt_eventId,
opt_clientVersion,
opt_additionalParams,
opt_endpointId,
opt_tenantId,
emulatorConfig) {
// OAuth helper widget URL.
var builder = new fireauth.iframeclient.OAuthUrlBuilder(
authDomain, apiKey, appName, authType, provider, emulatorConfig);
return builder
.setRedirectUrl(opt_redirectUrl)
.setEventId(opt_eventId)
.setVersion(opt_clientVersion)
.setAdditionalParameters(opt_additionalParams)
.setEndpointId(opt_endpointId)
.setTenantId(opt_tenantId)
.toString();
};
/**
* Post message receiver event names.
* @enum {string}
*/
fireauth.iframeclient.IfcHandler.ReceiverEvent = {
AUTH_EVENT: 'authEvent'
};
/**
* Post message sender event names.
* @enum {string}
*/
fireauth.iframeclient.IfcHandler.SenderEvent = {
WEB_STORAGE_SUPPORT_EVENT: 'webStorageSupport'
};
/**
* Post message response field names.
* @enum {string}
*/
fireauth.iframeclient.IfcHandler.Response = {
STATUS: 'status',
AUTH_EVENT: 'authEvent',
WEB_STORAGE_SUPPORT: 'webStorageSupport'
};
/**
* Post message status values.
* @enum {string}
*/
fireauth.iframeclient.IfcHandler.Status = {
ACK: 'ACK',
ERROR: 'ERROR'
};
/**
* Registers all event listeners.
* @private
*/
fireauth.iframeclient.IfcHandler.prototype.registerEvents_ = function() {
// Should be run in initialization.
if (!this.iframeWrapper_) {
throw new Error('IfcHandler must be initialized!');
}
var self = this;
// Listen to Auth change events emitted from iframe.
this.iframeWrapper_.registerEvent(
fireauth.iframeclient.IfcHandler.ReceiverEvent.AUTH_EVENT,
function(response) {