Skip to content

Commit

Permalink
authenticate with retries
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecsl committed Jun 19, 2023
1 parent 86c41f8 commit 60a3f0d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 15 deletions.
30 changes: 16 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "tda-wsjson-client",
"version": "0.2.0",
"version": "0.3.0",
"description": "WebSocket client for the TD Ameritrade wsjson API",
"main": "dist/web.bundle.js",
"types": "dist/web.d.ts",
"exports": {
"./wsJsonClient": "./dist/client/wsJsonClient.js",
"./wsJsonClientAuth": "./dist/client/wsJsonClientAuth.js",
"./alertTypes": "./dist/client/types/alertTypes.js",
"./tdaWsJsonTypes": "./dist/client/tdaWsJsonTypes.js",
"./messageTypeHelpers": "./dist/client/messageTypeHelpers.js",
Expand Down Expand Up @@ -33,6 +34,7 @@
"@babel/core": "^7.15.8",
"@babel/preset-env": "^7.16.0",
"@babel/preset-typescript": "^7.16.0",
"@badgateway/oauth2-client": "^2.2.0",
"@types/lodash": "^4.14.186",
"@types/node": "^20.3.0",
"debug": "^4.3.4",
Expand All @@ -43,16 +45,9 @@
"resolve": "1.22.2",
"ws": "^8.13.0"
},
"author": "felipe lima",
"author": "Felipe Lima",
"license": "MIT",
"devDependencies": {
"eslint": "^8.42.0",
"eslint-plugin-flowtype": "^8.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-testing-library": "^5.9.1",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@types/debug": "^4.1.8",
"@types/jest": "^29.4.0",
Expand All @@ -61,15 +56,22 @@
"@typescript-eslint/parser": "^5.59.9",
"dotenv": "^16.1.4",
"esbuild": "^0.18.0",
"prettier": "^2.4.1",
"ts-node": "^10.4.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.0.4",
"eslint": "^8.42.0",
"eslint-plugin-flowtype": "^8.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-testing-library": "^5.9.1",
"jest": "29.5.0",
"jest-circus": "^29.4.1",
"jest-resolve": "29.5.0",
"jest-watch-typeahead": "2.2.2",
"jest-websocket-mock": "^2.4.0"
"jest-websocket-mock": "^2.4.0",
"prettier": "^2.4.1",
"ts-node": "^10.4.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.0.4"
}
}
48 changes: 48 additions & 0 deletions src/client/wsJsonClientAuth.ts
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;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"allowJs": true,
"moduleResolution": "nodenext",
"module": "commonjs",
"lib": ["es2020"]
"lib": ["es2020", "dom"]
},
"include": ["src/**/*"],
"exclude": ["./node_modules", "dist", "src/test/**/*"]
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,11 @@
"@babel/helper-validator-identifier" "^7.22.5"
to-fast-properties "^2.0.0"

"@badgateway/oauth2-client@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@badgateway/oauth2-client/-/oauth2-client-2.2.0.tgz#c868576329af49104b32c07660c4f8b11363e292"
integrity sha512-P8roB2b800UOW9Lny8tzhhi/ZUH7rYcS+O+GOdeJT6waPRjU1JQLPfybCR9Rr58INQbbTXExtZbQqovZCAGP2g==

"@bcoe/v8-coverage@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
Expand Down

0 comments on commit 60a3f0d

Please sign in to comment.