-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
84 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 6 additions & 24 deletions
30
extensions/wrapper/clients/typescript-client/src/oauth2/AccessTokenInjector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 10 additions & 23 deletions
33
extensions/wrapper/clients/typescript-client/src/oauth2/AccessTokenStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,25 @@ | ||
import {AccessTokenFetcher} from './AccessTokenFetcher'; | ||
|
||
export class AccessTokenStore { | ||
_accessToken?: string; | ||
accessTokenFetcher: AccessTokenFetcher; | ||
private isRefreshing = false; | ||
private currentFetchPromise: Promise<string | undefined> = | ||
Promise.resolve(undefined); | ||
private activeRequest: Promise<string> | null = null; | ||
|
||
constructor(accessTokenFetcher: AccessTokenFetcher, token?: string) { | ||
this.accessToken = token; | ||
this.accessTokenFetcher = accessTokenFetcher; | ||
} | ||
|
||
get accessToken(): string | undefined { | ||
return this._accessToken; | ||
} | ||
|
||
set accessToken(token: string | undefined) { | ||
this._accessToken = token; | ||
} | ||
constructor( | ||
private accessTokenFetcher: AccessTokenFetcher, | ||
public accessToken?: string, | ||
) {} | ||
|
||
// Synchronized refreshing of the access token | ||
async refreshToken() { | ||
if (this.isRefreshing) { | ||
await this.currentFetchPromise; | ||
if (this.activeRequest) { | ||
await this.activeRequest; | ||
return; | ||
} | ||
|
||
this.isRefreshing = true; | ||
this.currentFetchPromise = this.accessTokenFetcher.fetch(); | ||
const newToken = await this.currentFetchPromise; | ||
this.activeRequest = this.accessTokenFetcher.fetch(); | ||
const newToken = await this.activeRequest; | ||
if (newToken) { | ||
this.accessToken = newToken; | ||
} | ||
this.isRefreshing = false; | ||
this.activeRequest = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
extensions/wrapper/clients/typescript-client/src/oauth2/RequestUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {isHeaders} from './ObjectUtils'; | ||
|
||
export function withHeader( | ||
headerName: string, | ||
headerValue: string, | ||
headers?: HeadersInit, | ||
): HeadersInit { | ||
if (!headers) { | ||
headers = {headerName: headerValue}; | ||
return headers; | ||
} | ||
|
||
if (Array.isArray(headers)) { | ||
return headers.map(([a, b]) => | ||
a !== headerName ? [a, b] : [headerName, headerValue], | ||
); | ||
} | ||
|
||
if (isHeaders(headers)) { | ||
if (headers.has(headerName)) { | ||
headers.set(headerName, headerValue); | ||
} else { | ||
headers.append(headerName, headerValue); | ||
} | ||
return headers; | ||
} | ||
|
||
return { | ||
...(headers as Record<string, string>), | ||
headerName: headerValue, | ||
}; | ||
} |