@casfa/client 0.0.1

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.
@@ -0,0 +1,338 @@
1
+ import { DepotListItem, DepotCommit, CreateDepot, CreateDepotResponse, DepotDetail, ListDepotsQuery, UpdateDepot, ServiceInfo, NodeMetadata, PrepareNodes, PrepareNodesResponse, TokenExchange, Login, Refresh, ApproveRequest, ApproveRequestResponse, CreateAuthRequest, CreateAuthRequestResponse, PollRequestResponse, DenyRequest, DenyRequestResponse, TicketListItem, CreateTicket, CreateTicketResponse, TicketDetail, ListTicketsQuery, TicketSubmit, TokenListItem, CreateToken, CreateTokenResponse, TokenDetail, RevokeTokenResponse } from '@casfa/protocol';
2
+ import { FetchResult, StoredUserToken } from './types/index.js';
3
+
4
+ /**
5
+ * Depot API functions.
6
+ *
7
+ * Token Requirement:
8
+ * - All depot operations require Access Token with canManageDepot permission.
9
+ */
10
+
11
+ type ListDepotsResponse = {
12
+ depots: DepotListItem[];
13
+ nextCursor?: string;
14
+ };
15
+ type CommitDepotResponse = {
16
+ depotId: string;
17
+ root: string;
18
+ updatedAt: number;
19
+ };
20
+ /**
21
+ * Create a new depot.
22
+ * Requires Access Token with canManageDepot.
23
+ */
24
+ declare const createDepot: (baseUrl: string, realm: string, accessTokenBase64: string, params: CreateDepot) => Promise<FetchResult<CreateDepotResponse>>;
25
+ /**
26
+ * List depots.
27
+ * Requires Access Token.
28
+ */
29
+ declare const listDepots: (baseUrl: string, realm: string, accessTokenBase64: string, params?: ListDepotsQuery) => Promise<FetchResult<ListDepotsResponse>>;
30
+ /**
31
+ * Get depot details.
32
+ * Requires Access Token.
33
+ */
34
+ declare const getDepot: (baseUrl: string, realm: string, accessTokenBase64: string, depotId: string) => Promise<FetchResult<DepotDetail>>;
35
+ /**
36
+ * Update depot metadata.
37
+ * Requires Access Token with canManageDepot.
38
+ */
39
+ declare const updateDepot: (baseUrl: string, realm: string, accessTokenBase64: string, depotId: string, params: UpdateDepot) => Promise<FetchResult<DepotDetail>>;
40
+ /**
41
+ * Delete a depot.
42
+ * Requires Access Token with canManageDepot.
43
+ */
44
+ declare const deleteDepot: (baseUrl: string, realm: string, accessTokenBase64: string, depotId: string) => Promise<FetchResult<void>>;
45
+ /**
46
+ * Commit new root to depot.
47
+ * Requires Access Token with canManageDepot.
48
+ */
49
+ declare const commitDepot: (baseUrl: string, realm: string, accessTokenBase64: string, depotId: string, params: DepotCommit) => Promise<FetchResult<CommitDepotResponse>>;
50
+
51
+ /**
52
+ * Service info API.
53
+ */
54
+
55
+ /**
56
+ * Fetch service info from /api/info.
57
+ */
58
+ declare const fetchServiceInfo: (baseUrl: string) => Promise<FetchResult<ServiceInfo>>;
59
+ /**
60
+ * Health check.
61
+ */
62
+ declare const healthCheck: (baseUrl: string) => Promise<FetchResult<{
63
+ status: string;
64
+ }>>;
65
+
66
+ /**
67
+ * Node API functions.
68
+ *
69
+ * Token Requirement:
70
+ * - GET /nodes/:nodeKey: Access Token (with X-CAS-Index-Path header)
71
+ * - POST /nodes/prepare: Access Token with canUpload
72
+ * - PUT /nodes/:nodeKey: Access Token with canUpload
73
+ */
74
+
75
+ type NodeUploadResult = {
76
+ nodeKey: string;
77
+ status: "created" | "exists";
78
+ };
79
+ /**
80
+ * Get node content.
81
+ * Requires Access Token with scope covering the index path.
82
+ *
83
+ * @param indexPath - The CAS index path for scope verification (e.g., "depot:MAIN:0:1")
84
+ */
85
+ declare const getNode: (baseUrl: string, realm: string, accessTokenBase64: string, nodeKey: string, indexPath: string) => Promise<FetchResult<Uint8Array>>;
86
+ /**
87
+ * Get node metadata.
88
+ * Requires Access Token with scope covering the index path.
89
+ */
90
+ declare const getNodeMetadata: (baseUrl: string, realm: string, accessTokenBase64: string, nodeKey: string, indexPath: string) => Promise<FetchResult<NodeMetadata>>;
91
+ /**
92
+ * Prepare nodes for upload.
93
+ * Returns which nodes need to be uploaded vs already exist.
94
+ * Requires Access Token with canUpload.
95
+ */
96
+ declare const prepareNodes: (baseUrl: string, realm: string, accessTokenBase64: string, params: PrepareNodes) => Promise<FetchResult<PrepareNodesResponse>>;
97
+ /**
98
+ * Upload a node.
99
+ * Requires Access Token with canUpload.
100
+ */
101
+ declare const putNode: (baseUrl: string, realm: string, accessTokenBase64: string, nodeKey: string, content: Uint8Array) => Promise<FetchResult<NodeUploadResult>>;
102
+
103
+ /**
104
+ * OAuth API functions.
105
+ */
106
+
107
+ type CognitoConfig = {
108
+ region: string;
109
+ userPoolId: string;
110
+ clientId: string;
111
+ domain: string;
112
+ };
113
+ type TokenResponse = {
114
+ accessToken: string;
115
+ refreshToken: string;
116
+ expiresIn: number;
117
+ idToken?: string;
118
+ };
119
+ type UserInfo = {
120
+ userId: string;
121
+ email: string;
122
+ role: string;
123
+ };
124
+ /**
125
+ * Get Cognito configuration.
126
+ */
127
+ declare const getOAuthConfig: (baseUrl: string) => Promise<FetchResult<CognitoConfig>>;
128
+ /**
129
+ * Exchange authorization code for tokens.
130
+ */
131
+ declare const exchangeCode: (baseUrl: string, params: TokenExchange) => Promise<FetchResult<TokenResponse>>;
132
+ /**
133
+ * Login with email and password.
134
+ */
135
+ declare const login: (baseUrl: string, params: Login) => Promise<FetchResult<TokenResponse>>;
136
+ /**
137
+ * Refresh access token.
138
+ */
139
+ declare const refresh: (baseUrl: string, params: Refresh) => Promise<FetchResult<TokenResponse>>;
140
+ /**
141
+ * Get current user info.
142
+ * Requires User JWT.
143
+ */
144
+ declare const getMe: (baseUrl: string, userAccessToken: string) => Promise<FetchResult<UserInfo>>;
145
+ /**
146
+ * Convert token response to stored user token.
147
+ */
148
+ declare const tokenResponseToStoredUserToken: (response: TokenResponse, userId: string) => StoredUserToken;
149
+
150
+ /**
151
+ * Client Authorization Request API functions.
152
+ *
153
+ * For CLI/desktop apps to request tokens through user approval.
154
+ */
155
+
156
+ /**
157
+ * Create an authorization request.
158
+ * No auth required - called by CLI/desktop clients.
159
+ */
160
+ declare const createAuthRequest: (baseUrl: string, params: CreateAuthRequest) => Promise<FetchResult<CreateAuthRequestResponse>>;
161
+ /**
162
+ * Poll authorization request status.
163
+ * No auth required - called by CLI/desktop clients.
164
+ */
165
+ declare const pollAuthRequest: (baseUrl: string, requestId: string) => Promise<FetchResult<PollRequestResponse>>;
166
+ /**
167
+ * Get authorization request details.
168
+ * Requires User JWT.
169
+ */
170
+ declare const getAuthRequest: (baseUrl: string, userAccessToken: string, requestId: string) => Promise<FetchResult<PollRequestResponse>>;
171
+ /**
172
+ * Approve an authorization request.
173
+ * Requires User JWT.
174
+ */
175
+ declare const approveAuthRequest: (baseUrl: string, userAccessToken: string, requestId: string, params?: ApproveRequest) => Promise<FetchResult<ApproveRequestResponse>>;
176
+ /**
177
+ * Reject an authorization request.
178
+ * Requires User JWT.
179
+ */
180
+ declare const rejectAuthRequest: (baseUrl: string, userAccessToken: string, requestId: string, params?: DenyRequest) => Promise<FetchResult<DenyRequestResponse>>;
181
+
182
+ /**
183
+ * Ticket API functions.
184
+ *
185
+ * Token Requirement:
186
+ * - POST /api/realm/{realmId}/tickets: Access Token (create ticket, bind pre-issued token)
187
+ * - GET /api/realm/{realmId}/tickets: Access Token (list)
188
+ * - GET /api/realm/{realmId}/tickets/:ticketId: Access Token (get detail)
189
+ * - POST /api/realm/{realmId}/tickets/:ticketId/submit: Access Token (submit)
190
+ *
191
+ * Design Principle: All Realm data operations use Access Token.
192
+ * Delegate Token is only for issuing tokens.
193
+ *
194
+ * Two-step Ticket creation flow:
195
+ * 1. Issue Access Token using Delegate Token (POST /api/tokens/delegate)
196
+ * 2. Create Ticket and bind the token (POST /api/realm/{realmId}/tickets)
197
+ */
198
+
199
+ type ListTicketsResponse = {
200
+ tickets: TicketListItem[];
201
+ nextCursor?: string;
202
+ };
203
+ type SubmitTicketResponse = {
204
+ ticketId: string;
205
+ status: "submitted";
206
+ root: string;
207
+ submittedAt: number;
208
+ };
209
+ /**
210
+ * Create a new ticket and bind a pre-issued Access Token.
211
+ * Requires Access Token.
212
+ *
213
+ * @param accessTokenBase64 - Caller's Access Token (for authentication)
214
+ * @param params.accessTokenId - Pre-issued Access Token ID to bind (for Tool use)
215
+ */
216
+ declare const createTicket: (baseUrl: string, realm: string, accessTokenBase64: string, params: CreateTicket) => Promise<FetchResult<CreateTicketResponse>>;
217
+ /**
218
+ * List tickets.
219
+ * Requires Access Token.
220
+ */
221
+ declare const listTickets: (baseUrl: string, realm: string, accessTokenBase64: string, params?: ListTicketsQuery) => Promise<FetchResult<ListTicketsResponse>>;
222
+ /**
223
+ * Get ticket details.
224
+ * Requires Access Token.
225
+ */
226
+ declare const getTicket: (baseUrl: string, realm: string, accessTokenBase64: string, ticketId: string) => Promise<FetchResult<TicketDetail>>;
227
+ /**
228
+ * Submit a ticket.
229
+ * Requires Access Token.
230
+ */
231
+ declare const submitTicket: (baseUrl: string, realm: string, accessTokenBase64: string, ticketId: string, params: TicketSubmit) => Promise<FetchResult<SubmitTicketResponse>>;
232
+
233
+ /**
234
+ * Token management API functions.
235
+ *
236
+ * Token Requirement:
237
+ * - POST /api/tokens: User JWT
238
+ * - GET /api/tokens: User JWT
239
+ * - GET /api/tokens/:tokenId: User JWT
240
+ * - POST /api/tokens/:tokenId/revoke: User JWT
241
+ * - POST /api/tokens/delegate: Delegate Token
242
+ */
243
+
244
+ type ListTokensParams = {
245
+ limit?: number;
246
+ cursor?: string;
247
+ type?: "delegate" | "access";
248
+ };
249
+ type ListTokensResponse = {
250
+ tokens: TokenListItem[];
251
+ nextCursor?: string;
252
+ };
253
+ /**
254
+ * Create a new Delegate Token.
255
+ * Requires User JWT.
256
+ */
257
+ declare const createToken: (baseUrl: string, userAccessToken: string, params: CreateToken) => Promise<FetchResult<CreateTokenResponse>>;
258
+ /**
259
+ * List Delegate Tokens.
260
+ * Requires User JWT.
261
+ */
262
+ declare const listTokens: (baseUrl: string, userAccessToken: string, params?: ListTokensParams) => Promise<FetchResult<ListTokensResponse>>;
263
+ /**
264
+ * Get token details.
265
+ * Requires User JWT.
266
+ */
267
+ declare const getToken: (baseUrl: string, userAccessToken: string, tokenId: string) => Promise<FetchResult<TokenDetail>>;
268
+ /**
269
+ * Revoke a token.
270
+ * Requires User JWT.
271
+ */
272
+ declare const revokeToken: (baseUrl: string, userAccessToken: string, tokenId: string) => Promise<FetchResult<RevokeTokenResponse>>;
273
+ type DelegateTokenParams = {
274
+ name: string;
275
+ type: "delegate" | "access";
276
+ expiresIn?: number;
277
+ canUpload?: boolean;
278
+ canManageDepot?: boolean;
279
+ scope?: string[];
280
+ };
281
+ /**
282
+ * Delegate (re-issue) a token using existing Delegate Token.
283
+ * Requires Delegate Token.
284
+ */
285
+ declare const delegateToken: (baseUrl: string, delegateTokenBase64: string, params: DelegateTokenParams) => Promise<FetchResult<CreateTokenResponse>>;
286
+
287
+ /**
288
+ * API module exports for @casfa/client
289
+ */
290
+
291
+ type index_CognitoConfig = CognitoConfig;
292
+ type index_CommitDepotResponse = CommitDepotResponse;
293
+ type index_DelegateTokenParams = DelegateTokenParams;
294
+ type index_ListDepotsResponse = ListDepotsResponse;
295
+ type index_ListTicketsResponse = ListTicketsResponse;
296
+ type index_ListTokensParams = ListTokensParams;
297
+ type index_ListTokensResponse = ListTokensResponse;
298
+ type index_NodeUploadResult = NodeUploadResult;
299
+ type index_SubmitTicketResponse = SubmitTicketResponse;
300
+ type index_TokenResponse = TokenResponse;
301
+ type index_UserInfo = UserInfo;
302
+ declare const index_approveAuthRequest: typeof approveAuthRequest;
303
+ declare const index_commitDepot: typeof commitDepot;
304
+ declare const index_createAuthRequest: typeof createAuthRequest;
305
+ declare const index_createDepot: typeof createDepot;
306
+ declare const index_createTicket: typeof createTicket;
307
+ declare const index_createToken: typeof createToken;
308
+ declare const index_delegateToken: typeof delegateToken;
309
+ declare const index_deleteDepot: typeof deleteDepot;
310
+ declare const index_exchangeCode: typeof exchangeCode;
311
+ declare const index_fetchServiceInfo: typeof fetchServiceInfo;
312
+ declare const index_getAuthRequest: typeof getAuthRequest;
313
+ declare const index_getDepot: typeof getDepot;
314
+ declare const index_getMe: typeof getMe;
315
+ declare const index_getNode: typeof getNode;
316
+ declare const index_getNodeMetadata: typeof getNodeMetadata;
317
+ declare const index_getOAuthConfig: typeof getOAuthConfig;
318
+ declare const index_getTicket: typeof getTicket;
319
+ declare const index_getToken: typeof getToken;
320
+ declare const index_healthCheck: typeof healthCheck;
321
+ declare const index_listDepots: typeof listDepots;
322
+ declare const index_listTickets: typeof listTickets;
323
+ declare const index_listTokens: typeof listTokens;
324
+ declare const index_login: typeof login;
325
+ declare const index_pollAuthRequest: typeof pollAuthRequest;
326
+ declare const index_prepareNodes: typeof prepareNodes;
327
+ declare const index_putNode: typeof putNode;
328
+ declare const index_refresh: typeof refresh;
329
+ declare const index_rejectAuthRequest: typeof rejectAuthRequest;
330
+ declare const index_revokeToken: typeof revokeToken;
331
+ declare const index_submitTicket: typeof submitTicket;
332
+ declare const index_tokenResponseToStoredUserToken: typeof tokenResponseToStoredUserToken;
333
+ declare const index_updateDepot: typeof updateDepot;
334
+ declare namespace index {
335
+ export { type index_CognitoConfig as CognitoConfig, type index_CommitDepotResponse as CommitDepotResponse, type index_DelegateTokenParams as DelegateTokenParams, type index_ListDepotsResponse as ListDepotsResponse, type index_ListTicketsResponse as ListTicketsResponse, type index_ListTokensParams as ListTokensParams, type index_ListTokensResponse as ListTokensResponse, type index_NodeUploadResult as NodeUploadResult, type index_SubmitTicketResponse as SubmitTicketResponse, type index_TokenResponse as TokenResponse, type index_UserInfo as UserInfo, index_approveAuthRequest as approveAuthRequest, index_commitDepot as commitDepot, index_createAuthRequest as createAuthRequest, index_createDepot as createDepot, index_createTicket as createTicket, index_createToken as createToken, index_delegateToken as delegateToken, index_deleteDepot as deleteDepot, index_exchangeCode as exchangeCode, index_fetchServiceInfo as fetchServiceInfo, index_getAuthRequest as getAuthRequest, index_getDepot as getDepot, index_getMe as getMe, index_getNode as getNode, index_getNodeMetadata as getNodeMetadata, index_getOAuthConfig as getOAuthConfig, index_getTicket as getTicket, index_getToken as getToken, index_healthCheck as healthCheck, index_listDepots as listDepots, index_listTickets as listTickets, index_listTokens as listTokens, index_login as login, index_pollAuthRequest as pollAuthRequest, index_prepareNodes as prepareNodes, index_putNode as putNode, index_refresh as refresh, index_rejectAuthRequest as rejectAuthRequest, index_revokeToken as revokeToken, index_submitTicket as submitTicket, index_tokenResponseToStoredUserToken as tokenResponseToStoredUserToken, index_updateDepot as updateDepot };
336
+ }
337
+
338
+ export { listTokens as A, login as B, type CommitDepotResponse as C, type DelegateTokenParams as D, pollAuthRequest as E, prepareNodes as F, putNode as G, refresh as H, rejectAuthRequest as I, revokeToken as J, submitTicket as K, type ListDepotsResponse as L, tokenResponseToStoredUserToken as M, type NodeUploadResult as N, updateDepot as O, type SubmitTicketResponse as S, type TokenResponse as T, type UserInfo as U, type CognitoConfig as a, type ListTicketsResponse as b, type ListTokensParams as c, type ListTokensResponse as d, approveAuthRequest as e, commitDepot as f, createAuthRequest as g, createDepot as h, index as i, createTicket as j, createToken as k, delegateToken as l, deleteDepot as m, exchangeCode as n, fetchServiceInfo as o, getAuthRequest as p, getDepot as q, getMe as r, getNode as s, getNodeMetadata as t, getOAuthConfig as u, getTicket as v, getToken as w, healthCheck as x, listDepots as y, listTickets as z };
@@ -0,0 +1,298 @@
1
+ import { ServiceInfo, CreateDepot, CreateDepotResponse, ListDepotsQuery, DepotDetail, UpdateDepot, DepotCommit, NodeMetadata, PrepareNodes, PrepareNodesResponse, CreateTicket, CreateTicketResponse, ListTicketsQuery, TicketDetail, TicketSubmit, CreateToken } from '@casfa/protocol';
2
+ import { TokenState, StoredUserToken, StoredDelegateToken, StoredAccessToken, TokenStorageProvider, OnTokenChangeCallback, OnAuthRequiredCallback, FetchResult, ClientConfig } from './types/index.js';
3
+ export { ClientError, emptyTokenState } from './types/index.js';
4
+ import { L as ListDepotsResponse, C as CommitDepotResponse, N as NodeUploadResult, a as CognitoConfig, U as UserInfo, b as ListTicketsResponse, S as SubmitTicketResponse, c as ListTokensParams, d as ListTokensResponse, D as DelegateTokenParams } from './index-cPO-6GxE.js';
5
+ export { i as api } from './index-cPO-6GxE.js';
6
+
7
+ /**
8
+ * Token store - manages the three-tier token state.
9
+ *
10
+ * Provides a closure-based store for token state management with
11
+ * automatic persistence and change notifications.
12
+ */
13
+
14
+ type TokenStore = {
15
+ /** Get current token state (immutable snapshot) */
16
+ getState: () => TokenState;
17
+ /** Set user JWT token */
18
+ setUser: (token: StoredUserToken | null) => void;
19
+ /** Set delegate token */
20
+ setDelegate: (token: StoredDelegateToken | null) => void;
21
+ /** Set access token */
22
+ setAccess: (token: StoredAccessToken | null) => void;
23
+ /** Clear all tokens */
24
+ clear: () => void;
25
+ /** Initialize from storage provider */
26
+ initialize: () => Promise<void>;
27
+ };
28
+ type TokenStoreConfig = {
29
+ storage?: TokenStorageProvider;
30
+ onTokenChange?: OnTokenChangeCallback;
31
+ onAuthRequired?: OnAuthRequiredCallback;
32
+ };
33
+ /**
34
+ * Create a token store instance.
35
+ */
36
+ declare const createTokenStore: (config?: TokenStoreConfig) => TokenStore;
37
+
38
+ /**
39
+ * Token selector and auto-issuer.
40
+ *
41
+ * Implements the "maximum authority" principle:
42
+ * - When issuing tokens, prefer User JWT (depth=0) over Delegate Token
43
+ * - Check issuer consistency before using existing tokens
44
+ * - Re-issue tokens when issuer is not maximized
45
+ */
46
+
47
+ type TokenSelectorConfig = {
48
+ store: TokenStore;
49
+ baseUrl: string;
50
+ realm: string;
51
+ serverInfo: ServiceInfo | null;
52
+ defaultTokenTtl?: number;
53
+ };
54
+ type TokenSelector = {
55
+ /**
56
+ * Get or issue an Access Token.
57
+ * - If valid Access Token exists and is from max issuer, return it
58
+ * - Otherwise, issue a new one using User JWT or Delegate Token
59
+ */
60
+ ensureAccessToken: () => Promise<StoredAccessToken | null>;
61
+ /**
62
+ * Get or issue a Delegate Token.
63
+ * - If valid Delegate Token exists, return it
64
+ * - If User JWT exists, issue a new one
65
+ */
66
+ ensureDelegateToken: () => Promise<StoredDelegateToken | null>;
67
+ };
68
+ /**
69
+ * Create a token selector instance.
70
+ */
71
+ declare const createTokenSelector: (config: TokenSelectorConfig) => TokenSelector;
72
+
73
+ /**
74
+ * Depot methods for the stateful client.
75
+ */
76
+
77
+ type DepotMethods = {
78
+ /** Create a new depot */
79
+ create: (params: CreateDepot) => Promise<FetchResult<CreateDepotResponse>>;
80
+ /** List depots */
81
+ list: (params?: ListDepotsQuery) => Promise<FetchResult<ListDepotsResponse>>;
82
+ /** Get depot details */
83
+ get: (depotId: string) => Promise<FetchResult<DepotDetail>>;
84
+ /** Update depot */
85
+ update: (depotId: string, params: UpdateDepot) => Promise<FetchResult<DepotDetail>>;
86
+ /** Delete depot */
87
+ delete: (depotId: string) => Promise<FetchResult<void>>;
88
+ /** Commit new root */
89
+ commit: (depotId: string, params: DepotCommit) => Promise<FetchResult<CommitDepotResponse>>;
90
+ };
91
+
92
+ /**
93
+ * Node methods for the stateful client.
94
+ */
95
+
96
+ type NodeMethods = {
97
+ /** Get node content */
98
+ get: (nodeKey: string, indexPath: string) => Promise<FetchResult<Uint8Array>>;
99
+ /** Get node metadata */
100
+ getMetadata: (nodeKey: string, indexPath: string) => Promise<FetchResult<NodeMetadata>>;
101
+ /** Prepare nodes for upload */
102
+ prepare: (params: PrepareNodes) => Promise<FetchResult<PrepareNodesResponse>>;
103
+ /** Upload a node */
104
+ put: (nodeKey: string, content: Uint8Array) => Promise<FetchResult<NodeUploadResult>>;
105
+ };
106
+
107
+ /**
108
+ * JWT refresh management with promise deduplication.
109
+ */
110
+
111
+ type RefreshManager = {
112
+ /**
113
+ * Ensure user token is valid, refreshing if needed.
114
+ * Returns the valid user token or null if refresh failed.
115
+ */
116
+ ensureValidUserToken: () => Promise<StoredUserToken | null>;
117
+ /**
118
+ * Schedule proactive refresh before expiration.
119
+ */
120
+ scheduleProactiveRefresh: () => void;
121
+ /**
122
+ * Cancel any scheduled refresh.
123
+ */
124
+ cancelScheduledRefresh: () => void;
125
+ };
126
+ type RefreshManagerConfig = {
127
+ store: TokenStore;
128
+ baseUrl: string;
129
+ onAuthRequired?: OnAuthRequiredCallback;
130
+ };
131
+ /**
132
+ * Create a refresh manager for JWT token refresh.
133
+ */
134
+ declare const createRefreshManager: (config: RefreshManagerConfig) => RefreshManager;
135
+
136
+ /**
137
+ * OAuth methods for the stateful client.
138
+ */
139
+
140
+ type OAuthMethods = {
141
+ /** Get Cognito configuration */
142
+ getConfig: () => Promise<FetchResult<CognitoConfig>>;
143
+ /** Login with email and password */
144
+ login: (email: string, password: string) => Promise<FetchResult<UserInfo>>;
145
+ /** Exchange authorization code for tokens */
146
+ exchangeCode: (code: string, redirectUri: string, codeVerifier?: string) => Promise<FetchResult<UserInfo>>;
147
+ /** Get current user info */
148
+ getMe: () => Promise<FetchResult<UserInfo>>;
149
+ };
150
+
151
+ /**
152
+ * Ticket methods for the stateful client.
153
+ *
154
+ * Design Principle: All Realm data operations use Access Token.
155
+ * Delegate Token is only for issuing tokens.
156
+ *
157
+ * Two-step Ticket creation flow:
158
+ * 1. Issue Access Token using tokens.delegate() (requires Delegate Token)
159
+ * 2. Create Ticket using tickets.create() (requires Access Token)
160
+ */
161
+
162
+ type TicketMethods = {
163
+ /**
164
+ * Create a new ticket and bind a pre-issued Access Token.
165
+ * Requires Access Token.
166
+ *
167
+ * Note: The Access Token to bind must be issued first using tokens.delegate().
168
+ *
169
+ * @param params.accessTokenId - Pre-issued Access Token ID to bind (for Tool use)
170
+ */
171
+ create: (params: CreateTicket) => Promise<FetchResult<CreateTicketResponse>>;
172
+ /** List tickets */
173
+ list: (params?: ListTicketsQuery) => Promise<FetchResult<ListTicketsResponse>>;
174
+ /** Get ticket details */
175
+ get: (ticketId: string) => Promise<FetchResult<TicketDetail>>;
176
+ /** Submit ticket */
177
+ submit: (ticketId: string, params: TicketSubmit) => Promise<FetchResult<SubmitTicketResponse>>;
178
+ };
179
+
180
+ /**
181
+ * Token management methods for the stateful client.
182
+ */
183
+
184
+ type TokenMethods = {
185
+ /** Create a new token (User JWT required) */
186
+ create: (params: CreateToken) => Promise<FetchResult<StoredDelegateToken | StoredAccessToken>>;
187
+ /** List tokens (User JWT required) */
188
+ list: (params?: ListTokensParams) => Promise<FetchResult<ListTokensResponse>>;
189
+ /** Revoke a token (User JWT required) */
190
+ revoke: (tokenId: string) => Promise<FetchResult<void>>;
191
+ /** Delegate a token using current Delegate Token */
192
+ delegate: (params: DelegateTokenParams) => Promise<FetchResult<StoredDelegateToken | StoredAccessToken>>;
193
+ };
194
+
195
+ /**
196
+ * Stateful CASFA Client
197
+ *
198
+ * A closure-based client that manages three-tier token hierarchy:
199
+ * - User JWT: OAuth login token, highest authority
200
+ * - Delegate Token: Re-delegation token, can issue child tokens
201
+ * - Access Token: Data access token, used for CAS operations
202
+ */
203
+
204
+ /**
205
+ * The stateful CASFA client.
206
+ */
207
+ type CasfaClient = {
208
+ /** Get current token state */
209
+ getState: () => TokenState;
210
+ /** Get server info */
211
+ getServerInfo: () => ServiceInfo | null;
212
+ /** Set delegate token (e.g., from external source) */
213
+ setDelegateToken: (token: StoredDelegateToken) => void;
214
+ /** Set access token (e.g., from external source) */
215
+ setAccessToken: (token: StoredAccessToken) => void;
216
+ /** Clear all tokens and logout */
217
+ logout: () => void;
218
+ /** OAuth methods */
219
+ oauth: OAuthMethods;
220
+ /** Token management methods */
221
+ tokens: TokenMethods;
222
+ /** Ticket methods */
223
+ tickets: TicketMethods;
224
+ /** Depot methods */
225
+ depots: DepotMethods;
226
+ /** Node methods */
227
+ nodes: NodeMethods;
228
+ };
229
+ /**
230
+ * Create a stateful CASFA client.
231
+ */
232
+ declare const createClient: (config: ClientConfig) => Promise<CasfaClient>;
233
+
234
+ /**
235
+ * Token validity and issuer consistency checks.
236
+ */
237
+
238
+ /**
239
+ * Default buffer time before expiration (60 seconds).
240
+ */
241
+ declare const DEFAULT_EXPIRY_BUFFER_MS = 60000;
242
+ /**
243
+ * Check if a token is valid (not expired with buffer).
244
+ */
245
+ declare const isTokenValid: (token: {
246
+ expiresAt: number;
247
+ } | null, bufferMs?: number) => boolean;
248
+ /**
249
+ * Check if token is expiring soon and should be refreshed proactively.
250
+ * Used for JWT refresh scheduling.
251
+ */
252
+ declare const isTokenExpiringSoon: (token: {
253
+ expiresAt: number;
254
+ } | null, windowMs?: number) => boolean;
255
+ /**
256
+ * Check if user JWT is valid.
257
+ */
258
+ declare const isUserTokenValid: (userToken: StoredUserToken | null, bufferMs?: number) => boolean;
259
+ /**
260
+ * Check if delegate token is valid.
261
+ */
262
+ declare const isDelegateTokenValid: (delegateToken: StoredDelegateToken | null, bufferMs?: number) => boolean;
263
+ /**
264
+ * Check if access token is valid.
265
+ */
266
+ declare const isAccessTokenValid: (accessToken: StoredAccessToken | null, bufferMs?: number) => boolean;
267
+ /**
268
+ * Get the current max issuer ID.
269
+ * Priority: User JWT (userId) > Delegate Token (tokenId)
270
+ */
271
+ declare const getMaxIssuerId: (state: TokenState) => string | null;
272
+ /**
273
+ * Check if access token is issued by the current max issuer.
274
+ * If not, the access token should be re-issued.
275
+ */
276
+ declare const isAccessTokenFromMaxIssuer: (state: TokenState) => boolean;
277
+ /**
278
+ * Check if delegate token is issued by current user.
279
+ * If user JWT exists but delegate token is from a different user,
280
+ * we may want to re-issue the delegate token.
281
+ */
282
+ declare const isDelegateTokenFromCurrentUser: (state: TokenState) => boolean;
283
+ /**
284
+ * Determine if access token needs re-issue.
285
+ * Returns true if:
286
+ * - Access token is invalid/expired
287
+ * - Access token's issuer is not the current max issuer
288
+ */
289
+ declare const shouldReissueAccessToken: (state: TokenState) => boolean;
290
+ /**
291
+ * Determine if delegate token needs re-issue.
292
+ * Returns true if:
293
+ * - Delegate token is invalid/expired
294
+ * - User token exists but delegate token is not from current user
295
+ */
296
+ declare const shouldReissueDelegateToken: (state: TokenState) => boolean;
297
+
298
+ export { type CasfaClient, ClientConfig, DEFAULT_EXPIRY_BUFFER_MS, type DepotMethods, FetchResult, type NodeMethods, type OAuthMethods, OnAuthRequiredCallback, OnTokenChangeCallback, type RefreshManager, StoredAccessToken, StoredDelegateToken, StoredUserToken, type TicketMethods, type TokenMethods, type TokenSelector, TokenState, TokenStorageProvider, type TokenStore, createClient, createRefreshManager, createTokenSelector, createTokenStore, getMaxIssuerId, isAccessTokenFromMaxIssuer, isAccessTokenValid, isDelegateTokenFromCurrentUser, isDelegateTokenValid, isTokenExpiringSoon, isTokenValid, isUserTokenValid, shouldReissueAccessToken, shouldReissueDelegateToken };