Skip to content

Commit c33aa21

Browse files
committed
PR comments
1 parent 8072416 commit c33aa21

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

packages-exp/auth-exp/src/api/authentication/token.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import { _ENDPOINT, requestStsToken } from './token';
2828
use(chaiAsPromised);
2929

3030
describe('requestStsToken', () => {
31-
const endpoint = `${_ENDPOINT}?key=${mockAuth.config.apiKey}`;
31+
const {apiKey, tokenApiHost, apiScheme} = mockAuth.config;
32+
const endpoint = `${apiScheme}://${tokenApiHost}/${_ENDPOINT}?key=${apiKey}`;
3233
beforeEach(fetch.setUp);
3334
afterEach(fetch.tearDown);
3435

packages-exp/auth-exp/src/api/authentication/token.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
* limitations under the License.
1616
*/
1717

18+
/* eslint-disable camelcase */
19+
1820
import { querystring } from '@firebase/util';
1921

20-
import { performFetchWithErrorHandling } from '../';
22+
import { HttpMethod, performFetchWithErrorHandling } from '../';
2123
import { Auth } from '../../model/auth';
2224

23-
export const _ENDPOINT = 'https://securetoken.googleapis.com/v1/token';
25+
export const _ENDPOINT = 'v1/token';
2426
const GRANT_TYPE = 'refresh_token';
2527

26-
enum ServerField {
27-
ACCESS_TOKEN = 'access_token',
28-
EXPIRES_IN = 'expires_in',
29-
REFRESH_TOKEN = 'refresh_token'
28+
/** The server responses with snake_case; we convert to camelCase */
29+
interface RequestStsTokenServerResponse {
30+
access_token?: string;
31+
expires_in?: string;
32+
refresh_token?: string;
3033
}
3134

3235
export interface RequestStsTokenResponse {
@@ -39,18 +42,18 @@ export async function requestStsToken(
3942
auth: Auth,
4043
refreshToken: string
4144
): Promise<RequestStsTokenResponse> {
42-
const response = await performFetchWithErrorHandling<{
43-
[key: string]: string;
44-
}>(auth, {}, () => {
45+
const response = await performFetchWithErrorHandling<RequestStsTokenServerResponse>(auth, {}, () => {
4546
const body = querystring({
4647
'grant_type': GRANT_TYPE,
4748
'refresh_token': refreshToken
4849
}).slice(1);
50+
const {apiScheme, tokenApiHost, apiKey, sdkClientVersion} = auth.config;
51+
const url = `${apiScheme}://${tokenApiHost}/${_ENDPOINT}`;
4952

50-
return fetch(`${_ENDPOINT}?key=${auth.config.apiKey}`, {
51-
method: 'POST',
53+
return fetch(`${url}?key=${apiKey}`, {
54+
method: HttpMethod.POST,
5255
headers: {
53-
'X-Client-Version': auth.config.sdkClientVersion,
56+
'X-Client-Version': sdkClientVersion,
5457
'Content-Type': 'application/x-www-form-urlencoded'
5558
},
5659
body
@@ -59,8 +62,8 @@ export async function requestStsToken(
5962

6063
// The response comes back in snake_case. Convert to camel:
6164
return {
62-
accessToken: response[ServerField.ACCESS_TOKEN],
63-
expiresIn: response[ServerField.EXPIRES_IN],
64-
refreshToken: response[ServerField.REFRESH_TOKEN]
65+
accessToken: response.access_token,
66+
expiresIn: response.expires_in,
67+
refreshToken: response.refresh_token
6568
};
6669
}

packages-exp/auth-exp/src/core/auth/auth_impl.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import { inMemoryPersistence } from '../persistence/in_memory';
3030
import { PersistenceUserManager } from '../persistence/persistence_user_manager';
3131
import { ClientPlatform, getClientVersion } from '../util/version';
3232
import {
33-
DEFAULT_API_HOST,
34-
DEFAULT_API_SCHEME,
35-
initializeAuth
33+
DEFAULT_API_HOST, DEFAULT_API_SCHEME, DEFAULT_TOKEN_API_HOST, initializeAuth
3634
} from './auth_impl';
3735

3836
use(sinonChai);
@@ -184,6 +182,7 @@ describe('initializeAuth', () => {
184182
authDomain: FAKE_APP.options.authDomain,
185183
apiHost: DEFAULT_API_HOST,
186184
apiScheme: DEFAULT_API_SCHEME,
185+
tokenApiHost: DEFAULT_TOKEN_API_HOST,
187186
sdkClientVersion: getClientVersion(ClientPlatform.BROWSER)
188187
});
189188
});

packages-exp/auth-exp/src/core/auth/auth_impl.ts

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ interface AsyncAction {
3030
(): Promise<void>;
3131
}
3232

33+
export const DEFAULT_TOKEN_API_HOST = 'securetoken.googleapis.com';
3334
export const DEFAULT_API_HOST = 'identitytoolkit.googleapis.com';
3435
export const DEFAULT_API_SCHEME = 'https';
3536

@@ -114,6 +115,7 @@ export function initializeAuth(
114115
apiKey: assert(apiKey, app.name, AuthErrorCode.INVALID_API_KEY),
115116
authDomain,
116117
apiHost: DEFAULT_API_HOST,
118+
tokenApiHost: DEFAULT_TOKEN_API_HOST,
117119
apiScheme: DEFAULT_API_SCHEME,
118120
sdkClientVersion: getClientVersion(ClientPlatform.BROWSER)
119121
};

packages-exp/auth-exp/src/core/user/token_manager.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ describe('core/user/token_manager', () => {
7878
context('with endpoint setup', () => {
7979
let mock: fetch.Route;
8080
beforeEach(() => {
81-
const endpoint = `${_ENDPOINT}?key=${mockAuth.config.apiKey}`;
81+
const {apiKey, tokenApiHost, apiScheme} = mockAuth.config;
82+
const endpoint =
83+
`${apiScheme}://${tokenApiHost}/${_ENDPOINT}?key=${apiKey}`;
8284
mock = fetch.mock(endpoint, {
8385
'access_token': 'new-access-token',
8486
'refresh_token': 'new-refresh-token',

packages-exp/auth-exp/src/core/user/token_manager.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ export class StsTokenManager {
9494
auth,
9595
oldToken
9696
);
97-
this.updateTokensAndExpiration(accessToken, refreshToken, expiresIn);
97+
this.updateTokensAndExpiration(accessToken || null, refreshToken || null, expiresIn || null);
9898
}
9999

100100
private updateTokensAndExpiration(
101-
accessToken: string | undefined,
102-
refreshToken: string | undefined,
103-
expiresInSec: string | undefined
101+
accessToken: string | null,
102+
refreshToken: string | null,
103+
expiresInSec: string | null
104104
): void {
105-
this.refreshToken = refreshToken || null;
106-
this.accessToken = accessToken || null;
105+
this.refreshToken = refreshToken;
106+
this.accessToken = accessToken;
107107
this.expirationTime = expiresInSec
108108
? Date.now() + Number(expiresInSec) * 1000
109109
: null;

packages-exp/auth-exp/src/model/auth.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface Config {
2626
apiKey: ApiKey;
2727
apiHost: string;
2828
apiScheme: string;
29+
tokenApiHost: string;
2930
sdkClientVersion: string;
3031
authDomain?: AuthDomain;
3132
}

packages-exp/auth-exp/test/mock_auth.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Auth } from '../src/model/auth';
2121
import { User } from '../src/model/user';
2222

2323
export const TEST_HOST = 'localhost';
24+
export const TEST_TOKEN_HOST = 'localhost/token';
2425
export const TEST_SCHEME = 'mock';
2526
export const TEST_KEY = 'test-api-key';
2627

@@ -30,6 +31,7 @@ export const mockAuth: Auth = {
3031
apiKey: TEST_KEY,
3132
apiHost: TEST_HOST,
3233
apiScheme: TEST_SCHEME,
34+
tokenApiHost: TEST_TOKEN_HOST,
3335
sdkClientVersion: 'testSDK/0.0.0'
3436
},
3537
currentUser: null,

0 commit comments

Comments
 (0)