-
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
8 changed files
with
710 additions
and
104 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
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,57 +1,31 @@ | ||
import { OAuth2Client, OAuth2Token } from "@badgateway/oauth2-client"; | ||
import { WsJsonClient } from "./wsJsonClient.js"; | ||
import debug from "debug"; | ||
import { WsJsonClient } from "./wsJsonClient.js"; | ||
|
||
// @ts-ignore | ||
const logger = debug("wsJsonClientAuth"); | ||
|
||
export default class WsJsonClientAuth { | ||
private readonly oauthClient: OAuth2Client; | ||
|
||
constructor( | ||
private readonly wsJsonClientFactory: () => WsJsonClient, | ||
clientId: string, | ||
originalFetch: typeof fetch | ||
) { | ||
this.oauthClient = new OAuth2Client({ | ||
server: "https://trade.thinkorswim.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: (...args) => originalFetch(...args), | ||
}); | ||
} | ||
constructor(private readonly wsJsonClientFactory: () => WsJsonClient) {} | ||
|
||
async authenticateWithRetry(token: OAuth2Token): Promise<AuthResult> { | ||
async authenticateWithRetry(authCode: string): Promise<WsJsonClient> { | ||
const client = this.wsJsonClientFactory(); | ||
try { | ||
await client.authenticate(token.accessToken); | ||
return { token, client }; | ||
} catch (e) { | ||
return await this.refreshToken(token); | ||
} | ||
await client.authenticate(authCode); | ||
return client; | ||
} | ||
|
||
async refreshToken(token: OAuth2Token): Promise<AuthResult> { | ||
logger("attempting token refresh"); | ||
const { oauthClient } = this; | ||
try { | ||
const newToken = await oauthClient.refreshToken(token); | ||
const client = this.wsJsonClientFactory(); | ||
await client.authenticate(newToken.accessToken); | ||
// oauthClient.refreshToken() doesn't return the refresh token so we need to re-add it | ||
const refreshedToken = { ...newToken, refreshToken: token.refreshToken }; | ||
return { token: refreshedToken, client }; | ||
} catch (e) { | ||
console.error(`Failed to refresh token`, e); | ||
throw e; | ||
} | ||
} | ||
// async refreshToken(token: OAuth2Token): Promise<AuthResult> { | ||
// logger("attempting token refresh"); | ||
// const { oauthClient } = this; | ||
// try { | ||
// const newToken = await oauthClient.refreshToken(token); | ||
// const client = this.wsJsonClientFactory(); | ||
// await client.authenticate(newToken.accessToken); | ||
// // oauthClient.refreshToken() doesn't return the refresh token so we need to re-add it | ||
// const refreshedToken = { ...newToken, refreshToken: token.refreshToken }; | ||
// return { token: refreshedToken, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import puppeteer from "puppeteer-extra"; | ||
import StealthPlugin from "puppeteer-extra-plugin-stealth"; | ||
import { PuppeteerExtra } from "puppeteer-extra"; | ||
|
||
export async function getAuthCode() { | ||
(puppeteer as unknown as PuppeteerExtra).use(StealthPlugin()); | ||
|
||
return new Promise<string>(async (resolve) => { | ||
const browser = await (puppeteer as unknown as PuppeteerExtra).launch({ | ||
headless: false, // or 'new' in the latest Puppeteer to get partial headless mode | ||
args: ["--no-sandbox", "--disable-setuid-sandbox"], | ||
}); | ||
|
||
const page = await browser.newPage(); | ||
await page.setRequestInterception(true); | ||
|
||
console.log( | ||
"Please log in manually. The script will watch for the final redirect URL." | ||
); | ||
|
||
let oauthCode = null; | ||
|
||
page.on("request", async (request) => { | ||
const reqUrl = request.url(); | ||
// If the request includes the OAuth code, capture it and abort | ||
if (reqUrl.includes("trade.thinkorswim.com/oauth?code=")) { | ||
const urlObj = new URL(reqUrl); | ||
oauthCode = urlObj.searchParams.get("code"); | ||
console.log("OAuth Code Captured:", oauthCode); | ||
// Abort the request so the code isn't consumed | ||
return request.abort(); | ||
} | ||
|
||
// Otherwise, let all other requests proceed normally | ||
request.continue(); | ||
}); | ||
|
||
await page.goto("https://trade.thinkorswim.com/"); | ||
|
||
// Wait until we pick up the code | ||
while (!oauthCode) { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
} | ||
|
||
await browser.close(); | ||
resolve(oauthCode); | ||
}); | ||
} |
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.