@flipstream/sdk 0.4.0 → 0.6.0
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.
- package/CHANGELOG.md +42 -0
- package/README.md +325 -20
- package/dist/auth/discovery.d.ts +2 -0
- package/dist/auth/discovery.js +4 -1
- package/dist/auth/discovery.js.map +1 -1
- package/dist/auth/signIn.d.ts +11 -2
- package/dist/auth/signIn.js +19 -27
- package/dist/auth/signIn.js.map +1 -1
- package/dist/auth/tokenEndpoint.d.ts +18 -0
- package/dist/auth/tokenEndpoint.js +36 -0
- package/dist/auth/tokenEndpoint.js.map +1 -0
- package/dist/core/fetch.d.ts +8 -0
- package/dist/core/fetch.js +36 -11
- package/dist/core/fetch.js.map +1 -1
- package/dist/core/result.d.ts +4 -2
- package/dist/core/result.js +3 -1
- package/dist/core/result.js.map +1 -1
- package/dist/flipstream.d.ts +7 -0
- package/dist/flipstream.js +14 -4
- package/dist/flipstream.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/node/clientSecret.d.ts +41 -0
- package/dist/node/clientSecret.js +124 -0
- package/dist/node/clientSecret.js.map +1 -0
- package/dist/node/fileLock.d.ts +27 -0
- package/dist/node/fileLock.js +267 -0
- package/dist/node/fileLock.js.map +1 -0
- package/dist/node/fileStore.d.ts +19 -0
- package/dist/node/fileStore.js +135 -0
- package/dist/node/fileStore.js.map +1 -0
- package/dist/node/headless.d.ts +7 -2
- package/dist/node/headless.js +15 -2
- package/dist/node/headless.js.map +1 -1
- package/dist/node/index.d.ts +9 -0
- package/dist/node/index.js +9 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/keychainChild.d.ts +1 -0
- package/dist/node/keychainChild.js +100 -0
- package/dist/node/keychainChild.js.map +1 -0
- package/dist/node/keychainStore.d.ts +30 -0
- package/dist/node/keychainStore.js +150 -0
- package/dist/node/keychainStore.js.map +1 -0
- package/dist/node/loopback.d.ts +24 -5
- package/dist/node/loopback.js +80 -23
- package/dist/node/loopback.js.map +1 -1
- package/dist/node/session.d.ts +224 -0
- package/dist/node/session.js +622 -0
- package/dist/node/session.js.map +1 -0
- package/dist/node/store.d.ts +44 -0
- package/dist/node/store.js +91 -0
- package/dist/node/store.js.map +1 -0
- package/dist/query/health.d.ts +16 -0
- package/dist/query/health.js +18 -0
- package/dist/query/health.js.map +1 -0
- package/dist/query/query.d.ts +8 -0
- package/dist/query/query.js +12 -0
- package/dist/query/query.js.map +1 -1
- package/dist/version.gen.d.ts +2 -0
- package/dist/version.gen.js +4 -0
- package/dist/version.gen.js.map +1 -0
- package/package.json +18 -3
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import type { TokenAsk } from '../core/fetch.js';
|
|
2
|
+
import type { ApiError } from '../core/result.js';
|
|
3
|
+
import { type AuthServer } from '../auth/discovery.js';
|
|
4
|
+
import type { Tokens } from '../auth/signIn.js';
|
|
5
|
+
import { type AuthCallOptions } from '../auth/transport.js';
|
|
6
|
+
/** What the app's store holds: `finishSignIn`'s tokens fit. */
|
|
7
|
+
export type SessionTokens = Pick<Tokens, 'accessToken' | 'refreshToken' | 'expiresAt'>;
|
|
8
|
+
/**
|
|
9
|
+
* New tokens from a refresh, for the app to save before anything uses them, or the end of the
|
|
10
|
+
* session. `replaces` is the store's key when the refresh began (D472): its refresh token, or its
|
|
11
|
+
* access token when it has none or an empty one, as `refreshToken || accessToken`. Write only while
|
|
12
|
+
* the store's key is still `replaces` (compare and set, or delete), so a refresh of an old session
|
|
13
|
+
* never overwrites a newer sign-in that another process saved meanwhile.
|
|
14
|
+
*/
|
|
15
|
+
export type TokenUpdate =
|
|
16
|
+
/** A new refresh token came: save both. The one sent is spent. */
|
|
17
|
+
{
|
|
18
|
+
readonly kind: 'rotated';
|
|
19
|
+
readonly accessToken: string;
|
|
20
|
+
readonly refreshToken: string;
|
|
21
|
+
readonly expiresAt?: number;
|
|
22
|
+
readonly replaces: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* This refresh crossed another one, perhaps another process's, and only the access token is
|
|
26
|
+
* new. Keep the refresh token that is stored now: the other refresh may already have saved its
|
|
27
|
+
* new one, and writing back the one this refresh sent would end the session.
|
|
28
|
+
*/
|
|
29
|
+
| {
|
|
30
|
+
readonly kind: 'access-only';
|
|
31
|
+
readonly accessToken: string;
|
|
32
|
+
readonly expiresAt?: number;
|
|
33
|
+
readonly replaces: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The session is over: the server ended it (`invalid_grant`), or the app signed out
|
|
37
|
+
* (`revoke()`). Delete the stored tokens, only while the store's key is still `replaces`, so a
|
|
38
|
+
* newer sign-in is kept. For a store that holds only an access token, `replaces` is that token.
|
|
39
|
+
*/
|
|
40
|
+
| {
|
|
41
|
+
readonly kind: 'cleared';
|
|
42
|
+
readonly replaces: string;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Where the session refreshes and signs out comes from exactly one of `issuer` and `server`:
|
|
46
|
+
* neither or both doesn't compile. From JavaScript, neither, both, or an issuer that is no https
|
|
47
|
+
* URL is a TypeError at creation.
|
|
48
|
+
*/
|
|
49
|
+
export type SessionOptions = Pick<AuthCallOptions, 'fetch'> & ({
|
|
50
|
+
/**
|
|
51
|
+
* The session discovers the server itself (`discover`'s rules), at the first refresh or
|
|
52
|
+
* sign-out that needs it, and keeps it (#130). A token with time left is handed out with
|
|
53
|
+
* nothing sent to the auth host. Written exactly as the metadata names it: for pulse-auth,
|
|
54
|
+
* `https://auth.flipstream.io`, with no slash at the end.
|
|
55
|
+
*/
|
|
56
|
+
readonly issuer: string;
|
|
57
|
+
readonly server?: never;
|
|
58
|
+
} | {
|
|
59
|
+
/** The server, as `discover` gave it, for an app that has discovered already. */
|
|
60
|
+
readonly server: AuthServer;
|
|
61
|
+
readonly issuer?: never;
|
|
62
|
+
}) & {
|
|
63
|
+
/** The app's own OAuth client ID (D421). */
|
|
64
|
+
readonly clientId: string;
|
|
65
|
+
/**
|
|
66
|
+
* The app's client secret, as a confidential client (#123): sent only in the Authorization header
|
|
67
|
+
* of the refresh and the revocation, to the server's own endpoints, and their failures then keep
|
|
68
|
+
* no server text (`finishSignIn`'s rule). Left out, the session is a public client's. Never log it.
|
|
69
|
+
*/
|
|
70
|
+
readonly clientSecret?: string | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* The tokens as the store holds them now, or null when nobody is signed in. Read again before
|
|
73
|
+
* every refresh, so the refresh token sent is the current one, even after another process
|
|
74
|
+
* rotated it, and again just before a refresh's answer is saved.
|
|
75
|
+
*
|
|
76
|
+
* The store's contract (D457): this returns only what `onTokens` has durably saved. Write first,
|
|
77
|
+
* make visible second, so no reader, in this process or another, ever sees tokens whose save
|
|
78
|
+
* may still fail. For example:
|
|
79
|
+
* - the system keychain, whose write is one step that is visible once it has succeeded;
|
|
80
|
+
* - a file, written in full to a temporary name in the same folder, flushed to disk (fsync),
|
|
81
|
+
* renamed over the old one, and the folder flushed too (fsync on the directory, where the
|
|
82
|
+
* platform has one): a reader sees it whole or not at all, and a power cut can't bring back
|
|
83
|
+
* the old file with its spent token.
|
|
84
|
+
*/
|
|
85
|
+
readonly tokens: () => SessionTokens | null | Promise<SessionTokens | null>;
|
|
86
|
+
/**
|
|
87
|
+
* Saves a refresh's tokens, or clears them once the session is over, only while the store's key
|
|
88
|
+
* (`refreshToken || accessToken`) is still `update.replaces`. The session reads the
|
|
89
|
+
* store again just before, and lets the answer go when the store has moved on (a newer sign-in,
|
|
90
|
+
* or a sign-out); comparing and setting on `replaces` closes the moment between that read and
|
|
91
|
+
* this write, and a store that refuses then throws. A refresh's answer is used only once this has
|
|
92
|
+
* settled. When a save fails, the session keeps a rotation's refresh token in memory, the only
|
|
93
|
+
* live one, for its next refresh and for `revoke()` (D456).
|
|
94
|
+
*/
|
|
95
|
+
readonly onTokens: (update: TokenUpdate) => void | Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Runs `work` while holding off every other process's refresh, for a store several Node
|
|
98
|
+
* processes share: `fileLock(path)` from this entry, or the app's own lock on its store. With
|
|
99
|
+
* `fileLock`, keep `tokens` and `onTokens` asynchronous: a holder whose thread is blocked for 10 s
|
|
100
|
+
* loses the lock. A refresh runs inside it (the reads, the request and the save), and so does a
|
|
101
|
+
* revocation.
|
|
102
|
+
* `signal` fires at the refresh's deadline: a lock still held then ends the refresh on time, as
|
|
103
|
+
* `unavailable`, and `work` never sends anything, even if the lock runs it late. A lock that
|
|
104
|
+
* ignores `signal` still holds off the others for as long as it waits.
|
|
105
|
+
*
|
|
106
|
+
* The lock's promise must settle only once `work` has settled, as Web Locks does with its
|
|
107
|
+
* signal: a lock that lets go while `work` still runs (a mutex with a timeout) lets another
|
|
108
|
+
* process in beside a pending save. The session itself waits for `work` before its next turn.
|
|
109
|
+
* A sign-out's turn passes a signal that fires 60 s after the turn reaches the lock (D487): it
|
|
110
|
+
* waits that long to clear the store, though it sends nothing after its own 10 s.
|
|
111
|
+
*
|
|
112
|
+
* Without one, there must be one session per store, shared by everything that uses it: two
|
|
113
|
+
* sessions on one store, in one process (a backend making one per request) or in two, refresh
|
|
114
|
+
* as two processes do. A refresh whose answer takes longer than pulse-auth's 10 s grace can then
|
|
115
|
+
* cross another's refresh of the same token, which pulse-auth reads as reuse, and the session
|
|
116
|
+
* ends.
|
|
117
|
+
*/
|
|
118
|
+
readonly lock?: <T>(work: () => Promise<T>, signal: AbortSignal) => Promise<T>;
|
|
119
|
+
};
|
|
120
|
+
/** Why a session has no token to give. */
|
|
121
|
+
export type SessionFailure =
|
|
122
|
+
/**
|
|
123
|
+
* Nobody is signed in: no tokens, no refresh token, or the server ended the session
|
|
124
|
+
* (`invalid_grant`), after which the stored tokens are cleared (`onTokens`, `cleared`).
|
|
125
|
+
*/
|
|
126
|
+
{
|
|
127
|
+
readonly kind: 'signed-out';
|
|
128
|
+
}
|
|
129
|
+
/** No token within 15 s, or no answer at all: offline, or the server is down. A refresh goes on, and its answer is still saved. */
|
|
130
|
+
| {
|
|
131
|
+
readonly kind: 'unavailable';
|
|
132
|
+
}
|
|
133
|
+
/** The app's store failed: reading the tokens, saving a refresh's, or taking its lock. */
|
|
134
|
+
| {
|
|
135
|
+
readonly kind: 'store_failed';
|
|
136
|
+
readonly step: 'read' | 'save' | 'lock';
|
|
137
|
+
readonly cause: unknown;
|
|
138
|
+
}
|
|
139
|
+
/** The refresh failed any other way: the server refused the client, or answered outside the contract. */
|
|
140
|
+
| {
|
|
141
|
+
readonly kind: 'refresh_failed';
|
|
142
|
+
readonly error: ApiError;
|
|
143
|
+
};
|
|
144
|
+
/** Thrown by `session.token()`; the default transport turns it into `no_token`, with this as its `cause`. */
|
|
145
|
+
export declare class SessionError extends Error {
|
|
146
|
+
readonly failure: SessionFailure;
|
|
147
|
+
constructor(failure: SessionFailure);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* How a revocation came out (RFC 7009). pulse-auth answers a revocation 200 whatever it was sent,
|
|
151
|
+
* so that answer proves nothing: `revoked` rests on the issuer taking the session's access token
|
|
152
|
+
* just before and refusing it just after (D461, D464). Either way, what the app clears locally is
|
|
153
|
+
* its own call.
|
|
154
|
+
*/
|
|
155
|
+
export type RevokeResult = {
|
|
156
|
+
readonly kind: 'revoked';
|
|
157
|
+
} | {
|
|
158
|
+
readonly kind: 'unconfirmed';
|
|
159
|
+
/**
|
|
160
|
+
* Why it isn't known to be over:
|
|
161
|
+
* - the revocation's own failure (an `ApiError`, `aborted` and `timeout` included);
|
|
162
|
+
* - `still_live`: the issuer still takes the access token after its revocation;
|
|
163
|
+
* - `check_failed`: the revocation went, but the check before or after it couldn't be made,
|
|
164
|
+
* or the issuer didn't take the access token before it (`error` says how);
|
|
165
|
+
* - `unconfirmable`: no access token known to have 30 s left to check with, so the refresh
|
|
166
|
+
* token went (or a last access token), and nothing can tell whether it ended the session;
|
|
167
|
+
* - `maybe_spent`: as `unconfirmable`, and the refresh token's last refresh got no answer, so
|
|
168
|
+
* the server may have rotated it, and it may end nothing;
|
|
169
|
+
* - `no_endpoint`, `no_tokens` (nothing to send), or the store failing.
|
|
170
|
+
*/
|
|
171
|
+
readonly reason: ApiError | {
|
|
172
|
+
readonly kind: 'still_live';
|
|
173
|
+
} | {
|
|
174
|
+
readonly kind: 'check_failed';
|
|
175
|
+
readonly error: ApiError;
|
|
176
|
+
} | {
|
|
177
|
+
readonly kind: 'unconfirmable';
|
|
178
|
+
} | {
|
|
179
|
+
readonly kind: 'maybe_spent';
|
|
180
|
+
} | {
|
|
181
|
+
readonly kind: 'no_endpoint';
|
|
182
|
+
} | {
|
|
183
|
+
readonly kind: 'no_tokens';
|
|
184
|
+
} | Extract<SessionFailure, {
|
|
185
|
+
readonly kind: 'store_failed';
|
|
186
|
+
}>;
|
|
187
|
+
};
|
|
188
|
+
export interface Session {
|
|
189
|
+
/**
|
|
190
|
+
* The token function for `createFlipstream` (or `fetchTransport`): the access token while it has
|
|
191
|
+
* more than a minute left, or else a refreshed one. Callers arriving while a refresh runs share
|
|
192
|
+
* it. After a 401 (`rejected`), the token is refreshed, unless another refresh already replaced
|
|
193
|
+
* it. Rejects with a `SessionError`.
|
|
194
|
+
*/
|
|
195
|
+
token(ask?: TokenAsk): Promise<string>;
|
|
196
|
+
/**
|
|
197
|
+
* Ends the session at the server (RFC 7009), with the tokens read once no refresh is spending
|
|
198
|
+
* them, inside the app's `lock` when there is one, all within 10 s, with nothing sent after. An
|
|
199
|
+
* access token known to have at least 30 s left is checked at the issuer, revoked (pulse-auth
|
|
200
|
+
* ends the whole session by it, whatever became of the refresh token) and checked again: only
|
|
201
|
+
* taken before and refused after is `revoked`. Without such a token, or when the check before
|
|
202
|
+
* fails, the live refresh token goes (the store's, or a rotation's whose save failed, D456), and
|
|
203
|
+
* the result is `unconfirmed`.
|
|
204
|
+
*
|
|
205
|
+
* A sign-out ends the session the store holds when its turn comes (#134), a newer sign-in from
|
|
206
|
+
* another terminal included: signing out means this person is signed out on this machine (D484).
|
|
207
|
+
* Its turn is queued at the call, and in one hold of the app's lock it revokes that session, then
|
|
208
|
+
* clears it from the store through `onTokens` (`cleared`), once, however the revocation went: in
|
|
209
|
+
* time or too late to send anything, aborted by the caller (D478), failed, or with any result. A
|
|
210
|
+
* sign-in saved after the turn read the store is kept. The caller waits for the clear within its
|
|
211
|
+
* 10 s.
|
|
212
|
+
*
|
|
213
|
+
* Once the turn reaches the lock, it waits at most 60 s for it, and at most 60 s for each store
|
|
214
|
+
* call; then it gives up, touching nothing more, and later turns run (D487). So after `timeout`
|
|
215
|
+
* or `aborted` the clear may still be on its way: 60 s for the lock and for each of its three
|
|
216
|
+
* store calls, so up to about four minutes after the turn reaches the lock, plus any queue ahead
|
|
217
|
+
* of it. A lock held longer than its 60 s by another process, or a store call that doesn't answer
|
|
218
|
+
* within its 60 s, leaves the store as it was. A clear the
|
|
219
|
+
* store fails leaves the tokens too: until the next refresh meets `invalid_grant`, or, for a store
|
|
220
|
+
* holding only an access token, until that token expires.
|
|
221
|
+
*/
|
|
222
|
+
revoke(options?: Pick<AuthCallOptions, 'signal'>): Promise<RevokeResult>;
|
|
223
|
+
}
|
|
224
|
+
export declare function createSession(options: SessionOptions): Session;
|