Skip to content

v0.31.0

Choose a tag to compare

@hogan-yuan hogan-yuan released this 20 Nov 02:01
· 6 commits to main since this release
2bb61e3

Adds the risk-assessment API and OAuth revoke/clear, and fixes two security issues in the OAuth callback.

Added

  • TradeContext.RiskAssessment(ctx) — returns the current member's risk assessment (suitability questionnaire) result from GET /v1/members/me/risk-assessment as *trade.RiskAssessment, mirroring the server response field-for-field: Risk (e.g. A5), Pi, Approvals (map[string]string, e.g. car / cka / risk_audit → pass), ReAuthentication, PiEndAt, Deadlines (map[string]int64), RiskAssessmentAt (the server's date string as-is, e.g. 2026-10-14), Score and StrategyRiskLevels. Decoding follows the same lenient rules as the Rust SDK, so a change in how the gateway encodes a value cannot fail the call: integers (including map values) are accepted as JSON integers or integer strings and anything else (null, floats, out-of-range values) becomes 0; string fields, approval values and strategy risk levels keep strings as-is, turn null into "" and render any other value as its compact JSON text (non-integer numbers keep their original text, e.g. 1.50, where Rust normalizes them to 1.5); a map or list field holding anything other than an object / array (e.g. null, or [] for a map) becomes empty; the maps and the slice are never nil. New jsontypes.FlexInt64 (int64 that also accepts a numeric string). Pi, PiEndAt and Deadlines are not interpreted or unit-converted because the server documents neither the PI code values nor the time unit (seconds or milliseconds), Risk has no documented scale (do not infer an ordering between levels), and Approvals values are strings, not booleans (is_corporate_client arrives as "true"/"false")
  • Typed OAuth errors — oauth.ErrNoToken (no usable token: Build not called, token cleared, or expired / expiring within the hour without a refresh token; returned by AccessToken, and by HTTP requests that need a token, so errors.Is(err, oauth.ErrNoToken) works end to end), oauth.ErrTokenCleared (a refresh or authorization was discarded because Clear ran) and *oauth.RevokeError. The error messages are unchanged
  • OAuth.Revoke(ctx) (ports longbridge/openapi#616) — revokes the current token on the server through the RFC 7009 endpoint (POST /oauth2/revoke): the refresh token when present, otherwise the access token. It only contacts the server; the in-memory and cached tokens are left untouched. A rejection is returned as *oauth.RevokeError (StatusCode, Code, Description, URI, from the server's error / error_description / error_uri; the message ends with (see <uri>) when present); with no token the error wraps oauth.ErrNoToken. The request uses the *http.Client in ctx under oauth2.HTTPClient when present, like token refreshes, otherwise a client with a 30-second timeout (token refreshes and the authorization code exchange now use the same default; a nil *http.Client in ctx also falls back to it). It is serialized with token refreshes (ports longbridge/openapi#617), so a refresh running concurrently in AccessToken or Build cannot rotate the refresh token after Revoke has read it (which would revoke the stale token and leave the new one valid); while it runs, AccessToken calls that need a refresh wait for it (waiting honors their ctx). A browser authorization still running in Build is not serialized with it
  • OAuth.Clear() (ports longbridge/openapi#616) — drops the token locally: deletes the cached token file (~/.longbridge/openapi/tokens/<client_id>; a missing file is not an error) and then clears the in-memory token. It does not contact the server, so call Revoke first to disconnect completely. After Clear, AccessToken fails until Build is called again. If deleting the file fails, the error is returned and the in-memory token is kept; a refresh or authorization that was in flight in AccessToken or Build when Clear ran is discarded instead of storing the token again (Build then returns ErrTokenCleared, unless a valid token was stored after Clear, which it keeps)

Fixed

  • OAuth authorization errors include error_description (ports longbridge/openapi#619) — an error redirected back by the server now reads e.g. oauth: authorization failed: internal_server_error: validate client err:oauth client not found instead of only the error code, which makes an unknown or placeholder client ID easy to spot. The text comes from the redirect, so control characters, line/paragraph separators, zero-width characters and bidi overrides are replaced with spaces, which also defuses an ANSI escape sequence (only its leading ESC is a control character; the rest stays as literal text). error and error_description are each capped at 512 characters, with … appended when a value was truncated
  • Security: the local OAuth callback page HTML-escapes the error text (ports longbridge/openapi#619) — it was inserted as raw HTML, so any web page could redirect the browser to http://localhost:<port>/callback?error=<script>… and run script on the localhost origin while an authorization was pending
  • Security: the OAuth callback checks state for every redirect, error redirects included (ports longbridge/openapi#619) — a redirect with a wrong or missing state is answered with an error page and ignored, and the flow keeps waiting for the real one. Previously any web page could end a pending authorization with an error text of its choosing; a mismatched successful redirect ended it with oauth: CSRF state mismatch, which is no longer returned (without a valid redirect the flow ends with the 5-minute authorization timeout). The callback accepts only GET (and HEAD; other methods get 405, also under GODEBUG=httpmuxgo121=1), a later redirect no longer blocks its handler goroutine, and the callback server is shut down with the flow (keep-alive connections included) once the page that ended it has been sent, waiting at most 300 ms
  • Build loading a different valid token file discards an in-flight refresh — a refresh of the previous token that finished afterwards could overwrite the token just loaded from disk. Reloading the same token (the file this client wrote) does not, and a successful refresh (in AccessToken or Build) wins over a "replacement" that still carries the refresh token it consumed, so a server-rotated refresh token is not lost when Build reloads the same token or a stale copy of it
  • OAuth token refreshes have a default 30-second timeout — AccessToken / Build refreshed through http.DefaultClient (no timeout) unless ctx carried an oauth2.HTTPClient; a hung refresh now fails instead of blocking every caller waiting for it (and Revoke); the default client is a copy of http.DefaultClient (so a proxy or mTLS transport, cookie jar or redirect policy configured there still applies), with the 30-second timeout added only when it has none
  • Concurrent OAuth.AccessToken calls refresh the token once — every caller that found the token expiring started its own refresh; when the server rotates refresh tokens, the later refreshes used an already-rotated token and failed. Refreshes are now serialized and a caller that waited reuses the token the first one stored; waiting for the refresh in flight honors the caller's ctx (a cancelled wait returns ctx.Err() unwrapped). Build likewise reuses a token refreshed while it waited instead of refreshing again with the rotated refresh token it read from disk
  • The OAuth browser authorization flow uses PKCE (RFC 7636, S256; ports longbridge/openapi#616) — the authorization request carries a code_challenge and the token exchange sends the matching code_verifier. The server requires PKCE for public clients, so such clients (e.g. those created through dynamic client registration) previously could not authorize through the SDK. Clients that don't require PKCE are unaffected