Skip to content

Commit 16fda4f

Browse files
committed
Update API to take URI instead of hostname/port
1 parent 33689e6 commit 16fda4f

File tree

9 files changed

+63
-62
lines changed

9 files changed

+63
-62
lines changed

packages/auth/src/auth.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,17 @@ fireauth.Auth.prototype.useDeviceLanguage = function() {
294294

295295
/**
296296
* Sets the emulator configuration (go/firebase-emulator-connection-api).
297-
* @param {string} hostname The hostname for the Auth emulator.
298-
* @param {number} port The port for the Auth emulator.
299-
*/
300-
fireauth.Auth.prototype.useEmulator = function(hostname, port) {
297+
/**
298+
* Sets the emulator configuration (go/firebase-emulator-connection-api).
299+
* @param {string} url The url for the Auth emulator.
300+
*/
301+
fireauth.Auth.prototype.useEmulator = function(url) {
301302
// Don't do anything if no change detected.
302-
if (!this.emulatorConfig_ ||
303-
hostname !== this.emulatorConfig_.hostname ||
304-
port !== this.emulatorConfig_.port) {
305-
this.emulatorConfig_ = { hostname: hostname, port: port };
303+
if (!this.emulatorConfig_ || url !== this.emulatorConfig_.url) {
304+
console.warn("WARNING: You are using the Auth Emulator, which is" +
305+
" intended for local testing only. Do not use with" +
306+
" production credentials.");
307+
this.emulatorConfig_ = { url: url };
306308
// Disable app verification.
307309
this.settings_().setAppVerificationDisabledForTesting(true);
308310
// Update custom Firebase locale field.
@@ -875,6 +877,7 @@ fireauth.Auth.prototype.updateCurrentUser = function(user) {
875877
options['apiKey'] = this.app_().options['apiKey'];
876878
options['authDomain'] = this.app_().options['authDomain'];
877879
options['appName'] = this.app_().name;
880+
options['emulatorConfig'] = this.emulatorConfig_;
878881
var newUser = fireauth.AuthUser.copyUser(user, options,
879882
self.redirectUserStorageManager_, self.getFramework());
880883
return this.registerPendingPromise_(

packages/auth/src/defines.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,10 @@ fireauth.constants.OIDC_PREFIX = 'oidc.';
172172
/**
173173
* The settings of an Auth emulator. The fields are:
174174
* <ul>
175-
* <li>hostname: defines the hostname where the emulator is running.</li>
176-
* <li>port: defines the port where the emulator is running.</li>
175+
* <li>url: defines the URL where the emulator is running.</li>
177176
* </ul>
178177
* @typedef {{
179-
* hostname: string,
180-
* port: number
178+
* url: string,
181179
* }}
182180
*/
183181
fireauth.constants.EmulatorSettings;

packages/auth/src/exports_auth.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ fireauth.exportlib.exportPrototypeMethods(
200200
useEmulator: {
201201
name: 'useEmulator',
202202
args: [
203-
fireauth.args.string('hostname'),
204-
fireauth.args.number('port')
203+
fireauth.args.string('url')
205204
]
206205
},
207206
verifyPasswordResetCode: {

packages/auth/src/iframeclient/ifchandler.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,31 @@ fireauth.iframeclient.IframeUrlBuilder = function(authDomain, apiKey, appName, e
8282
this.emulatorConfig_ = emulatorConfig;
8383
/** @private {?string|undefined} The client version. */
8484
this.v_ = null;
85-
/**
86-
* @private @const {!goog.Uri} The URI object used to build the iframe URL.
87-
*/
88-
this.uri_ = this.emulatorConfig_ ? goog.Uri.create(
89-
'http',
85+
let uri;
86+
if (this.emulatorConfig_) {
87+
const emulatorUri = goog.Uri.parse(this.emulatorConfig_.url);
88+
uri = goog.Uri.create(
89+
emulatorUri.getScheme(),
9090
null,
91-
this.emulatorConfig_.hostname,
92-
this.emulatorConfig_.port,
91+
emulatorUri.getDomain(),
92+
emulatorUri.getPort(),
9393
'/emulator/auth/iframe',
9494
null,
95-
null) :
96-
goog.Uri.create(
95+
null);
96+
} else {
97+
uri = goog.Uri.create(
9798
fireauth.iframeclient.SCHEME,
9899
null,
99100
this.authDomain_,
100101
fireauth.iframeclient.PORT_NUMBER,
101102
'/__/auth/iframe',
102103
null,
103104
null);
105+
}
106+
/**
107+
* @private @const {!goog.Uri} The URI object used to build the iframe URL.
108+
*/
109+
this.uri_ = uri;
104110
this.uri_.setParameterValue('apiKey', this.apiKey_);
105111
this.uri_.setParameterValue('appName', this.appName_);
106112
/** @private {?string|undefined} The endpoint ID. */
@@ -306,23 +312,28 @@ fireauth.iframeclient.OAuthUrlBuilder.prototype.setAdditionalParameters =
306312
* @return {string} The constructed OAuth URL string.
307313
* @override
308314
*/
309-
fireauth.iframeclient.OAuthUrlBuilder.prototype.toString = function() {
310-
var uri = this.emulatorConfig_ ? goog.Uri.create(
311-
'http',
315+
fireauth.iframeclient.OAuthUrlBuilder.prototype.toString = function () {
316+
var uri;
317+
if (this.emulatorConfig_) {
318+
const emulatorUri = goog.Uri.parse(this.emulatorConfig_.url);
319+
uri = goog.Uri.create(
320+
emulatorUri.getScheme(),
312321
null,
313-
this.emulatorConfig_.hostname,
314-
this.emulatorConfig_.port,
322+
emulatorUri.getDomain(),
323+
emulatorUri.getPort(),
315324
'/emulator/auth/handler',
316325
null,
317-
null) :
318-
goog.Uri.create(
326+
null);
327+
} else {
328+
uri = goog.Uri.create(
319329
fireauth.iframeclient.SCHEME,
320330
null,
321331
this.authDomain_,
322332
fireauth.iframeclient.PORT_NUMBER,
323333
'/__/auth/handler',
324334
null,
325335
null);
336+
}
326337
uri.setParameterValue('apiKey', this.apiKey_);
327338
uri.setParameterValue('appName', this.appName_);
328339
uri.setParameterValue('authType', this.authType_);

packages/auth/src/rpchandler.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,11 @@ fireauth.RpcHandler.prototype.updateEmulatorConfig = function(emulatorConfig) {
470470
*/
471471
fireauth.RpcHandler.generateEmululatorEndpointUrl_ = function(endpoint, emulatorConfig) {
472472
const uri = goog.Uri.parse(endpoint);
473-
uri.setScheme("http");
473+
const endpointUri = goog.Uri.parse(emulatorConfig.url);
474474
uri.setPath(uri.getDomain() + uri.getPath());
475-
uri.setDomain(emulatorConfig.hostname);
476-
uri.setPort(emulatorConfig.port);
475+
uri.setScheme(endpointUri.getScheme());
476+
uri.setDomain(endpointUri.getDomain());
477+
uri.setPort(endpointUri.getPort());
477478
return uri.toString();
478479
}
479480

packages/auth/test/auth_test.js

+11-18
Original file line numberDiff line numberDiff line change
@@ -921,45 +921,40 @@ function testUseEmulator() {
921921
0, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
922922

923923
// Update the emulator config.
924-
auth1.useEmulator('emulator.test.domain', 1234);
924+
auth1.useEmulator('http://emulator.test.domain:1234');
925925
assertObjectEquals(
926926
auth1.getEmulatorConfig(), {
927-
hostname: 'emulator.test.domain',
928-
port: 1234
927+
url: 'http://emulator.test.domain:1234',
929928
});
930929
// Should notify the RPC handler.
931930
assertEquals(
932931
1, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
933932
assertObjectEquals({
934-
hostname: 'emulator.test.domain',
935-
port: 1234
933+
url: 'http://emulator.test.domain:1234',
936934
},
937935
fireauth.RpcHandler.prototype.updateEmulatorConfig.getLastCall()
938936
.getArgument(0)
939937
);
940938

941939
// Update to the same config should not trigger event again.
942-
auth1.useEmulator('emulator.test.domain', 1234);
940+
auth1.useEmulator('http://emulator.test.domain:1234');
943941
assertObjectEquals(
944942
auth1.getEmulatorConfig(), {
945-
hostname: 'emulator.test.domain',
946-
port: 1234
943+
url: 'http://emulator.test.domain:1234',
947944
});
948945
assertEquals(
949946
1, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
950947

951948
// Updating to different config should trigger event.
952-
auth1.useEmulator('emulator.other.domain', 9876);
949+
auth1.useEmulator('http://emulator.other.domain:9876');
953950
assertObjectEquals(
954951
auth1.getEmulatorConfig(), {
955-
hostname: 'emulator.other.domain',
956-
port: 9876
952+
url: 'http://emulator.other.domain:9876'
957953
});
958954
assertEquals(
959955
2, fireauth.RpcHandler.prototype.updateEmulatorConfig.getCallCount());
960956
assertObjectEquals({
961-
hostname: 'emulator.other.domain',
962-
port: 9876
957+
url: 'http://emulator.other.domain:9876',
963958
},
964959
fireauth.RpcHandler.prototype.updateEmulatorConfig.getLastCall()
965960
.getArgument(0)
@@ -2246,21 +2241,19 @@ function testAuth_authEventManager_withEmulator() {
22462241
assertEquals('API_KEY', apiKey);
22472242
assertEquals(appId1, appName);
22482243
assertObjectEquals(emulatorConfig, {
2249-
hostname: 'emulator.host',
2250-
port: 1234
2244+
url: 'http://emulator.test.domain:1234'
22512245
});
22522246
return expectedManager;
22532247
});
22542248
asyncTestCase.waitForSignals(1);
22552249
app1 = firebase.initializeApp(config3, appId1);
22562250
auth1 = app1.auth();
2257-
auth1.useEmulator('emulator.host', 1234);
2251+
auth1.useEmulator('http://emulator.test.domain:1234');
22582252
// Test manager initialized and Auth subscribed.
22592253
auth1.onIdTokenChanged(function (user) {
22602254
var manager = fireauth.AuthEventManager.getManager(
22612255
config3['authDomain'], config3['apiKey'], app1.name, {
2262-
hostname: 'emulator.host',
2263-
port: 1234
2256+
url: 'http://emulator.test.domain:1234',
22642257
});
22652258
assertEquals(expectedManager, manager);
22662259
assertEquals(0, expectedManager.unsubscribe.getCallCount());

packages/auth/test/authuser_test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -13183,13 +13183,11 @@ function testUser_emulatorConfigChanges() {
1318313183
var dispatcher2 = createEventDispatcher();
1318413184
user = new fireauth.AuthUser(config1, tokenResponse, accountInfo);
1318513185
var emulatorConfig = {
13186-
hostname: 'emulator.test.domain',
13187-
port: 1234
13186+
url: 'http://emulator.test.domain:1234',
1318813187
};
1318913188

1319013189
var otherEmulatorConfig = {
13191-
hostname: 'other.emulator.host',
13192-
port: 9876
13190+
url: 'http://other.emulator.host:9876'
1319313191
};
1319413192

1319513193
// Set emulator config.

packages/auth/test/iframeclient/ifchandler_test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ function testOAuthUrlBuilder_notOAuthProviderInstance() {
486486
function testOAuthUrlBuilder_withEmulatorConfig() {
487487
var provider = new fireauth.GoogleAuthProvider();
488488
var emulatorConfig = {
489-
hostname: "emulator.host",
490-
port: 1234
489+
url: "http://emulator.host:1234"
491490
};
492491
var builder = new fireauth.iframeclient.OAuthUrlBuilder(
493492
'example.firebaseapp.com', 'API_KEY', 'APP_NAME', 'signInWithPopup',
@@ -1513,8 +1512,7 @@ function testGetAuthIframeUrl_withEmulator() {
15131512
var version = '3.0.0-rc.1';
15141513
var endpointId = 's';
15151514
var emulatorConfig = {
1516-
hostname: "emulator.host",
1517-
port: 1234
1515+
url: "http://emulator.host:1234"
15181516
};
15191517
assertEquals(
15201518
'http://emulator.host:1234/emulator/auth/iframe?apiKey=apiKey1&appNa' +

packages/auth/test/rpchandler_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ function testRequestStsToken_emulator() {
11931193
expectedStsTokenResponse);
11941194
// Set an emulator config.
11951195
rpcHandler.updateEmulatorConfig(
1196-
{ hostname: 'emulator.test.domain', port: 1234 });
1196+
{ url: 'http://emulator.test.domain:1234' });
11971197
// Send STS token request, default config will be used.
11981198
rpcHandler
11991199
.requestStsToken({ 'grant_type': 'authorization_code', 'code': 'idToken' })
@@ -1248,7 +1248,7 @@ function testRequestFirebaseEndpoint_emulator() {
12481248
expectedResponse);
12491249
// Set an emulator config.
12501250
rpcHandler.updateEmulatorConfig(
1251-
{ hostname: 'emulator.test.domain', port: 1234 });
1251+
{ url: 'http://emulator.test.domain:1234' });
12521252

12531253
rpcHandler
12541254
.requestFirebaseEndpoint(
@@ -1303,7 +1303,7 @@ function testRequestIdentityPlatformEndpoint_emulator() {
13031303
expectedResponse);
13041304
// Set an emulator config.
13051305
rpcHandler.updateEmulatorConfig(
1306-
{ hostname: 'emulator.test.domain', port: 1234 });
1306+
{ url: 'http://emulator.test.domain:1234' });
13071307
rpcHandler
13081308
.requestIdentityPlatformEndpoint(
13091309
'method1', 'POST', { 'key1': 'value1', 'key2': 'value2' })

0 commit comments

Comments
 (0)