Skip to content

Commit 8a23ed1

Browse files
committed
Extract IAT from custom token
1 parent 0b4d4fa commit 8a23ed1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

packages/app-check/src/internal-api.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
} from './client';
3838
import { writeTokenToStorage, readTokenFromStorage } from './storage';
3939
import { getDebugToken, isDebugMode } from './debug';
40-
import { base64 } from '@firebase/util';
40+
import { base64, issuedAtTime } from '@firebase/util';
4141
import { ERROR_FACTORY, AppCheckError } from './errors';
4242
import { logger } from './logger';
4343

@@ -113,7 +113,19 @@ export async function getToken(
113113
try {
114114
if (state.customProvider) {
115115
const customToken = await state.customProvider.getToken();
116-
token = { ...customToken, issuedAtTimeMillis: Date.now() };
116+
// Try to extract IAT from custom token, in case this token is not
117+
// being newly issued. JWT timestamps are in seconds since epoch.
118+
const issuedAtTimeSeconds = issuedAtTime(customToken.token);
119+
// Very basic validation, use current timestamp as IAT if JWT
120+
// has no `iat` field or value is out of bounds.
121+
const issuedAtTimeMillis =
122+
issuedAtTimeSeconds !== null &&
123+
issuedAtTimeSeconds < Date.now() &&
124+
issuedAtTimeSeconds > 0
125+
? issuedAtTimeSeconds * 1000
126+
: Date.now();
127+
128+
token = { ...customToken, issuedAtTimeMillis };
117129
} else {
118130
const attestedClaimsToken = await getReCAPTCHAToken(app).catch(_e => {
119131
// reCaptcha.execute() throws null which is not very descriptive.

0 commit comments

Comments
 (0)