Skip to content

Commit 28ccf1e

Browse files
committed
Write BYO-CIAM token, expiresIn to the Auth Object initialized
1 parent b609765 commit 28ccf1e

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface ExchangeTokenRequest {
2828

2929
export interface ExchangeTokenRespose {
3030
accessToken: string;
31-
expiresIn?: string;
31+
expiresIn: string;
3232
}
3333

3434
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+
TokenResponse
4142
} from '../../model/public_types';
4243
import {
4344
createSubscribe,
@@ -99,6 +100,7 @@ export const enum DefaultConfig {
99100
export class AuthImpl implements AuthInternal, _FirebaseService {
100101
currentUser: User | null = null;
101102
emulatorConfig: EmulatorConfig | null = null;
103+
tokenResponse: TokenResponse | null = null;
102104
private operations = Promise.resolve();
103105
private persistenceManager?: PersistenceUserManager;
104106
private redirectPersistenceManager?: PersistenceUserManager;
@@ -454,6 +456,12 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
454456
});
455457
}
456458

459+
async _updateTokenResponse(tokenResponse: TokenResponse): Promise<void> {
460+
if (tokenResponse) {
461+
this.tokenResponse = tokenResponse;
462+
}
463+
}
464+
457465
async signOut(): Promise<void> {
458466
if (_isFirebaseServerApp(this.app)) {
459467
return Promise.reject(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ describe('core/strategies/exchangeToken', () => {
6161
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
6262
token: 'custom-token'
6363
});
64+
expect(regionalAuth.tokenResponse?.token).to.equal('outbound-token');
65+
expect(regionalAuth.tokenResponse?.expiresIn).to.equal('1000');
6466
expect(mock.calls[0].method).to.eq('POST');
6567
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
6668
'application/json'

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._updateTokenResponse({
59+
token: token.accessToken,
60+
expiresIn: token.expiresIn
61+
});
62+
}
5863
return token.accessToken;
5964
}
6065

packages/auth/src/model/auth.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
PasswordValidationStatus,
2525
PopupRedirectResolver,
2626
TenantConfig,
27+
TokenResponse,
2728
User
2829
} from './public_types';
2930
import { ErrorFactory } from '@firebase/util';
@@ -66,6 +67,7 @@ export interface ConfigInternal extends Config {
6667
export interface AuthInternal extends Auth {
6768
currentUser: User | null;
6869
emulatorConfig: EmulatorConfig | null;
70+
tokenResponse: TokenResponse | null;
6971
_agentRecaptchaConfig: RecaptchaConfig | null;
7072
_tenantRecaptchaConfigs: Record<string, RecaptchaConfig>;
7173
_projectPasswordPolicy: PasswordPolicy | null;
@@ -75,6 +77,7 @@ export interface AuthInternal extends Auth {
7577
_initializationPromise: Promise<void> | null;
7678
_persistenceManagerAvailable: Promise<void>;
7779
_updateCurrentUser(user: UserInternal | null): Promise<void>;
80+
_updateTokenResponse(tokenResponse: TokenResponse | null): Promise<void>;
7881

7982
_onStorageEvent(): void;
8083

packages/auth/src/model/public_types.ts

Lines changed: 20 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 tokenResponse: TokenResponse | null;
337345
}
338346

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

977+
/**
978+
* Interface for TokenRespone returned via {@link exchangeToken} endpoint.
979+
* This is expected to be returned only if {@link Auth} object initialized
980+
* has defined {@link TenantConfig}.
981+
*/
982+
export interface TokenResponse {
983+
// The firebase access token (JWT signed by Firebase Auth).
984+
readonly token: string;
985+
// The time in seconds when the access token expires.
986+
readonly expiresIn: string;
987+
}
988+
969989
/**
970990
* A user account.
971991
*

0 commit comments

Comments
 (0)