-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pr/ianshade/329' into release51
- Loading branch information
Showing
8 changed files
with
290 additions
and
6 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
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
134 changes: 134 additions & 0 deletions
134
packages/timeline-state-resolver/src/integrations/httpSend/AuthenticatedHTTPSendDevice.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,134 @@ | ||
import { HTTPSendOptions } from 'timeline-state-resolver-types' | ||
import { HTTPSendDevice, HttpSendDeviceCommand } from '.' | ||
import { AccessToken, ClientCredentials } from 'simple-oauth2' | ||
|
||
const TOKEN_REQUEST_RETRY_TIMEOUT_MS = 1000 | ||
const TOKEN_EXPIRATION_WINDOW_SEC = 60 | ||
const DEFAULT_TOKEN_PATH = '/oauth/token' | ||
|
||
const enum AuthMethod { | ||
BEARER_TOKEN, | ||
CLIENT_CREDENTIALS, | ||
} | ||
type AuthOptions = | ||
| { | ||
method: AuthMethod.CLIENT_CREDENTIALS | ||
clientId: string | ||
clientSecret: string | ||
tokenHost: string | ||
tokenPath: string | ||
audience?: string | ||
} | ||
| { method: AuthMethod.BEARER_TOKEN; bearerToken: string } | ||
| undefined | ||
|
||
export class AuthenticatedHTTPSendDevice extends HTTPSendDevice { | ||
private tokenPromise: Promise<AccessToken> | undefined | ||
private tokenRequestPending = false | ||
private authOptions: AuthOptions | ||
private tokenRefreshTimeout: NodeJS.Timeout | undefined | ||
|
||
async init(options: HTTPSendOptions): Promise<boolean> { | ||
if (options.bearerToken) { | ||
this.authOptions = { | ||
method: AuthMethod.BEARER_TOKEN, | ||
bearerToken: options.bearerToken, | ||
} | ||
} else if (options.oauthClientId && options.oauthClientSecret && options.oauthTokenHost) { | ||
this.authOptions = { | ||
method: AuthMethod.CLIENT_CREDENTIALS, | ||
clientId: options.oauthClientId, | ||
clientSecret: options.oauthClientSecret, | ||
audience: options.oauthAudience, | ||
tokenHost: options.oauthTokenHost, | ||
tokenPath: options.oauthTokenPath ?? DEFAULT_TOKEN_PATH, | ||
} | ||
this.requestAccessToken() | ||
} | ||
return super.init(options) | ||
} | ||
|
||
async terminate() { | ||
this.clearTokenRefreshTimeout() | ||
return super.terminate() | ||
} | ||
|
||
private requestAccessToken(): void { | ||
if (this.tokenRequestPending) return | ||
this.clearTokenRefreshTimeout() | ||
this.tokenRequestPending = true | ||
const promise = this.makeAccessTokenRequest() | ||
promise | ||
.then((accessToken) => { | ||
this.context.logger.debug(`token received`) | ||
const expiresIn = accessToken.token.expires_in | ||
if (typeof expiresIn === 'number') { | ||
this.scheduleTokenRefresh(expiresIn) | ||
} | ||
}) | ||
.catch((e) => { | ||
this.context.logger.error('AuthenticatedHTTPSendDevice', e) | ||
setTimeout(() => this.requestAccessToken(), TOKEN_REQUEST_RETRY_TIMEOUT_MS) | ||
}) | ||
.finally(() => { | ||
this.tokenRequestPending = false | ||
}) | ||
this.tokenPromise = promise | ||
} | ||
|
||
private clearTokenRefreshTimeout() { | ||
if (this.tokenRefreshTimeout) { | ||
clearTimeout(this.tokenRefreshTimeout) | ||
} | ||
} | ||
|
||
private scheduleTokenRefresh(expiresInSec: number) { | ||
const timeoutMs = (expiresInSec - TOKEN_EXPIRATION_WINDOW_SEC) * 1000 | ||
this.context.logger.debug(`token refresh scheduled in ${timeoutMs}`) | ||
this.tokenRefreshTimeout = setTimeout(() => this.refreshAccessToken(), timeoutMs) | ||
} | ||
|
||
private refreshAccessToken(): void { | ||
this.context.logger.debug(`token refresh`) | ||
this.requestAccessToken() | ||
this.tokenRefreshTimeout = undefined | ||
} | ||
|
||
private async makeAccessTokenRequest(): Promise<AccessToken> { | ||
if (!this.authOptions || this.authOptions.method !== AuthMethod.CLIENT_CREDENTIALS) { | ||
throw Error('authOptions missing or incorrect') | ||
} | ||
this.context.logger.debug('debug', 'token request') | ||
const token = await new ClientCredentials({ | ||
client: { | ||
id: this.authOptions.clientId, | ||
secret: this.authOptions.clientSecret, | ||
}, | ||
auth: { | ||
tokenHost: this.authOptions.tokenHost, | ||
tokenPath: this.authOptions.tokenPath, | ||
}, | ||
}).getToken({ | ||
audience: this.authOptions.audience, | ||
}) | ||
return token | ||
} | ||
|
||
async sendCommand({ timelineObjId, context, command }: HttpSendDeviceCommand): Promise<void> { | ||
if (this.authOptions) { | ||
const bearerToken = | ||
this.authOptions.method === AuthMethod.BEARER_TOKEN ? this.authOptions.bearerToken : await this.tokenPromise | ||
if (bearerToken) { | ||
const bearerHeader = `Bearer ${typeof bearerToken === 'string' ? bearerToken : bearerToken.token.access_token}` | ||
command = { | ||
...command, | ||
content: { | ||
...command.content, | ||
headers: { ...command.content.headers, ['Authorization']: bearerHeader }, | ||
}, | ||
} | ||
} | ||
} | ||
return super.sendCommand({ timelineObjId, context, command }) | ||
} | ||
} |
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
Oops, something went wrong.