tigeropen 0.4.0 → 0.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/README.md +82 -2
  2. package/dist/cjs/auth/authentication.js +74 -0
  3. package/dist/cjs/auth/index.js +24 -0
  4. package/dist/cjs/auth/oauth2/client-registration.js +44 -0
  5. package/dist/cjs/auth/oauth2/device-authorization.js +40 -0
  6. package/dist/cjs/auth/oauth2/errors.js +64 -0
  7. package/dist/cjs/auth/oauth2/http-utils.js +112 -0
  8. package/dist/cjs/auth/oauth2/index.js +42 -0
  9. package/dist/cjs/auth/oauth2/loopback-receiver.js +215 -0
  10. package/dist/cjs/auth/oauth2/metadata.js +34 -0
  11. package/dist/cjs/auth/oauth2/oauth2-authentication.js +74 -0
  12. package/dist/cjs/auth/oauth2/session-manager.js +762 -0
  13. package/dist/cjs/auth/oauth2/token-store.js +252 -0
  14. package/dist/cjs/auth/oauth2/token.js +76 -0
  15. package/dist/cjs/client/api-response.js +28 -7
  16. package/dist/cjs/client/case-convert.js +4 -3
  17. package/dist/cjs/client/http-client.js +124 -8
  18. package/dist/cjs/client/token-methods.js +42 -0
  19. package/dist/cjs/config/client-config.js +115 -16
  20. package/dist/cjs/config/config-parser.js +19 -9
  21. package/dist/cjs/config/index.js +4 -1
  22. package/dist/cjs/config/token-manager.js +54 -4
  23. package/dist/cjs/index.js +53 -2
  24. package/dist/cjs/model/enums.js +18 -1
  25. package/dist/cjs/model/order-helpers.js +149 -0
  26. package/dist/cjs/push/index.js +1 -0
  27. package/dist/cjs/push/pb/Request.js +12 -1
  28. package/dist/cjs/push/proto-message.js +5 -1
  29. package/dist/cjs/push/push-client.js +69 -25
  30. package/dist/cjs/push/tick-util.js +172 -0
  31. package/dist/cjs/quote/quote-client.js +300 -70
  32. package/dist/cjs/trade/trade-client.js +186 -16
  33. package/dist/cjs/version.js +4 -0
  34. package/dist/esm/auth/authentication.js +68 -0
  35. package/dist/esm/auth/index.js +3 -0
  36. package/dist/esm/auth/oauth2/client-registration.js +40 -0
  37. package/dist/esm/auth/oauth2/device-authorization.js +36 -0
  38. package/dist/esm/auth/oauth2/errors.js +59 -0
  39. package/dist/esm/auth/oauth2/http-utils.js +105 -0
  40. package/dist/esm/auth/oauth2/index.js +11 -0
  41. package/dist/esm/auth/oauth2/loopback-receiver.js +178 -0
  42. package/dist/esm/auth/oauth2/metadata.js +30 -0
  43. package/dist/esm/auth/oauth2/oauth2-authentication.js +70 -0
  44. package/dist/esm/auth/oauth2/session-manager.js +725 -0
  45. package/dist/esm/auth/oauth2/token-store.js +214 -0
  46. package/dist/esm/auth/oauth2/token.js +71 -0
  47. package/dist/esm/client/api-request.js +1 -1
  48. package/dist/esm/client/api-response.js +29 -8
  49. package/dist/esm/client/case-convert.js +4 -3
  50. package/dist/esm/client/http-client.js +128 -12
  51. package/dist/esm/client/index.js +6 -6
  52. package/dist/esm/client/token-methods.js +37 -0
  53. package/dist/esm/config/client-config.js +118 -20
  54. package/dist/esm/config/config-parser.js +19 -9
  55. package/dist/esm/config/index.js +4 -3
  56. package/dist/esm/config/token-manager.js +55 -5
  57. package/dist/esm/index.js +15 -1
  58. package/dist/esm/logger/index.js +1 -1
  59. package/dist/esm/model/contract-helpers.js +1 -1
  60. package/dist/esm/model/enums.js +17 -0
  61. package/dist/esm/model/order-helpers.js +142 -1
  62. package/dist/esm/model/order-status.js +1 -1
  63. package/dist/esm/package.json +3 -0
  64. package/dist/esm/push/index.js +6 -5
  65. package/dist/esm/push/pb/PushData.js +11 -11
  66. package/dist/esm/push/pb/QuoteBasicData.js +1 -1
  67. package/dist/esm/push/pb/Request.js +12 -1
  68. package/dist/esm/push/pb/Response.js +1 -1
  69. package/dist/esm/push/pb/index.js +17 -17
  70. package/dist/esm/push/proto-message.js +8 -4
  71. package/dist/esm/push/push-client.js +76 -32
  72. package/dist/esm/push/tick-util.js +165 -0
  73. package/dist/esm/quote/index.js +1 -1
  74. package/dist/esm/quote/quote-client.js +269 -72
  75. package/dist/esm/signer/index.js +2 -2
  76. package/dist/esm/trade/index.js +1 -1
  77. package/dist/esm/trade/trade-client.js +156 -19
  78. package/dist/esm/version.js +1 -0
  79. package/dist/types/auth/authentication.d.ts +98 -0
  80. package/dist/types/auth/index.d.ts +4 -0
  81. package/dist/types/auth/oauth2/client-registration.d.ts +32 -0
  82. package/dist/types/auth/oauth2/device-authorization.d.ts +40 -0
  83. package/dist/types/auth/oauth2/errors.d.ts +46 -0
  84. package/dist/types/auth/oauth2/http-utils.d.ts +21 -0
  85. package/dist/types/auth/oauth2/index.d.ts +16 -0
  86. package/dist/types/auth/oauth2/loopback-receiver.d.ts +29 -0
  87. package/dist/types/auth/oauth2/metadata.d.ts +32 -0
  88. package/dist/types/auth/oauth2/oauth2-authentication.d.ts +31 -0
  89. package/dist/types/auth/oauth2/session-manager.d.ts +241 -0
  90. package/dist/types/auth/oauth2/token-store.d.ts +49 -0
  91. package/dist/types/auth/oauth2/token.d.ts +56 -0
  92. package/dist/types/client/api-response.d.ts +6 -4
  93. package/dist/types/client/case-convert.d.ts +3 -1
  94. package/dist/types/client/http-client.d.ts +32 -1
  95. package/dist/types/client/token-methods.d.ts +34 -0
  96. package/dist/types/config/client-config.d.ts +22 -1
  97. package/dist/types/config/index.d.ts +3 -1
  98. package/dist/types/config/token-manager.d.ts +27 -3
  99. package/dist/types/index.d.ts +22 -1
  100. package/dist/types/model/enums.d.ts +18 -2
  101. package/dist/types/model/order-helpers.d.ts +52 -1
  102. package/dist/types/model/order.d.ts +127 -10
  103. package/dist/types/model/quote-requests.d.ts +27 -15
  104. package/dist/types/model/quote.d.ts +151 -24
  105. package/dist/types/model/trade-requests.d.ts +123 -4
  106. package/dist/types/model/trade.d.ts +121 -17
  107. package/dist/types/push/callbacks.d.ts +3 -7
  108. package/dist/types/push/index.d.ts +2 -1
  109. package/dist/types/push/pb/Request.d.ts +1 -0
  110. package/dist/types/push/proto-message.d.ts +4 -1
  111. package/dist/types/push/push-client.d.ts +38 -3
  112. package/dist/types/push/tick-util.d.ts +30 -0
  113. package/dist/types/quote/quote-client.d.ts +101 -36
  114. package/dist/types/trade/trade-client.d.ts +69 -14
  115. package/dist/types/version.d.ts +1 -0
  116. package/package.json +9 -4
@@ -1,6 +1,17 @@
1
- import { createApiRequest } from '../client/api-request';
2
- import { unmarshalData } from '../client/api-response';
3
- import { normalizeOrderStatus } from '../model/order-status';
1
+ /**
2
+ * TradeClient — trading client.
3
+ *
4
+ * All methods return strongly-typed responses; request parameters are
5
+ * written in camelCase in TypeScript and converted to snake_case on the wire.
6
+ *
7
+ * v0.4.0: 所有查询类方法改为 Request 对象签名(OrdersRequest 等),补齐 17
8
+ * 个新方法(账户管理 / 资产分析 / 资金调拨 / 持仓划转等)。
9
+ */
10
+ import { HttpClient } from '../client/http-client.js';
11
+ import { createApiRequest } from '../client/api-request.js';
12
+ import { unmarshalData } from '../client/api-response.js';
13
+ import * as tokenMethods from '../client/token-methods.js';
14
+ import { normalizeOrderStatus } from '../model/order-status.js';
4
15
  /** Normalize `Order.status` field in-place (handles int → string). */
5
16
  function normalizeOrderInPlace(o) {
6
17
  if (o && o.status !== undefined && o.status !== null) {
@@ -22,12 +33,51 @@ function withAccount(req, account) {
22
33
  }
23
34
  /** Trading client wrapping all trade-related APIs. */
24
35
  export class TradeClient {
25
- constructor(httpClient, account) {
36
+ constructor(httpClient, account, secretKey) {
26
37
  this.httpClient = httpClient;
27
38
  this.account = account;
39
+ this.secretKey = secretKey;
40
+ }
41
+ /** Create a TradeClient directly from a ClientConfig — no need to construct HttpClient manually. */
42
+ static fromConfig(config, account, secretKey) {
43
+ return new TradeClient(new HttpClient(config), account, secretKey);
44
+ }
45
+ // ── Token management ──────────────────────────────────────────────────────
46
+ /** Call the `user_token_refresh` API and return the new token. Read-only. */
47
+ async queryToken() {
48
+ return tokenMethods.queryToken(this.httpClient);
49
+ }
50
+ /**
51
+ * Refresh the token: call the API, update the in-memory config token, and
52
+ * optionally persist via the provided TokenManager (file + writer callback).
53
+ *
54
+ * @param tokenManager - optional TokenManager for file persistence and writer callback
55
+ */
56
+ async refreshToken(tokenManager) {
57
+ return tokenMethods.refreshToken(this.httpClient, tokenManager);
58
+ }
59
+ /**
60
+ * Start background token auto-refresh. Returns a TokenManager that can be
61
+ * used to stop the background task via `tm.stopAutoRefresh()`.
62
+ *
63
+ * @param refreshDurationSecs - Refresh threshold in seconds (minimum 30).
64
+ * @param checkIntervalMs - How often to check, in milliseconds (default 300 000 / 5 min).
65
+ * @param tokenWriter - Optional callback invoked after each successful refresh.
66
+ */
67
+ startTokenAutoRefresh(refreshDurationSecs, checkIntervalMs, tokenWriter) {
68
+ return tokenMethods.startTokenAutoRefresh(this.httpClient, refreshDurationSecs, checkIntervalMs, tokenWriter);
28
69
  }
29
70
  async callInto(method, bizParams) {
30
- const request = createApiRequest(method, bizParams);
71
+ // Inject secretKey into every request when available (institution accounts require it).
72
+ // Individual methods that already set secretKey will not be overwritten (|| not &&).
73
+ let params = bizParams;
74
+ if (this.secretKey && params !== null && typeof params === 'object' && !Array.isArray(params)) {
75
+ const p = params;
76
+ if (p['secretKey'] == null && p['secret_key'] == null) {
77
+ params = { ...p, secretKey: this.secretKey };
78
+ }
79
+ }
80
+ const request = createApiRequest(method, params);
31
81
  const response = await this.httpClient.executeRequest(request);
32
82
  return unmarshalData(response.data);
33
83
  }
@@ -59,17 +109,60 @@ export class TradeClient {
59
109
  });
60
110
  }
61
111
  // === Order operations ===
112
+ /**
113
+ * Prepare an order request for the wire.
114
+ *
115
+ * The gateway expects `algo_params` as a `[{tag, value}, ...]` array
116
+ * (matching the Python SDK's AlgoParams.to_dict serialization), not the
117
+ * plain object shape that reads naturally in TypeScript. This helper
118
+ * normalizes the object form into the array form on outbound payloads
119
+ * so callers can pass the natural object and TWAP/VWAP orders work end
120
+ * to end without wire-format workarounds.
121
+ *
122
+ * Fields with undefined/null values are dropped; empty algoParams
123
+ * becomes an empty array rather than being stripped, matching the
124
+ * Python SDK behavior when the user passes an empty AlgoParams().
125
+ */
126
+ marshalOrderForWire(order) {
127
+ const { algoParams, ...rest } = order;
128
+ const withAccount = { ...rest, account: this.account };
129
+ if (algoParams === undefined)
130
+ return withAccount;
131
+ // Already a tag/value array (advanced callers who want raw control)
132
+ if (Array.isArray(algoParams)) {
133
+ withAccount.algoParams = algoParams;
134
+ return withAccount;
135
+ }
136
+ // Object shape → tag/value array.
137
+ // Keys are converted to snake_case explicitly here (e.g. `startTime` →
138
+ // `start_time`) so they land correctly in the `tag` string that the
139
+ // gateway reads. `keysToSnakeCase` only rewrites object *keys*, not
140
+ // string *values*, so `tag` would be left camelCase if we didn't do
141
+ // this conversion ourselves.
142
+ const tagValues = [];
143
+ for (const [key, value] of Object.entries(algoParams)) {
144
+ if (value === undefined || value === null)
145
+ continue;
146
+ const snakeKey = key.replace(/[A-Z]/g, (c) => '_' + c.toLowerCase());
147
+ tagValues.push({ tag: snakeKey, value });
148
+ }
149
+ withAccount.algoParams = tagValues;
150
+ return withAccount;
151
+ }
62
152
  async placeOrder(order) {
63
- return this.callInto('place_order', { ...order, account: this.account });
153
+ return this.callInto('place_order', this.marshalOrderForWire(order));
64
154
  }
65
155
  async previewOrder(order) {
66
- return this.callInto('preview_order', { ...order, account: this.account });
156
+ return this.callInto('preview_order', this.marshalOrderForWire(order));
67
157
  }
68
158
  async modifyOrder(id, order) {
69
- return this.callInto('modify_order', { ...order, account: this.account, id });
159
+ return this.callInto('modify_order', { ...this.marshalOrderForWire(order), id });
70
160
  }
71
- async cancelOrder(id) {
72
- return this.callInto('cancel_order', { account: this.account, id });
161
+ async cancelOrder(id, secretKey) {
162
+ const params = { account: this.account, id };
163
+ if (secretKey !== undefined)
164
+ params.secretKey = secretKey; // per-call override takes precedence; undefined means use config default
165
+ return this.callInto('cancel_order', params);
73
166
  }
74
167
  // === Order queries (BREAKING v0.4.0) ===
75
168
  /**
@@ -106,14 +199,11 @@ export class TradeClient {
106
199
  }
107
200
  /**
108
201
  * Get a single order by ID (v0.4.0 new).
109
- * Wire method: `orders` with id/order_id param; server returns a single Order object
110
- * (not `{items:[]}` envelope).
111
- *
112
202
  * Returns `undefined` if no match.
113
203
  */
114
204
  async getOrder(req) {
115
205
  const merged = withAccount(req, this.account);
116
- const out = await this.callInto('orders', merged);
206
+ const out = await this.callInto('order_no', merged);
117
207
  if (!out || (!out.id && !out.orderId))
118
208
  return undefined;
119
209
  normalizeOrderInPlace(out);
@@ -131,11 +221,17 @@ export class TradeClient {
131
221
  async getPositions(req) {
132
222
  return this.callIntoItems('positions', withAccount(req, this.account));
133
223
  }
134
- /** Query assets. v0.4.0: supports sub-account list and aggregation options. */
224
+ /**
225
+ * Query assets. v0.4.0: supports sub-account list and aggregation options.
226
+ * `assetQuoteType`(ETH / RTH / OVERNIGHT) 选择资产行情口径。
227
+ */
135
228
  async getAssets(req) {
136
229
  return this.callIntoItems('assets', withAccount(req, this.account));
137
230
  }
138
- /** Query prime assets. v0.4.0: takes AssetsRequest for filters. */
231
+ /**
232
+ * Query prime assets. v0.4.0: takes AssetsRequest for filters.
233
+ * `assetQuoteType`(ETH / RTH / OVERNIGHT) 选择资产行情口径。
234
+ */
139
235
  async getPrimeAssets(req) {
140
236
  return this.callInto('prime_assets', withAccount(req, this.account));
141
237
  }
@@ -156,7 +252,10 @@ export class TradeClient {
156
252
  async getAnalyticsAsset(req) {
157
253
  return this.callIntoItems('analytics_asset', withAccount(req, this.account));
158
254
  }
159
- /** Aggregate assets (prime) by base currency. */
255
+ /**
256
+ * Aggregate assets (prime) by base currency.
257
+ * `assetQuoteType`(ETH / RTH / OVERNIGHT) 选择资产行情口径。
258
+ */
160
259
  async getAggregateAssets(req) {
161
260
  return this.callInto('aggregate_assets', withAccount(req, this.account));
162
261
  }
@@ -191,9 +290,10 @@ export class TradeClient {
191
290
  async getFundDetails(req) {
192
291
  return this.callIntoItems('fund_details', withAccount(req, this.account));
193
292
  }
194
- /** Funding history (wire method: `transfer_fund`). */
293
+ /** Funding history (wire method: `transfer_fund`).
294
+ * Server returns bare list (no items wrapper). */
195
295
  async getFundingHistory(req) {
196
- return this.callIntoItems('transfer_fund', withAccount(req, this.account));
296
+ return this.callInto('transfer_fund', withAccount(req, this.account));
197
297
  }
198
298
  // ===== v0.4.0 NEW: Internal / external position transfer =====
199
299
  /**
@@ -227,4 +327,41 @@ export class TradeClient {
227
327
  merged.accountId = this.account;
228
328
  return this.callIntoItems('position_transfer_external_records', merged);
229
329
  }
330
+ /** 行权检验:预估行权/作废后正股持仓变化 (wire: option_exercise_check) */
331
+ async checkOptionExercise(req) {
332
+ const merged = { ...req };
333
+ if (!merged.account)
334
+ merged.account = this.account;
335
+ return this.callInto('option_exercise_check', merged);
336
+ }
337
+ /** 查询可行权持仓列表 (wire: option_exercise_position) */
338
+ async getOptionExercisePositions(req) {
339
+ const merged = { ...req };
340
+ if (!merged.account)
341
+ merged.account = this.account;
342
+ return this.callInto('option_exercise_position', merged);
343
+ }
344
+ /** 提交行权/作废申请 (wire: option_exercise_submit) */
345
+ async submitOptionExercise(req) {
346
+ const merged = { ...req };
347
+ if (!merged.account)
348
+ merged.account = this.account;
349
+ const result = await this.callInto('option_exercise_submit', merged);
350
+ return result ?? false;
351
+ }
352
+ /** 分页查询行权记录 (wire: option_exercise_record) */
353
+ async getOptionExerciseRecords(req) {
354
+ const merged = { ...req };
355
+ if (!merged.account)
356
+ merged.account = this.account;
357
+ return this.callInto('option_exercise_record', merged);
358
+ }
359
+ /** 撤销行权申请 (wire: option_exercise_cancel) */
360
+ async cancelOptionExercise(req) {
361
+ const merged = { ...req };
362
+ if (!merged.account)
363
+ merged.account = this.account;
364
+ const result = await this.callInto('option_exercise_cancel', merged);
365
+ return result ?? false;
366
+ }
230
367
  }
@@ -0,0 +1 @@
1
+ export const SDK_VERSION = '0.5.7';
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Request authentication interfaces.
3
+ *
4
+ * Implementations attach credentials, handle authentication retries, and release
5
+ * resources. OAuth 2.0 authorization flows live in `OAuth2SessionManager`; this module
6
+ * is the data plane and knows nothing about login.
7
+ */
8
+ /** Which kind of authentication. */
9
+ export declare enum AuthenticationType {
10
+ /** tigerId + RSA private key signature */
11
+ Signature = "SIGNATURE",
12
+ /** `Authorization: Bearer <access_token>` */
13
+ OAuth2 = "OAUTH2"
14
+ }
15
+ /**
16
+ * Per-request authentication context.
17
+ *
18
+ * Business parameters (`method`, `version`, `biz_content`, `timestamp`) are filled in by
19
+ * the client and must not be modified by an authentication implementation.
20
+ */
21
+ export declare class RequestAuthContext {
22
+ /** Request body parameters. Read-only as far as authentication is concerned. */
23
+ readonly params: Record<string, string>;
24
+ /**
25
+ * Whether a request may be retried with a new credential after a 401.
26
+ *
27
+ * Order placement, modification, and cancellation are not retryable because a 401
28
+ * does not prove that the operation was not executed.
29
+ */
30
+ readonly retryable: boolean;
31
+ /** Full `Authorization` header value; undefined means do not send the header. */
32
+ authorizationHeader?: string;
33
+ constructor(params: Record<string, string>, retryable?: boolean);
34
+ }
35
+ /**
36
+ * Credential snapshot used to coordinate authentication retries.
37
+ *
38
+ * Comparing the failed credential with current state detects rotation completed
39
+ * elsewhere and avoids duplicate refresh requests.
40
+ */
41
+ export declare class AuthenticationAttempt {
42
+ readonly authType: AuthenticationType;
43
+ /**
44
+ * Credential used by the request, including a raw OAuth 2.0 access token.
45
+ *
46
+ * This value is restricted to equality comparisons and must not be logged or exposed.
47
+ */
48
+ readonly credential: string | undefined;
49
+ constructor(authType: AuthenticationType, credential: string | undefined);
50
+ /** Never prints the credential value. */
51
+ toString(): string;
52
+ }
53
+ /**
54
+ * Authentication retry decision.
55
+ *
56
+ * A retry always supplies a new full `Authorization` header value, including the
57
+ * `Bearer ` prefix. There is deliberately no "retry with the same credential" option:
58
+ * that only collects the same 401 twice.
59
+ */
60
+ export declare class RetryDecision {
61
+ private static readonly NO_RETRY;
62
+ readonly shouldRetry: boolean;
63
+ readonly authorizationHeader: string | undefined;
64
+ private constructor();
65
+ static noRetry(): RetryDecision;
66
+ /** @param authorizationHeader full new header value, including `Bearer ` */
67
+ static retry(authorizationHeader: string): RetryDecision;
68
+ /** Never prints the header value. */
69
+ toString(): string;
70
+ }
71
+ /** Authentication interface. */
72
+ export interface Authentication {
73
+ /**
74
+ * Which kind of authentication. For logging and assertions; business logic should not
75
+ * branch on it.
76
+ */
77
+ readonly authType: AuthenticationType;
78
+ /**
79
+ * Write the credential into the request and return a snapshot of which credential was
80
+ * used.
81
+ *
82
+ * The OAuth2 implementation may refresh here, so this may reject with an
83
+ * `OAuth2Error`.
84
+ *
85
+ * @returns snapshot, passed back to {@link onUnauthorized}
86
+ */
87
+ apply(context: RequestAuthContext): Promise<AuthenticationAttempt>;
88
+ /**
89
+ * Return the retry decision for an unauthorized response.
90
+ *
91
+ * Refresh applies only to HTTP 401 credential failures, never to HTTP 403, server
92
+ * errors, or timeouts. Authentication retries occur at most once per request and
93
+ * require a different credential.
94
+ */
95
+ onUnauthorized(attempt: AuthenticationAttempt | undefined, statusCode: number): Promise<RetryDecision>;
96
+ /** Release resources. Must be called when switching authentication method. */
97
+ close(): void;
98
+ }
@@ -0,0 +1,4 @@
1
+ /** Authentication layer: the request-level data plane plus the OAuth 2.0 control plane. */
2
+ export { AuthenticationAttempt, AuthenticationType, RequestAuthContext, RetryDecision, } from './authentication';
3
+ export type { Authentication } from './authentication';
4
+ export * from './oauth2';
@@ -0,0 +1,32 @@
1
+ /**
2
+ * OAuth 2.0 dynamic client registration state (RFC 7591).
3
+ *
4
+ * Registration state is stored separately from tokens and retained across logout.
5
+ * `registeredScopes` records the granted scope set; scope expansion requires a new
6
+ * registration to avoid `invalid_scope`.
7
+ */
8
+ /** Raw on-disk shape, shared with the Java and Python SDKs. */
9
+ export interface OAuth2ClientRegistrationJson {
10
+ issuer?: string;
11
+ client_id?: string;
12
+ client_name?: string;
13
+ registered_scopes?: string;
14
+ client_id_issued_at?: number;
15
+ }
16
+ export declare class OAuth2ClientRegistration {
17
+ issuer?: string;
18
+ clientId?: string;
19
+ clientName?: string;
20
+ registeredScopes?: string;
21
+ clientIdIssuedAt: number;
22
+ constructor(init?: {
23
+ issuer?: string;
24
+ clientId?: string;
25
+ clientName?: string;
26
+ registeredScopes?: string;
27
+ clientIdIssuedAt?: number;
28
+ });
29
+ toJSON(): OAuth2ClientRegistrationJson;
30
+ static fromJSON(data: OAuth2ClientRegistrationJson | null | undefined): OAuth2ClientRegistration | null;
31
+ toString(): string;
32
+ }
@@ -0,0 +1,40 @@
1
+ /** Device authorization response (RFC 8628). */
2
+ /**
3
+ * Default polling interval per RFC 8628 §3.5. Tiger's authorization server does not
4
+ * return `interval`, so this default applies in practice.
5
+ */
6
+ export declare const DEFAULT_DEVICE_INTERVAL_SECONDS = 5;
7
+ /** Raw shape of the device authorization response. */
8
+ export interface OAuth2DeviceAuthorizationJson {
9
+ device_code?: string;
10
+ user_code?: string;
11
+ verification_uri?: string;
12
+ verification_uri_complete?: string;
13
+ expires_in?: number;
14
+ interval?: number;
15
+ }
16
+ export declare class OAuth2DeviceAuthorization {
17
+ /** Credential used to redeem the token; it must not be displayed or logged. */
18
+ deviceCode?: string;
19
+ /** Short code presented to the resource owner. */
20
+ userCode?: string;
21
+ /** Verification URI presented to the resource owner. */
22
+ verificationUri?: string;
23
+ /** Verification URI with the user code included. */
24
+ verificationUriComplete?: string;
25
+ expiresIn: number;
26
+ /** Polling interval in seconds; 0 means the server sent none */
27
+ interval: number;
28
+ constructor(init?: {
29
+ deviceCode?: string;
30
+ userCode?: string;
31
+ verificationUri?: string;
32
+ verificationUriComplete?: string;
33
+ expiresIn?: number;
34
+ interval?: number;
35
+ });
36
+ effectiveIntervalSeconds(): number;
37
+ static fromJSON(data: OAuth2DeviceAuthorizationJson | null | undefined): OAuth2DeviceAuthorization | null;
38
+ /** Prints no deviceCode. */
39
+ toString(): string;
40
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Categorized errors from OAuth 2.0 authorization flows.
3
+ *
4
+ * Authorization server error details are retained in the error message.
5
+ */
6
+ /** Error category. Each one implies a different action. */
7
+ export declare enum OAuth2Category {
8
+ /** Cannot fetch authorization server metadata: wrong address, service down, no network */
9
+ MetadataUnavailable = "METADATA_UNAVAILABLE",
10
+ /** No usable client_id, and automatic registration is not possible */
11
+ ClientRegistrationRequired = "CLIENT_REGISTRATION_REQUIRED",
12
+ /** Callback stage failed: timeout, user denial, or state mismatch */
13
+ CallbackFailed = "CALLBACK_FAILED",
14
+ /**
15
+ * Device flow not approved in time, or explicitly denied. Kept separate from
16
+ * ReauthorizationRequired because only a new device authorization is needed -- the
17
+ * stored token was not touched.
18
+ */
19
+ DeviceAuthorizationFailed = "DEVICE_AUTHORIZATION_FAILED",
20
+ /** refresh_token expired, revoked, or never obtained. Retrying is pointless */
21
+ ReauthorizationRequired = "REAUTHORIZATION_REQUIRED",
22
+ /** Token endpoint call failed, and not in the terminal way above */
23
+ TokenRequestFailed = "TOKEN_REQUEST_FAILED",
24
+ /** Reading or writing the local token file failed */
25
+ StorageFailed = "STORAGE_FAILED"
26
+ }
27
+ /** Thrown by any stage of the OAuth2 flow. */
28
+ export declare class OAuth2Error extends Error {
29
+ readonly category: OAuth2Category;
30
+ readonly cause?: unknown;
31
+ constructor(category: OAuth2Category, message: string, cause?: unknown);
32
+ get reauthorizationRequired(): boolean;
33
+ toString(): string;
34
+ }
35
+ /**
36
+ * A grant the authorization server refused.
37
+ *
38
+ * Carries the error code so a caller can tell a rejected credential from a rejected
39
+ * client registration. Its category is `ReauthorizationRequired`, so callers that only
40
+ * know the public type behave as they would for any terminal authorization failure.
41
+ */
42
+ export declare class RejectedGrantError extends OAuth2Error {
43
+ readonly error: string;
44
+ constructor(error: string, description: string);
45
+ get isClientRejected(): boolean;
46
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Every call here is a small JSON exchange with the authorization server, so a request
3
+ * that has not answered in this long is not going to. Same values as the Java and
4
+ * Python SDKs.
5
+ */
6
+ export declare const CONNECT_TIMEOUT_SECONDS = 5;
7
+ export declare const READ_TIMEOUT_SECONDS = 10;
8
+ /** Form fields whose value is null or undefined are skipped. */
9
+ export type FormFields = Record<string, string | undefined | null>;
10
+ export declare function getJson(url: string): Promise<Record<string, unknown>>;
11
+ export declare function postJson(url: string, payload: unknown): Promise<Record<string, unknown>>;
12
+ export declare function postForm(url: string, form: FormFields): Promise<Record<string, unknown>>;
13
+ /**
14
+ * Post a form and return the HTTP status without requiring a response body.
15
+ *
16
+ * RFC 7009 revocation returns an empty success body, so no parsing is performed.
17
+ *
18
+ * @returns the HTTP status code
19
+ * @throws OAuth2Error on connection failure
20
+ */
21
+ export declare function postFormForStatus(url: string, form: FormFields): Promise<number>;
@@ -0,0 +1,16 @@
1
+ /** OAuth 2.0 authentication, authorization flows, and token persistence. */
2
+ export { OAuth2ClientRegistration } from './client-registration';
3
+ export type { OAuth2ClientRegistrationJson } from './client-registration';
4
+ export { OAuth2DeviceAuthorization, DEFAULT_DEVICE_INTERVAL_SECONDS } from './device-authorization';
5
+ export type { OAuth2DeviceAuthorizationJson } from './device-authorization';
6
+ export { OAuth2Category, OAuth2Error, RejectedGrantError } from './errors';
7
+ export { LoopbackReceiver, CALLBACK_PATH } from './loopback-receiver';
8
+ export { OAuth2Metadata } from './metadata';
9
+ export { OAuth2Authentication, BEARER_PREFIX } from './oauth2-authentication';
10
+ export { OAuth2SessionManager, BUILTIN_ISSUER, CLIENT_NAME, DEFAULT_AUTHORIZE_TIMEOUT_SECONDS, DEFAULT_MANUAL_CALLBACK_PORT, DEFAULT_REFRESH_AHEAD_MILLIS, ENV_ISSUER, MIN_USABLE_REMAINING_MILLIS, } from './session-manager';
11
+ export type { AuthorizationUrlHandler, DeviceAuthorizationHandler, OAuth2SessionManagerOptions, } from './session-manager';
12
+ export { OAuth2Token, TOKEN_SCHEMA_VERSION, currentMillis } from './token';
13
+ export type { OAuth2TokenJson } from './token';
14
+ export { OAuth2TokenStore, DEFAULT_SURFACE, ENV_HOME, hashIssuer } from './token-store';
15
+ export type { TokenStore } from './token-store';
16
+ export { CONNECT_TIMEOUT_SECONDS, READ_TIMEOUT_SECONDS } from './http-utils';
@@ -0,0 +1,29 @@
1
+ export declare const CALLBACK_PATH = "/callback";
2
+ /** Single-shot local callback server. */
3
+ export declare class LoopbackReceiver {
4
+ private readonly server;
5
+ private port;
6
+ private params;
7
+ private fired;
8
+ private waiters;
9
+ private constructor();
10
+ /**
11
+ * Bind the receiver. It must be listening before the authorization request is issued.
12
+ *
13
+ * @param port port to bind; 0 lets the system assign a free one
14
+ */
15
+ static start(port?: number): Promise<LoopbackReceiver>;
16
+ get redirectUri(): string;
17
+ get boundPort(): number;
18
+ /**
19
+ * Wait for the browser callback.
20
+ *
21
+ * @returns the query parameters carried by the callback
22
+ * @throws OAuth2Error on timeout
23
+ */
24
+ waitForCallback(timeoutSeconds: number): Promise<Record<string, string>>;
25
+ close(): void;
26
+ /** Record the callback params (first one wins) and return a snapshot of them. */
27
+ private record;
28
+ private release;
29
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Authorization server metadata (RFC 8414).
3
+ *
4
+ * Endpoints always come from metadata, never hardcoded, so switching environments only
5
+ * means changing the issuer.
6
+ */
7
+ /** Raw shape of `{issuer}/.well-known/oauth-authorization-server`. */
8
+ export interface OAuth2MetadataJson {
9
+ issuer?: string;
10
+ authorization_endpoint?: string;
11
+ token_endpoint?: string;
12
+ registration_endpoint?: string;
13
+ device_authorization_endpoint?: string;
14
+ revocation_endpoint?: string;
15
+ scopes_supported?: string[];
16
+ }
17
+ export declare class OAuth2Metadata {
18
+ issuer?: string;
19
+ authorizationEndpoint?: string;
20
+ tokenEndpoint?: string;
21
+ /** Absent means only a manually configured clientId can be used */
22
+ registrationEndpoint?: string;
23
+ /** Absent means this environment does not support the device flow */
24
+ deviceAuthorizationEndpoint?: string;
25
+ /** RFC 7009 revocation. Absent means logout can only clear the local token */
26
+ revocationEndpoint?: string;
27
+ scopesSupported: string[];
28
+ constructor(init?: Partial<Omit<OAuth2Metadata, 'scopesSupported'>> & {
29
+ scopesSupported?: string[];
30
+ });
31
+ static fromJSON(data: OAuth2MetadataJson | null | undefined): OAuth2Metadata | null;
32
+ }
@@ -0,0 +1,31 @@
1
+ /** Request authentication backed by an OAuth 2.0 session manager. */
2
+ import { AuthenticationAttempt, AuthenticationType, RequestAuthContext, RetryDecision, type Authentication } from '../authentication';
3
+ import type { OAuth2SessionManager } from './session-manager';
4
+ export declare const BEARER_PREFIX = "Bearer ";
5
+ /** Authenticate with an OAuth2 access token. */
6
+ export declare class OAuth2Authentication implements Authentication {
7
+ private readonly sessions;
8
+ constructor(sessionManager: OAuth2SessionManager);
9
+ get authType(): AuthenticationType;
10
+ get sessionManager(): OAuth2SessionManager;
11
+ /**
12
+ * Attach a Bearer token to the request context.
13
+ *
14
+ * Near-expiry refreshes use the session manager's single-flight synchronization.
15
+ * Missing authorization rejects with `ReauthorizationRequired` without starting an
16
+ * implicit browser flow.
17
+ */
18
+ apply(context: RequestAuthContext): Promise<AuthenticationAttempt>;
19
+ /**
20
+ * Process HTTP 401 responses.
21
+ *
22
+ * HTTP 403 indicates insufficient permission and cannot be resolved by refreshing a
23
+ * token with the same scope. Server errors and timeouts are not credential failures.
24
+ */
25
+ onUnauthorized(attempt: AuthenticationAttempt | undefined, statusCode: number): Promise<RetryDecision>;
26
+ /**
27
+ * The session is owned by the application and may still be in use, so it is not
28
+ * closed here.
29
+ */
30
+ close(): void;
31
+ }