@@ -10,6 +10,36 @@ import type { CryptoKey, JWK } from 'jose';
1010
1111import type { AddClientAuthentication , OAuthClientProvider } from './auth.js' ;
1212
13+ type SavedOAuthTokens = {
14+ tokens : OAuthTokens ;
15+ expiresAt ?: number ;
16+ } ;
17+
18+ function saveOAuthTokens ( tokens : OAuthTokens ) : SavedOAuthTokens {
19+ const savedTokens : SavedOAuthTokens = { tokens } ;
20+
21+ if ( tokens . expires_in !== undefined ) {
22+ savedTokens . expiresAt = Date . now ( ) + tokens . expires_in * 1000 ;
23+ }
24+
25+ return savedTokens ;
26+ }
27+
28+ function readOAuthTokens ( savedTokens : SavedOAuthTokens | undefined ) : OAuthTokens | undefined {
29+ if ( savedTokens === undefined ) {
30+ return undefined ;
31+ }
32+
33+ if ( savedTokens . expiresAt === undefined ) {
34+ return savedTokens . tokens ;
35+ }
36+
37+ return {
38+ ...savedTokens . tokens ,
39+ expires_in : Math . max ( 0 , Math . ceil ( ( savedTokens . expiresAt - Date . now ( ) ) / 1000 ) )
40+ } ;
41+ }
42+
1343/**
1444 * Helper to produce a `private_key_jwt` client authentication function.
1545 *
@@ -138,7 +168,7 @@ export interface ClientCredentialsProviderOptions {
138168 * ```
139169 */
140170export class ClientCredentialsProvider implements OAuthClientProvider {
141- private _tokens ?: OAuthTokens ;
171+ private _tokens ?: SavedOAuthTokens ;
142172 private _clientInfo : OAuthClientInformation ;
143173 private _clientMetadata : OAuthClientMetadata ;
144174
@@ -173,11 +203,11 @@ export class ClientCredentialsProvider implements OAuthClientProvider {
173203 }
174204
175205 tokens ( ) : OAuthTokens | undefined {
176- return this . _tokens ;
206+ return readOAuthTokens ( this . _tokens ) ;
177207 }
178208
179209 saveTokens ( tokens : OAuthTokens ) : void {
180- this . _tokens = tokens ;
210+ this . _tokens = saveOAuthTokens ( tokens ) ;
181211 }
182212
183213 redirectToAuthorization ( ) : void {
@@ -266,7 +296,7 @@ export interface PrivateKeyJwtProviderOptions {
266296 * ```
267297 */
268298export class PrivateKeyJwtProvider implements OAuthClientProvider {
269- private _tokens ?: OAuthTokens ;
299+ private _tokens ?: SavedOAuthTokens ;
270300 private _clientInfo : OAuthClientInformation ;
271301 private _clientMetadata : OAuthClientMetadata ;
272302 addClientAuthentication : AddClientAuthentication ;
@@ -309,11 +339,11 @@ export class PrivateKeyJwtProvider implements OAuthClientProvider {
309339 }
310340
311341 tokens ( ) : OAuthTokens | undefined {
312- return this . _tokens ;
342+ return readOAuthTokens ( this . _tokens ) ;
313343 }
314344
315345 saveTokens ( tokens : OAuthTokens ) : void {
316- this . _tokens = tokens ;
346+ this . _tokens = saveOAuthTokens ( tokens ) ;
317347 }
318348
319349 redirectToAuthorization ( ) : void {
@@ -371,7 +401,7 @@ export interface StaticPrivateKeyJwtProviderOptions {
371401 * uses it directly for authentication.
372402 */
373403export class StaticPrivateKeyJwtProvider implements OAuthClientProvider {
374- private _tokens ?: OAuthTokens ;
404+ private _tokens ?: SavedOAuthTokens ;
375405 private _clientInfo : OAuthClientInformation ;
376406 private _clientMetadata : OAuthClientMetadata ;
377407 addClientAuthentication : AddClientAuthentication ;
@@ -412,11 +442,11 @@ export class StaticPrivateKeyJwtProvider implements OAuthClientProvider {
412442 }
413443
414444 tokens ( ) : OAuthTokens | undefined {
415- return this . _tokens ;
445+ return readOAuthTokens ( this . _tokens ) ;
416446 }
417447
418448 saveTokens ( tokens : OAuthTokens ) : void {
419- this . _tokens = tokens ;
449+ this . _tokens = saveOAuthTokens ( tokens ) ;
420450 }
421451
422452 redirectToAuthorization ( ) : void {
@@ -569,7 +599,7 @@ export interface CrossAppAccessProviderOptions {
569599 * ```
570600 */
571601export class CrossAppAccessProvider implements OAuthClientProvider {
572- private _tokens ?: OAuthTokens ;
602+ private _tokens ?: SavedOAuthTokens ;
573603 private _clientInfo : OAuthClientInformation ;
574604 private _clientMetadata : OAuthClientMetadata ;
575605 private _assertionCallback : AssertionCallback ;
@@ -610,11 +640,11 @@ export class CrossAppAccessProvider implements OAuthClientProvider {
610640 }
611641
612642 tokens ( ) : OAuthTokens | undefined {
613- return this . _tokens ;
643+ return readOAuthTokens ( this . _tokens ) ;
614644 }
615645
616646 saveTokens ( tokens : OAuthTokens ) : void {
617- this . _tokens = tokens ;
647+ this . _tokens = saveOAuthTokens ( tokens ) ;
618648 }
619649
620650 redirectToAuthorization ( ) : void {
0 commit comments