Skip to content

Commit

Permalink
Pull over more changes from internal repo
Browse files Browse the repository at this point in the history
  • Loading branch information
avolkovi committed Oct 1, 2020
1 parent b1dae4b commit 2f756a5
Show file tree
Hide file tree
Showing 14 changed files with 858 additions and 170 deletions.
75 changes: 46 additions & 29 deletions packages/auth/src/auth.js
Expand Up @@ -294,30 +294,57 @@ fireauth.Auth.prototype.useDeviceLanguage = function() {

/**
* Sets the emulator configuration (go/firebase-emulator-connection-api).
/**
* Sets the emulator configuration (go/firebase-emulator-connection-api).
* @param {string} url The url for the Auth emulator.
*/
* @param {string} url The url for the Auth emulator.
*/
fireauth.Auth.prototype.useEmulator = function(url) {
// Don't do anything if no change detected.
if (!this.emulatorConfig_ || url !== this.emulatorConfig_.url) {
console.warn("WARNING: You are using the Auth Emulator, which is" +
" intended for local testing only. Do not use with" +
" production credentials.");
this.emulatorConfig_ = { url: url };
// Emulator config can only be set once.
if (!this.emulatorConfig_) {
// Emit a warning so dev knows we are now in test mode.
this.emitEmulatorWarning_();
// Persist the config.
this.emulatorConfig_ = { url };
// Disable app verification.
this.settings_().setAppVerificationDisabledForTesting(true);
// Update custom Firebase locale field.
this.settings.setAppVerificationDisabledForTesting(true);
// Update RPC handler endpoints.
this.rpcHandler_.updateEmulatorConfig(this.emulatorConfig_);
// Notify external language code change listeners.
// Notify external event listeners.
this.notifyEmulatorConfigListeners_();
}
}


/**
* Emits a console warning and a visual banner if emulator integration is
* enabled.
*/
fireauth.Auth.prototype.emitEmulatorWarning_ = function() {
fireauth.util.consoleWarn('WARNING: You are using the Auth Emulator,' +
' which is intended for local testing only. Do not use with' +
' production credentials.');
if (goog.global.document) {
const ele = goog.global.document.createElement('p');
ele.innerText = 'Running in emulator mode. Do not use with production' +
' credentials.';
ele.style.position = 'fixed';
ele.style.width = '100%';
ele.style.backgroundColor = '#ffffff';
ele.style.border = '.1em solid #000000';
ele.style.color = '#ff0000';
ele.style.bottom = '0px';
ele.style.left = '0px';
ele.style.margin = '0px';
ele.style.zIndex = 10000;
ele.style.textAlign = 'center';
ele.classList.add('firebase-emulator-warning');
goog.global.document.body.appendChild(ele);
}
}


/**
* @return {?fireauth.constants.EmulatorSettings}
*/
fireauth.Auth.prototype.getEmulatorConfig = function () {
fireauth.Auth.prototype.getEmulatorConfig = function() {
return this.emulatorConfig_;
}

Expand Down Expand Up @@ -451,7 +478,7 @@ fireauth.Auth.prototype.notifyLanguageCodeListeners_ = function() {
* @private
*/
fireauth.Auth.prototype.notifyEmulatorConfigListeners_ = function() {
// Notify external listeners on the language code change.
// Notify external listeners on the emulator config change.
this.dispatchEvent(
new fireauth.Auth.EmulatorConfigChangeEvent(this.emulatorConfig_));
}
Expand Down Expand Up @@ -877,7 +904,6 @@ fireauth.Auth.prototype.updateCurrentUser = function(user) {
options['apiKey'] = this.app_().options['apiKey'];
options['authDomain'] = this.app_().options['authDomain'];
options['appName'] = this.app_().name;
options['emulatorConfig'] = this.emulatorConfig_;
var newUser = fireauth.AuthUser.copyUser(user, options,
self.redirectUserStorageManager_, self.getFramework());
return this.registerPendingPromise_(
Expand Down Expand Up @@ -922,7 +948,9 @@ fireauth.Auth.prototype.signInWithIdTokenResponse =
options['apiKey'] = self.app_().options['apiKey'];
options['authDomain'] = self.app_().options['authDomain'];
options['appName'] = self.app_().name;
options['emulatorConfig'] = self.emulatorConfig_;
if (self.emulatorConfig_) {
options['emulatorConfig'] = self.emulatorConfig_;
}
// Wait for state to be ready.
// This is used internally and is also used for redirect sign in so there is
// no need for waiting for redirect result to resolve since redirect result
Expand Down Expand Up @@ -1071,7 +1099,7 @@ fireauth.Auth.prototype.initAuthState_ = function() {
var p = this.initRedirectUser_().then(function() {
// Override user's authDomain with app's authDomain if there is a mismatch.
return /** @type {!fireauth.storage.UserManager} */ (
self.userStorageManager_).getCurrentUser(authDomain);
self.userStorageManager_).getCurrentUser(authDomain, self.emulatorConfig_);
}).then(function(user) {
// Logged in user.
if (user) {
Expand Down Expand Up @@ -1765,17 +1793,6 @@ fireauth.Auth.prototype.app_ = function() {
};



/**
* @return {!fireauth.AuthSettings} The AuthSettings object this auth object
* is connected to.
* @private
*/
fireauth.Auth.prototype.settings_ = function() {
return this['settings'];
}


/**
* @return {!fireauth.RpcHandler} The RPC handler.
*/
Expand Down
34 changes: 24 additions & 10 deletions packages/auth/src/autheventmanager.js
Expand Up @@ -672,12 +672,18 @@ fireauth.AuthEventManager.KEY_SEPARATOR_ = ':';
/**
* @param {string} apiKey The API key for sending backend Auth requests.
* @param {string} appName The Auth instance that initiated the Auth event.
* @return {string} The key identifying the Auth event manager instance.
* @private
*/
fireauth.AuthEventManager.getKey_ = function(apiKey, appName) {
return apiKey + fireauth.AuthEventManager.KEY_SEPARATOR_ + appName;
};
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
* configuration.
* @return {string} The key identifying the Auth event manager instance.
* @private
*/
fireauth.AuthEventManager.getKey_ = function(apiKey, appName, emulatorConfig) {
var key = apiKey + fireauth.AuthEventManager.KEY_SEPARATOR_ + appName;
if (emulatorConfig) {
key = key + fireauth.AuthEventManager.KEY_SEPARATOR_ + emulatorConfig.url;
}
return key;
}


/**
Expand All @@ -690,18 +696,26 @@ fireauth.AuthEventManager.getKey_ = function(apiKey, appName) {
* configuration.
* @return {!fireauth.AuthEventManager} the requested manager instance.
*/
fireauth.AuthEventManager.getManager = function(authDomain, apiKey, appName, emulatorConfig) {
fireauth.AuthEventManager.getManager = function (authDomain, apiKey, appName, emulatorConfig) {
// Construct storage key.
var key = fireauth.AuthEventManager.getKey_(apiKey, appName);
var key = fireauth.AuthEventManager.getKey_(
apiKey,
appName,
emulatorConfig
);
if (!fireauth.AuthEventManager.manager_[key]) {
fireauth.AuthEventManager.manager_[key] =
new fireauth.AuthEventManager(authDomain, apiKey, appName, emulatorConfig);
new fireauth.AuthEventManager(
authDomain,
apiKey,
appName,
emulatorConfig
);
}
return fireauth.AuthEventManager.manager_[key];
};



/**
* The interface that represents a specific type of Auth event processor.
* @interface
Expand Down
13 changes: 7 additions & 6 deletions packages/auth/src/authuser.js
Expand Up @@ -351,10 +351,10 @@ fireauth.AuthUser.prototype.setLanguageCodeChangeDispatcher =


/**
* Listens to emulator config changes triggered by the provided dispatcher.
* @param {?goog.events.EventTarget} dispatcher The emulator config changed
* event dispatcher.
*/
* Listens to emulator config changes triggered by the provided dispatcher.
* @param {?goog.events.EventTarget} dispatcher The emulator config changed
* event dispatcher.
*/
fireauth.AuthUser.prototype.setEmulatorConfigChangeDispatcher = function(dispatcher) {
// Remove any previous listener.
if (this.emulatorConfigChangeEventDispatcher_) {
Expand All @@ -367,7 +367,7 @@ fireauth.AuthUser.prototype.setEmulatorConfigChangeDispatcher = function(dispatc
this.emulatorConfigChangeEventDispatcher_ = dispatcher;
// Using an event listener makes it easy for non-currentUsers to detect
// emulator changes on the parent Auth instance. A developer could still
// call APIs that require localization on signed out user references.
// call APIs that require emulation on signed out user references.
if (dispatcher) {
goog.events.listen(
dispatcher, fireauth.constants.AuthEventType.EMULATOR_CONFIG_CHANGED,
Expand Down Expand Up @@ -2423,7 +2423,8 @@ fireauth.AuthUser.fromPlainObject = function(user) {
var options = {
'apiKey': user['apiKey'],
'authDomain': user['authDomain'],
'appName': user['appName']
'appName': user['appName'],
'emulatorConfig': user['emulatorConfig']
};
// Convert to server response format. Constructor does not take
// stsTokenManager toPlainObject as that format is different than the return
Expand Down
19 changes: 9 additions & 10 deletions packages/auth/src/cordovahandler.js
Expand Up @@ -56,28 +56,27 @@ goog.require('goog.crypt.Sha256');
* @param {string} authDomain The application authDomain.
* @param {string} apiKey The API key.
* @param {string} appName The App name.
* @param {?string=} opt_clientVersion The optional client version string.
* @param {number=} opt_initialTimeout Initial Auth event timeout.
* @param {number=} opt_redirectTimeout Redirect result timeout.
* @param {?string=} opt_endpointId The endpoint ID (staging, test Gaia, etc).
* @param {?string=} clientVersion The optional client version string.
* @param {number=} initialTimeout Initial Auth event timeout.
* @param {number=} redirectTimeout Redirect result timeout.
* @param {?string=} endpointId The endpoint ID (staging, test Gaia, etc).
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The emulator
* configuration
* @constructor
* @implements {fireauth.OAuthSignInHandler}
*/
fireauth.CordovaHandler = function(authDomain, apiKey, appName,
opt_clientVersion, opt_initialTimeout, opt_redirectTimeout,
opt_endpointId, emulatorConfig) {
clientVersion, initialTimeout, redirectTimeout, endpointId, emulatorConfig) {
/** @private {string} The application authDomain. */
this.authDomain_ = authDomain;
/** @private {string} The application API key. */
this.apiKey_ = apiKey;
/** @private {string} The application name. */
this.appName_ = appName;
/** @private {?string} The client version */
this.clientVersion_ = opt_clientVersion || null;
this.clientVersion_ = clientVersion || null;
/** @private {?string} The Auth endpoint ID. */
this.endpointId_ = opt_endpointId || null;
this.endpointId_ = endpointId || null;
/**
* @private @const {?fireauth.constants.EmulatorSettings|undefined}
* The emulator configuration
Expand Down Expand Up @@ -109,10 +108,10 @@ fireauth.CordovaHandler = function(authDomain, apiKey, appName,
*/
this.authEventListeners_ = [];
/** @private {number} The initial Auth event timeout. */
this.initialTimeout_ = opt_initialTimeout ||
this.initialTimeout_ = initialTimeout ||
fireauth.CordovaHandler.INITIAL_TIMEOUT_MS_;
/** @private {number} The return to app after redirect timeout. */
this.redirectTimeout_ = opt_redirectTimeout ||
this.redirectTimeout_ = redirectTimeout ||
fireauth.CordovaHandler.REDIRECT_TIMEOUT_MS_;
/**
* @private {?goog.Promise} The last pending redirect promise. This is null if
Expand Down
7 changes: 5 additions & 2 deletions packages/auth/src/iframeclient/ifchandler.js
Expand Up @@ -104,8 +104,8 @@ fireauth.iframeclient.IframeUrlBuilder = function(authDomain, apiKey, appName, e
null);
}
/**
* @private @const {!goog.Uri} The URI object used to build the iframe URL.
*/
* @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_);
Expand Down Expand Up @@ -774,6 +774,9 @@ fireauth.iframeclient.IfcHandler.prototype.getRpcHandler_ = function() {
// Get the client Auth endpoint used.
fireauth.constants.getEndpointConfig(this.endpointId_),
this.fullClientVersion_);
if (this.emulatorConfig_) {
this.rpcHandler_.updateEmulatorConfig(this.emulatorConfig_);
}
}
return this.rpcHandler_;
};
Expand Down
35 changes: 16 additions & 19 deletions packages/auth/src/rpchandler.js
Expand Up @@ -437,10 +437,10 @@ fireauth.RpcHandler.prototype.updateCustomLocaleHeader =


/**
* Updates the emulator configuration.
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The new
* emulator config.
*/
* Updates the emulator configuration.
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The new
* emulator config.
*/
fireauth.RpcHandler.prototype.updateEmulatorConfig = function(emulatorConfig) {
if (!emulatorConfig) {
return;
Expand All @@ -457,24 +457,21 @@ fireauth.RpcHandler.prototype.updateEmulatorConfig = function(emulatorConfig) {
fireauth.RpcHandler.IDENTITY_PLATFORM_ENDPOINT_, emulatorConfig);
}


/**
* Creates an endpoint URL intended for use by the emulator.
*
* According to go/firebase-auth-emulator-dd
* @param {string} endpoint the production endpoint URL.
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The emulator
* config.
* @return {string} The emulator endpoint URL.
* @private
*/
/**
* Creates an endpoint URL intended for use by the emulator.
* @param {string} endpoint the production endpoint URL.
* @param {?fireauth.constants.EmulatorSettings} emulatorConfig The emulator
* config.
* @return {string} The emulator endpoint URL.
* @private
*/
fireauth.RpcHandler.generateEmululatorEndpointUrl_ = function(endpoint, emulatorConfig) {
const uri = goog.Uri.parse(endpoint);
const endpointUri = goog.Uri.parse(emulatorConfig.url);
const emulatorUri = goog.Uri.parse(emulatorConfig.url);
uri.setPath(uri.getDomain() + uri.getPath());
uri.setScheme(endpointUri.getScheme());
uri.setDomain(endpointUri.getDomain());
uri.setPort(endpointUri.getPort());
uri.setScheme(emulatorUri.getScheme());
uri.setDomain(emulatorUri.getDomain());
uri.setPort(emulatorUri.getPort());
return uri.toString();
}

Expand Down
15 changes: 10 additions & 5 deletions packages/auth/src/storageusermanager.js
Expand Up @@ -67,6 +67,7 @@ goog.provide('fireauth.storage.UserManager');

goog.require('fireauth.AuthUser');
goog.require('fireauth.authStorage');
goog.require('fireauth.constants');
goog.require('goog.Promise');


Expand Down Expand Up @@ -399,13 +400,14 @@ fireauth.storage.UserManager.prototype.removeCurrentUser = function() {


/**
* @param {?string=} opt_authDomain The optional Auth domain to override if
* @param {?string=} authDomain The optional Auth domain to override if
* provided.
* @param {?fireauth.constants.EmulatorSettings=} emulatorConfig The current
* emulator config to use in user requests.
* @return {!goog.Promise<?fireauth.AuthUser>} A promise that resolves with
* the stored current user for the provided app ID.
*/
fireauth.storage.UserManager.prototype.getCurrentUser =
function(opt_authDomain) {
fireauth.storage.UserManager.prototype.getCurrentUser = function(authDomain, emulatorConfig) {
var self = this;
// Wait for any pending persistence change to be resolved.
return this.waitForReady_(function() {
Expand All @@ -420,8 +422,11 @@ fireauth.storage.UserManager.prototype.getCurrentUser =
// authDomain for the purpose of linking with a popup. The loaded user
// (stored without the authDomain) must have this field updated with
// the current authDomain.
if (response && opt_authDomain) {
response['authDomain'] = opt_authDomain;
if (response && authDomain) {
response['authDomain'] = authDomain;
}
if (response && emulatorConfig) {
response['emulatorConfig'] = emulatorConfig;
}
return fireauth.AuthUser.fromPlainObject(response || {});
});
Expand Down

0 comments on commit 2f756a5

Please sign in to comment.