Skip to content

Commit

Permalink
Update API to take URI instead of hostname/port
Browse files Browse the repository at this point in the history
  • Loading branch information
avolkovi committed Sep 24, 2020
1 parent 33689e6 commit 16fda4f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 62 deletions.
19 changes: 11 additions & 8 deletions packages/auth/src/auth.js
Expand Up @@ -294,15 +294,17 @@ fireauth.Auth.prototype.useDeviceLanguage = function() {

/**
* Sets the emulator configuration (go/firebase-emulator-connection-api).
* @param {string} hostname The hostname for the Auth emulator.
* @param {number} port The port for the Auth emulator.
*/
fireauth.Auth.prototype.useEmulator = function(hostname, port) {
/**
* Sets the emulator configuration (go/firebase-emulator-connection-api).
* @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_ ||
hostname !== this.emulatorConfig_.hostname ||
port !== this.emulatorConfig_.port) {
this.emulatorConfig_ = { hostname: hostname, port: port };
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 };
// Disable app verification.
this.settings_().setAppVerificationDisabledForTesting(true);
// Update custom Firebase locale field.
Expand Down Expand Up @@ -875,6 +877,7 @@ 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
6 changes: 2 additions & 4 deletions packages/auth/src/defines.js
Expand Up @@ -172,12 +172,10 @@ fireauth.constants.OIDC_PREFIX = 'oidc.';
/**
* The settings of an Auth emulator. The fields are:
* <ul>
* <li>hostname: defines the hostname where the emulator is running.</li>
* <li>port: defines the port where the emulator is running.</li>
* <li>url: defines the URL where the emulator is running.</li>
* </ul>
* @typedef {{
* hostname: string,
* port: number
* url: string,
* }}
*/
fireauth.constants.EmulatorSettings;
3 changes: 1 addition & 2 deletions packages/auth/src/exports_auth.js
Expand Up @@ -200,8 +200,7 @@ fireauth.exportlib.exportPrototypeMethods(
useEmulator: {
name: 'useEmulator',
args: [
fireauth.args.string('hostname'),
fireauth.args.number('port')
fireauth.args.string('url')
]
},
verifyPasswordResetCode: {
Expand Down
43 changes: 27 additions & 16 deletions packages/auth/src/iframeclient/ifchandler.js
Expand Up @@ -82,25 +82,31 @@ fireauth.iframeclient.IframeUrlBuilder = function(authDomain, apiKey, appName, e
this.emulatorConfig_ = emulatorConfig;
/** @private {?string|undefined} The client version. */
this.v_ = null;
/**
* @private @const {!goog.Uri} The URI object used to build the iframe URL.
*/
this.uri_ = this.emulatorConfig_ ? goog.Uri.create(
'http',
let uri;
if (this.emulatorConfig_) {
const emulatorUri = goog.Uri.parse(this.emulatorConfig_.url);
uri = goog.Uri.create(
emulatorUri.getScheme(),
null,
this.emulatorConfig_.hostname,
this.emulatorConfig_.port,
emulatorUri.getDomain(),
emulatorUri.getPort(),
'/emulator/auth/iframe',
null,
null) :
goog.Uri.create(
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. */
Expand Down Expand Up @@ -306,23 +312,28 @@ fireauth.iframeclient.OAuthUrlBuilder.prototype.setAdditionalParameters =
* @return {string} The constructed OAuth URL string.
* @override
*/
fireauth.iframeclient.OAuthUrlBuilder.prototype.toString = function() {
var uri = this.emulatorConfig_ ? goog.Uri.create(
'http',
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,
this.emulatorConfig_.hostname,
this.emulatorConfig_.port,
emulatorUri.getDomain(),
emulatorUri.getPort(),
'/emulator/auth/handler',
null,
null) :
goog.Uri.create(
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_);
Expand Down
7 changes: 4 additions & 3 deletions packages/auth/src/rpchandler.js
Expand Up @@ -470,10 +470,11 @@ fireauth.RpcHandler.prototype.updateEmulatorConfig = function(emulatorConfig) {
*/
fireauth.RpcHandler.generateEmululatorEndpointUrl_ = function(endpoint, emulatorConfig) {
const uri = goog.Uri.parse(endpoint);
uri.setScheme("http");
const endpointUri = goog.Uri.parse(emulatorConfig.url);
uri.setPath(uri.getDomain() + uri.getPath());
uri.setDomain(emulatorConfig.hostname);
uri.setPort(emulatorConfig.port);
uri.setScheme(endpointUri.getScheme());
uri.setDomain(endpointUri.getDomain());
uri.setPort(endpointUri.getPort());
return uri.toString();
}

Expand Down
29 changes: 11 additions & 18 deletions packages/auth/test/auth_test.js
Expand Up @@ -921,45 +921,40 @@ function testUseEmulator() {
0, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());

// Update the emulator config.
auth1.useEmulator('emulator.test.domain', 1234);
auth1.useEmulator('http://emulator.test.domain:1234');
assertObjectEquals(
auth1.getEmulatorConfig(), {
hostname: 'emulator.test.domain',
port: 1234
url: 'http://emulator.test.domain:1234',
});
// Should notify the RPC handler.
assertEquals(
1, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
assertObjectEquals({
hostname: 'emulator.test.domain',
port: 1234
url: 'http://emulator.test.domain:1234',
},
fireauth.RpcHandler.prototype.updateEmulatorConfig.getLastCall()
.getArgument(0)
);

// Update to the same config should not trigger event again.
auth1.useEmulator('emulator.test.domain', 1234);
auth1.useEmulator('http://emulator.test.domain:1234');
assertObjectEquals(
auth1.getEmulatorConfig(), {
hostname: 'emulator.test.domain',
port: 1234
url: 'http://emulator.test.domain:1234',
});
assertEquals(
1, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());

// Updating to different config should trigger event.
auth1.useEmulator('emulator.other.domain', 9876);
auth1.useEmulator('http://emulator.other.domain:9876');
assertObjectEquals(
auth1.getEmulatorConfig(), {
hostname: 'emulator.other.domain',
port: 9876
url: 'http://emulator.other.domain:9876'
});
assertEquals(
2, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
assertObjectEquals({
hostname: 'emulator.other.domain',
port: 9876
url: 'http://emulator.other.domain:9876',
},
fireauth.RpcHandler.prototype.updateEmulatorConfig.getLastCall()
.getArgument(0)
Expand Down Expand Up @@ -2246,21 +2241,19 @@ function testAuth_authEventManager_withEmulator() {
assertEquals('API_KEY', apiKey);
assertEquals(appId1, appName);
assertObjectEquals(emulatorConfig, {
hostname: 'emulator.host',
port: 1234
url: 'http://emulator.test.domain:1234'
});
return expectedManager;
});
asyncTestCase.waitForSignals(1);
app1 = firebase.initializeApp(config3, appId1);
auth1 = app1.auth();
auth1.useEmulator('emulator.host', 1234);
auth1.useEmulator('http://emulator.test.domain:1234');
// Test manager initialized and Auth subscribed.
auth1.onIdTokenChanged(function (user) {
var manager = fireauth.AuthEventManager.getManager(
config3['authDomain'], config3['apiKey'], app1.name, {
hostname: 'emulator.host',
port: 1234
url: 'http://emulator.test.domain:1234',
});
assertEquals(expectedManager, manager);
assertEquals(0, expectedManager.unsubscribe.getCallCount());
Expand Down
6 changes: 2 additions & 4 deletions packages/auth/test/authuser_test.js
Expand Up @@ -13183,13 +13183,11 @@ function testUser_emulatorConfigChanges() {
var dispatcher2 = createEventDispatcher();
user = new fireauth.AuthUser(config1, tokenResponse, accountInfo);
var emulatorConfig = {
hostname: 'emulator.test.domain',
port: 1234
url: 'http://emulator.test.domain:1234',
};

var otherEmulatorConfig = {
hostname: 'other.emulator.host',
port: 9876
url: 'http://other.emulator.host:9876'
};

// Set emulator config.
Expand Down
6 changes: 2 additions & 4 deletions packages/auth/test/iframeclient/ifchandler_test.js
Expand Up @@ -486,8 +486,7 @@ function testOAuthUrlBuilder_notOAuthProviderInstance() {
function testOAuthUrlBuilder_withEmulatorConfig() {
var provider = new fireauth.GoogleAuthProvider();
var emulatorConfig = {
hostname: "emulator.host",
port: 1234
url: "http://emulator.host:1234"
};
var builder = new fireauth.iframeclient.OAuthUrlBuilder(
'example.firebaseapp.com', 'API_KEY', 'APP_NAME', 'signInWithPopup',
Expand Down Expand Up @@ -1513,8 +1512,7 @@ function testGetAuthIframeUrl_withEmulator() {
var version = '3.0.0-rc.1';
var endpointId = 's';
var emulatorConfig = {
hostname: "emulator.host",
port: 1234
url: "http://emulator.host:1234"
};
assertEquals(
'http://emulator.host:1234/emulator/auth/iframe?apiKey=apiKey1&appNa' +
Expand Down
6 changes: 3 additions & 3 deletions packages/auth/test/rpchandler_test.js
Expand Up @@ -1193,7 +1193,7 @@ function testRequestStsToken_emulator() {
expectedStsTokenResponse);
// Set an emulator config.
rpcHandler.updateEmulatorConfig(
{ hostname: 'emulator.test.domain', port: 1234 });
{ url: 'http://emulator.test.domain:1234' });
// Send STS token request, default config will be used.
rpcHandler
.requestStsToken({ 'grant_type': 'authorization_code', 'code': 'idToken' })
Expand Down Expand Up @@ -1248,7 +1248,7 @@ function testRequestFirebaseEndpoint_emulator() {
expectedResponse);
// Set an emulator config.
rpcHandler.updateEmulatorConfig(
{ hostname: 'emulator.test.domain', port: 1234 });
{ url: 'http://emulator.test.domain:1234' });

rpcHandler
.requestFirebaseEndpoint(
Expand Down Expand Up @@ -1303,7 +1303,7 @@ function testRequestIdentityPlatformEndpoint_emulator() {
expectedResponse);
// Set an emulator config.
rpcHandler.updateEmulatorConfig(
{ hostname: 'emulator.test.domain', port: 1234 });
{ url: 'http://emulator.test.domain:1234' });
rpcHandler
.requestIdentityPlatformEndpoint(
'method1', 'POST', { 'key1': 'value1', 'key2': 'value2' })
Expand Down

0 comments on commit 16fda4f

Please sign in to comment.