-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
70 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { OAuth2Client, OAuth2Token } from "@badgateway/oauth2-client"; | ||
import WsJsonClient from "./wsJsonClient"; | ||
import debug from "debug"; | ||
|
||
const logger = debug("wsJsonClientAuth"); | ||
|
||
export default class WsJsonClientAuth { | ||
private readonly oauthClient: OAuth2Client; | ||
|
||
constructor(clientId: string, fetchFn: typeof fetch) { | ||
this.oauthClient = new OAuth2Client({ | ||
server: "https://auth.tdameritrade.com/", | ||
clientId, | ||
clientSecret: "", | ||
tokenEndpoint: "https://api.tdameritrade.com/v1/oauth2/token", | ||
authorizationEndpoint: "/auth", | ||
authenticationMethod: "client_secret_post", | ||
// https://github.com/badgateway/oauth2-client/issues/105 | ||
fetch: fetchFn, | ||
}); | ||
} | ||
|
||
async authenticateWithRetry(token: OAuth2Token): Promise<AuthResult> { | ||
const { oauthClient } = this; | ||
let client = new WsJsonClient(token.accessToken); | ||
try { | ||
await client.authenticate(); | ||
return { token, client }; | ||
} catch (e) { | ||
// refresh token and retry | ||
try { | ||
const newToken = await oauthClient.refreshToken(token); | ||
client = new WsJsonClient(newToken.accessToken); | ||
await client.authenticate(); | ||
logger(`Successfully refreshed token: ${JSON.stringify(newToken)}`); | ||
return { token: newToken, client }; | ||
} catch (e) { | ||
console.error(`Failed to refresh token`, e); | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export type AuthResult = { | ||
token: OAuth2Token; | ||
client: WsJsonClient; | ||
} |
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