Skip to content

Commit 28c985d

Browse files
committed
Write Firebase Token to Auth instance
1 parent ffec9ba commit 28c985d

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

packages/auth/src/api/authentication/exchange_token.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export interface ExchangeTokenRequest {
2727
}
2828

2929
export interface ExchangeTokenResponse {
30+
// The firebase access token (JWT signed by Firebase Auth).
3031
accessToken: string;
31-
expiresIn?: string;
32+
// The time when the access token expires.
33+
expiresIn: number;
3234
}
3335

3436
export async function exchangeToken(

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import {
3737
NextFn,
3838
Unsubscribe,
3939
PasswordValidationStatus,
40-
TenantConfig
40+
TenantConfig,
41+
FirebaseToken
4142
} from '../../model/public_types';
4243
import {
4344
createSubscribe,
@@ -100,6 +101,7 @@ export const enum DefaultConfig {
100101
export class AuthImpl implements AuthInternal, _FirebaseService {
101102
currentUser: User | null = null;
102103
emulatorConfig: EmulatorConfig | null = null;
104+
firebaseToken: FirebaseToken | null = null;
103105
private operations = Promise.resolve();
104106
private persistenceManager?: PersistenceUserManager;
105107
private redirectPersistenceManager?: PersistenceUserManager;
@@ -455,6 +457,12 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
455457
});
456458
}
457459

460+
async _updateFirebaseToken(firebaseToken: FirebaseToken | null): Promise<void> {
461+
if (firebaseToken) {
462+
this.firebaseToken = firebaseToken;
463+
}
464+
}
465+
458466
async signOut(): Promise<void> {
459467
if (_isFirebaseServerApp(this.app)) {
460468
return Promise.reject(

packages/auth/src/core/strategies/exchange_token.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
testAuth,
2525
TestAuth
2626
} from '../../../test/helpers/mock_auth';
27+
import * as sinon from 'sinon';
2728
import * as mockFetch from '../../../test/helpers/mock_fetch';
2829
import { HttpHeader, RegionalEndpoint } from '../../api';
2930
import { exchangeToken } from './exhange_token';
@@ -35,19 +36,23 @@ use(chaiAsPromised);
3536
describe('core/strategies/exchangeToken', () => {
3637
let auth: TestAuth;
3738
let regionalAuth: TestAuth;
39+
let now: number;
3840

3941
beforeEach(async () => {
4042
auth = await testAuth();
4143
regionalAuth = await regionalTestAuth();
4244
mockFetch.setUp();
45+
now = Date.now();
46+
sinon.stub(Date, 'now').returns(now);
4347
});
4448
afterEach(mockFetch.tearDown);
49+
afterEach(() => sinon.restore());
4550

4651
it('should return a valid access token for Regional Auth', async () => {
4752
const mock = mockRegionalEndpointWithParent(
4853
RegionalEndpoint.EXCHANGE_TOKEN,
4954
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
50-
{ accessToken: 'outbound-token', expiresIn: '1000' }
55+
{ accessToken: 'outbound-token', expiresIn: 10000 }
5156
);
5257

5358
const accessToken = await exchangeToken(
@@ -65,6 +70,8 @@ describe('core/strategies/exchangeToken', () => {
6570
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
6671
'application/json'
6772
);
73+
expect(regionalAuth.firebaseToken?.token).to.equal('outbound-token');
74+
expect(regionalAuth.firebaseToken?.expirationTime).to.equal(now + 10_000);
6875
});
6976

7077
it('throws exception for default Auth', async () => {
@@ -106,5 +113,6 @@ describe('core/strategies/exchangeToken', () => {
106113
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
107114
'application/json'
108115
);
116+
expect(regionalAuth.firebaseToken).is.null;
109117
});
110118
});

packages/auth/src/core/strategies/exhange_token.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ export async function exchangeToken(
5454
parent: buildParent(auth, idpConfigId),
5555
token: customToken
5656
});
57-
// TODO(sammansi): Write token to the Auth object passed.
57+
if (token) {
58+
await authInternal._updateFirebaseToken({
59+
token: token.accessToken,
60+
expirationTime: Date.now() + token.expiresIn * 1000
61+
});
62+
}
5863
return token.accessToken;
5964
}
6065

packages/auth/src/model/auth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
AuthSettings,
2121
Config,
2222
EmulatorConfig,
23+
FirebaseToken,
2324
PasswordPolicy,
2425
PasswordValidationStatus,
2526
PopupRedirectResolver,
@@ -75,6 +76,7 @@ export interface AuthInternal extends Auth {
7576
_initializationPromise: Promise<void> | null;
7677
_persistenceManagerAvailable: Promise<void>;
7778
_updateCurrentUser(user: UserInternal | null): Promise<void>;
79+
_updateFirebaseToken(firebaseToken: FirebaseToken | null): Promise<void>;
7880

7981
_onStorageEvent(): void;
8082

packages/auth/src/model/public_types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ export interface Auth {
334334
* {@link @firebase/app#FirebaseServerApp}.
335335
*/
336336
signOut(): Promise<void>;
337+
/**
338+
* The token response initialized via {@link exchangeToken} endpoint.
339+
*
340+
* @remarks
341+
* This field is only supported for {@link Auth} instance that have defined
342+
* {@link TenantConfig}.
343+
*/
344+
readonly firebaseToken: FirebaseToken | null;
337345
}
338346

339347
/**
@@ -966,6 +974,13 @@ export interface ReactNativeAsyncStorage {
966974
removeItem(key: string): Promise<void>;
967975
}
968976

977+
export interface FirebaseToken {
978+
// The firebase access token (JWT signed by Firebase Auth).
979+
readonly token: string;
980+
// The time when the access token expires.
981+
readonly expirationTime: number;
982+
}
983+
969984
/**
970985
* A user account.
971986
*

0 commit comments

Comments
 (0)