fix(exchange): verify Trading212 credentials against an authenticated endpoint#1179
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes credential verification for Trading212Broker by introducing a dedicated verifyCredentials() hook on the base Broker and updating authentication flow to use it, ensuring Trading212 keys are validated via an authenticated endpoint rather than a local-time stub.
Changes:
- Added
Broker.verifyCredentials(): Promise<void>with a default implementation that probes viagetTime()and documentation requiring overrides whengetTime()is not network-authenticated. - Implemented
Trading212Broker.verifyCredentials()usingGET /api/v0/equity/account/cashto perform a low-cost authenticated probe. - Updated
getAuthenticatedBrokerClientto callverifyCredentials()and added tests covering both success and failure propagation.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/exchange/src/broker/Broker.ts | Adds the new verifyCredentials() API with a default probe and guidance for subclasses. |
| packages/exchange/src/broker/trading212/Trading212Broker.ts | Overrides verifyCredentials() to validate Trading212 credentials via an authenticated endpoint. |
| packages/exchange/src/broker/getAuthenticatedBrokerClient.ts | Switches the authentication probe from getTime() to verifyCredentials(). |
| packages/exchange/src/broker/trading212/Trading212Broker.test.ts | Adds unit tests verifying Trading212 credential verification success/failure behavior. |
| packages/exchange/src/broker/getAuthenticatedBrokerClient.test.ts | Adds unit tests ensuring verification is invoked and failures are propagated. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getAuthenticatedBrokerClientclaimed to prove credentials by callingbroker.getTime(). That works for Alpaca (/v2/clockis authenticated), butTrading212Broker.getTime()returns the local clock without any network call (Trading212 has no server-time endpoint) — so Trading212 credentials were never actually validated and bad keys only surfaced at first real use.Broker: newverifyCredentials(): Promise<void>with a default implementation that probes viagetTime(); documented that subclasses whosegetTime()never leaves the process MUST override. Public API is additive — no removals.Trading212Broker: overridesverifyCredentials()to hitGET /api/v0/equity/account/cash— the cheapest authenticated Trading212 endpoint (1 req / 2s rate limit, vs. 30s for account info pergetRetryDelay). A 401 is not retried by axios-retry (isRetryableErrorexcludes 4xx), so bad keys reject immediately with aSimplifiedHttpError.getAuthenticatedBrokerClient: callsverifyCredentials()instead ofgetTime(); error semantics for callers are unchanged (auth failures still reject with the underlyingSimplifiedHttpError).AlpacaBroker: unchanged — itsgetTime()already performs an authenticated/v2/clockrequest, so the default probe is correct.Test plan
Trading212Broker.test.ts:verifyCredentialsresolves on a mocked account-cash response and rejects with aSimplifiedHttpError(status 401) when the API denies the credentials.getAuthenticatedBrokerClient.test.ts: returns the client after successful verification and propagates verification failures (mockedgetBrokerClientfactory).packages/exchange:npm test— 10 files, 89 tests passed;npm run lint— 0 errors (6 pre-existing unused-directive warnings).packages/messaging(consumes this viagetAccountBrokerClient): built dependencies,npm test— 6 files, 84 tests passed, no fallout.