techne-sdk 0.7.0 → 0.9.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 +39 -0
- package/README.md +50 -0
- package/dist/auth/placement.gen.d.ts +1 -1
- package/dist/auth/placement.gen.js +1 -0
- package/dist/catalog/catalog.gen.js +95 -4
- package/dist/generated/index.d.ts +28 -0
- package/dist/generated/index.js +36 -0
- package/dist/generated/lexicons.d.ts +76 -0
- package/dist/generated/lexicons.js +39 -0
- package/dist/generated/nsid.d.ts +1 -0
- package/dist/generated/nsid.js +1 -0
- package/dist/generated/nsid.json +1 -0
- package/dist/generated/records.d.ts +8 -6
- package/dist/generated/types/pub/techne/draft/tagFeedback.d.ts +18 -0
- package/dist/generated/types/pub/techne/draft/tagFeedback.js +12 -0
- package/dist/session/dpop.d.ts +49 -0
- package/dist/session/dpop.js +105 -0
- package/dist/session/index.d.ts +22 -0
- package/dist/session/index.js +22 -0
- package/dist/session/own-session.d.ts +169 -0
- package/dist/session/own-session.js +364 -0
- package/dist/session/phone.d.ts +79 -0
- package/dist/session/phone.js +179 -0
- package/dist/session/relay.d.ts +69 -0
- package/dist/session/relay.js +120 -0
- package/dist/wasm/techne-wasm.gen.js +1 -1
- package/dist/wasm/techne-wasm2js.gen.js +14138 -14138
- package/package.json +10 -3
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GENERATED CODE - DO NOT MODIFY
|
|
3
|
+
*/
|
|
4
|
+
import { type ValidationResult } from '@atproto/lexicon';
|
|
5
|
+
import type * as ComAtprotoRepoStrongRef from '../../../com/atproto/repo/strongRef.js';
|
|
6
|
+
export interface Main {
|
|
7
|
+
$type: 'pub.techne.draft.tagFeedback';
|
|
8
|
+
subject: ComAtprotoRepoStrongRef.Main;
|
|
9
|
+
/** The tag as the app names it: a chip id in the app's chip groups, e.g. allAges or kids. Not a bank question: the chip groups map the tag to its questions. */
|
|
10
|
+
tag: string;
|
|
11
|
+
/** What the person says about the record, not about the display: `fits`: the record has this tag's quality; `doesntFit`: it does not. Either can be said whether or not the tag is shown; feedback that agrees with what is shown asks nothing. */
|
|
12
|
+
verdict: 'fits' | 'doesntFit' | (string & {});
|
|
13
|
+
createdAt: string;
|
|
14
|
+
[k: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
export declare function isMain<V>(v: V): v is import("../../../../util.js").$TypedObject<V, "pub.techne.draft.tagFeedback", "main">;
|
|
17
|
+
export declare function validateMain<V>(v: V): ValidationResult<Main & V>;
|
|
18
|
+
export { type Main as Record, isMain as isRecord, validateMain as validateRecord, };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { validate as _validate } from '../../../../lexicons.js';
|
|
2
|
+
import { is$typed as _is$typed, } from '../../../../util.js';
|
|
3
|
+
const is$typed = _is$typed, validate = _validate;
|
|
4
|
+
const id = 'pub.techne.draft.tagFeedback';
|
|
5
|
+
const hashMain = 'main';
|
|
6
|
+
export function isMain(v) {
|
|
7
|
+
return is$typed(v, id, hashMain);
|
|
8
|
+
}
|
|
9
|
+
export function validateMain(v) {
|
|
10
|
+
return validate(v, id, hashMain, true);
|
|
11
|
+
}
|
|
12
|
+
export { isMain as isRecord, validateMain as validateRecord, };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** A P-256 private key as a JWK (`d` is the secret). Keep it in the app's secure storage, never in a log. */
|
|
2
|
+
export interface DpopKey {
|
|
3
|
+
kty: 'EC';
|
|
4
|
+
crv: 'P-256';
|
|
5
|
+
x: string;
|
|
6
|
+
y: string;
|
|
7
|
+
d: string;
|
|
8
|
+
}
|
|
9
|
+
/** The public half of a {@link DpopKey}: what a proof's header carries. */
|
|
10
|
+
export type DpopPublicKey = Omit<DpopKey, 'd'>;
|
|
11
|
+
/** `bytes` as unpadded base64url. */
|
|
12
|
+
export declare function base64url(bytes: Uint8Array): string;
|
|
13
|
+
/** The bytes of unpadded (or padded) base64url `s`. */
|
|
14
|
+
export declare function fromBase64url(s: string): Uint8Array;
|
|
15
|
+
/** base64url SHA-256 of `s` (UTF-8): a proof's `ath` for token `s`, or the S256 PKCE challenge of verifier `s`. */
|
|
16
|
+
export declare function sha256b64url(s: string): string;
|
|
17
|
+
/** A new P-256 key for DPoP. */
|
|
18
|
+
export declare function generateDpopKey(): DpopKey;
|
|
19
|
+
/** `key` checked to be a P-256 private JWK whose public half matches its secret (a JWK from storage or
|
|
20
|
+
* from the platform's `adoptHeldSession`). */
|
|
21
|
+
export declare function dpopKeyFromJwk(key: unknown): DpopKey;
|
|
22
|
+
/** The public half of `key`. */
|
|
23
|
+
export declare function publicJwk(key: DpopKey): DpopPublicKey;
|
|
24
|
+
/** The RFC 7638 thumbprint of `key` (`jkt`): base64url SHA-256 of its required members in order. */
|
|
25
|
+
export declare function jwkThumbprint(key: DpopPublicKey): string;
|
|
26
|
+
/** A random URL-safe string from `n` random bytes (`jti`, the PKCE verifier). */
|
|
27
|
+
export declare function randomString(n?: number): string;
|
|
28
|
+
/** A PKCE pair (S256): the verifier the app keeps, the challenge it sends. */
|
|
29
|
+
export declare function pkcePair(): {
|
|
30
|
+
verifier: string;
|
|
31
|
+
challenge: string;
|
|
32
|
+
};
|
|
33
|
+
/** `url` without its query and fragment: a proof's `htu` (RFC 9449 section 4.2). */
|
|
34
|
+
export declare function htuOf(url: string): string;
|
|
35
|
+
/** What one proof is for. */
|
|
36
|
+
export interface ProofFor {
|
|
37
|
+
/** The request's method (`POST`, `GET`; `GET` for a WebSocket upgrade). */
|
|
38
|
+
htm: string;
|
|
39
|
+
/** The request's full URL; its query and fragment are dropped ({@link htuOf}). */
|
|
40
|
+
htu: string;
|
|
41
|
+
/** The server's latest DPoP nonce, when it asks for one. */
|
|
42
|
+
nonce?: string;
|
|
43
|
+
/** The access token (or device credential) the proof goes with: its hash becomes `ath`. */
|
|
44
|
+
token?: string;
|
|
45
|
+
/** Unix seconds; now by default. */
|
|
46
|
+
iat?: number;
|
|
47
|
+
}
|
|
48
|
+
/** A DPoP proof (a `dpop+jwt`, ES256) by `key` for one request. Each has a fresh `jti`. */
|
|
49
|
+
export declare function dpopProof(key: DpopKey, p: ProofFor): string;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DPoP (RFC 9449) and PKCE (RFC 7636) for a session the app holds itself, in plain JavaScript: P-256 and
|
|
3
|
+
* SHA-256 from `@noble/curves` and `@noble/hashes`, so it runs on Hermes (React Native), which has no
|
|
4
|
+
* `crypto.subtle`. The one thing the host must give is `crypto.getRandomValues` (React Native:
|
|
5
|
+
* `react-native-get-random-values`, imported once at the app's entry).
|
|
6
|
+
*
|
|
7
|
+
* No `Buffer` and no `URL` here either: React Native has neither in full.
|
|
8
|
+
*/
|
|
9
|
+
import { p256 } from '@noble/curves/nist.js';
|
|
10
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
11
|
+
import { randomBytes } from '@noble/hashes/utils.js';
|
|
12
|
+
const B64U = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
13
|
+
/** `bytes` as unpadded base64url. */
|
|
14
|
+
export function base64url(bytes) {
|
|
15
|
+
let out = '';
|
|
16
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
17
|
+
const n = (bytes[i] << 16) | ((bytes[i + 1] ?? 0) << 8) | (bytes[i + 2] ?? 0);
|
|
18
|
+
out += B64U[(n >> 18) & 63] + B64U[(n >> 12) & 63];
|
|
19
|
+
if (i + 1 < bytes.length)
|
|
20
|
+
out += B64U[(n >> 6) & 63];
|
|
21
|
+
if (i + 2 < bytes.length)
|
|
22
|
+
out += B64U[n & 63];
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
/** The bytes of unpadded (or padded) base64url `s`. */
|
|
27
|
+
export function fromBase64url(s) {
|
|
28
|
+
const clean = s.replace(/=+$/, '');
|
|
29
|
+
const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
|
|
30
|
+
let bits = 0;
|
|
31
|
+
let acc = 0;
|
|
32
|
+
let j = 0;
|
|
33
|
+
for (const ch of clean) {
|
|
34
|
+
const v = B64U.indexOf(ch);
|
|
35
|
+
if (v < 0)
|
|
36
|
+
throw new Error('techne-sdk/session: not base64url');
|
|
37
|
+
acc = (acc << 6) | v;
|
|
38
|
+
bits += 6;
|
|
39
|
+
if (bits >= 8) {
|
|
40
|
+
bits -= 8;
|
|
41
|
+
out[j++] = (acc >> bits) & 0xff;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
const utf8 = (s) => new TextEncoder().encode(s);
|
|
47
|
+
/** base64url SHA-256 of `s` (UTF-8): a proof's `ath` for token `s`, or the S256 PKCE challenge of verifier `s`. */
|
|
48
|
+
export function sha256b64url(s) {
|
|
49
|
+
return base64url(sha256(utf8(s)));
|
|
50
|
+
}
|
|
51
|
+
/** A new P-256 key for DPoP. */
|
|
52
|
+
export function generateDpopKey() {
|
|
53
|
+
const secret = p256.utils.randomSecretKey();
|
|
54
|
+
return jwkOf(secret);
|
|
55
|
+
}
|
|
56
|
+
function jwkOf(secret) {
|
|
57
|
+
const pub = p256.getPublicKey(secret, false); // 0x04 || x || y
|
|
58
|
+
return { kty: 'EC', crv: 'P-256', x: base64url(pub.slice(1, 33)), y: base64url(pub.slice(33, 65)), d: base64url(secret) };
|
|
59
|
+
}
|
|
60
|
+
/** `key` checked to be a P-256 private JWK whose public half matches its secret (a JWK from storage or
|
|
61
|
+
* from the platform's `adoptHeldSession`). */
|
|
62
|
+
export function dpopKeyFromJwk(key) {
|
|
63
|
+
const k = key;
|
|
64
|
+
if (!k || k.kty !== 'EC' || k.crv !== 'P-256' || typeof k.d !== 'string' || typeof k.x !== 'string' || typeof k.y !== 'string') {
|
|
65
|
+
throw new Error('techne-sdk/session: the DPoP key is not a P-256 private JWK');
|
|
66
|
+
}
|
|
67
|
+
const derived = jwkOf(fromBase64url(k.d));
|
|
68
|
+
if (derived.x !== k.x || derived.y !== k.y)
|
|
69
|
+
throw new Error('techne-sdk/session: the DPoP key does not match its public half');
|
|
70
|
+
return derived;
|
|
71
|
+
}
|
|
72
|
+
/** The public half of `key`. */
|
|
73
|
+
export function publicJwk(key) {
|
|
74
|
+
return { kty: key.kty, crv: key.crv, x: key.x, y: key.y };
|
|
75
|
+
}
|
|
76
|
+
/** The RFC 7638 thumbprint of `key` (`jkt`): base64url SHA-256 of its required members in order. */
|
|
77
|
+
export function jwkThumbprint(key) {
|
|
78
|
+
return sha256b64url(JSON.stringify({ crv: key.crv, kty: key.kty, x: key.x, y: key.y }));
|
|
79
|
+
}
|
|
80
|
+
/** A random URL-safe string from `n` random bytes (`jti`, the PKCE verifier). */
|
|
81
|
+
export function randomString(n = 32) {
|
|
82
|
+
return base64url(randomBytes(n));
|
|
83
|
+
}
|
|
84
|
+
/** A PKCE pair (S256): the verifier the app keeps, the challenge it sends. */
|
|
85
|
+
export function pkcePair() {
|
|
86
|
+
const verifier = randomString(32);
|
|
87
|
+
return { verifier, challenge: sha256b64url(verifier) };
|
|
88
|
+
}
|
|
89
|
+
/** `url` without its query and fragment: a proof's `htu` (RFC 9449 section 4.2). */
|
|
90
|
+
export function htuOf(url) {
|
|
91
|
+
return url.replace(/[?#].*$/, '');
|
|
92
|
+
}
|
|
93
|
+
/** A DPoP proof (a `dpop+jwt`, ES256) by `key` for one request. Each has a fresh `jti`. */
|
|
94
|
+
export function dpopProof(key, p) {
|
|
95
|
+
const header = { typ: 'dpop+jwt', alg: 'ES256', jwk: publicJwk(key) };
|
|
96
|
+
const claims = { jti: randomString(16), htm: p.htm.toUpperCase(), htu: htuOf(p.htu), iat: p.iat ?? Math.floor(Date.now() / 1000) };
|
|
97
|
+
if (p.nonce)
|
|
98
|
+
claims.nonce = p.nonce;
|
|
99
|
+
if (p.token !== undefined)
|
|
100
|
+
claims.ath = sha256b64url(p.token);
|
|
101
|
+
const input = `${base64url(utf8(JSON.stringify(header)))}.${base64url(utf8(JSON.stringify(claims)))}`;
|
|
102
|
+
// noble hashes the message with SHA-256 first (prehash) and answers the 64-byte r||s JWS wants.
|
|
103
|
+
const sig = p256.sign(utf8(input), fromBase64url(key.d));
|
|
104
|
+
return `${input}.${base64url(sig)}`;
|
|
105
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* techne-sdk/session — a session the app holds itself, for React Native (Hermes) as well as the browser:
|
|
3
|
+
* sign-in through the platform's phone door in own-session mode, the tokens and their DPoP key on the
|
|
4
|
+
* device, refresh and revoke through the platform's relay, and the narrow device credential with a proof
|
|
5
|
+
* per request. Plain JavaScript (`@noble/curves` P-256), no native module. The host provides
|
|
6
|
+
* `crypto.getRandomValues` (React Native: `react-native-get-random-values`) and `TextEncoder` (Hermes has it
|
|
7
|
+
* from React Native 0.74).
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { OwnSession, startPhoneSignIn, finishPhoneSignIn } from 'techne-sdk/session'
|
|
11
|
+
* import { connect } from 'techne-sdk/connect'
|
|
12
|
+
*
|
|
13
|
+
* const pending = await startPhoneSignIn({ platformUrl, phone })
|
|
14
|
+
* const { session: data } = await finishPhoneSignIn(pending, code, { platformUrl, device: { platform: 'ios' } })
|
|
15
|
+
* const session = await OwnSession.signedIn(data, { platformUrl, store }) // later: OwnSession.restore({ platformUrl, store })
|
|
16
|
+
* const techne = connect(session, TECHNE_PLATFORM_DID)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export { dpopProof, generateDpopKey, jwkThumbprint, type DpopKey, type ProofFor } from './dpop.js';
|
|
20
|
+
export { OwnSession, deviceProofHeaders, type OwnSessionData, type OwnSessionOptions, type RefreshLock, type TokenStore } from './own-session.js';
|
|
21
|
+
export { adoptHeldSession, finishPhoneSignIn, startPhoneSignIn, type FinishPhoneSignInOptions, type OwnSessionEndpoints, type PendingPhoneSignIn, type PhoneSignInOptions, } from './phone.js';
|
|
22
|
+
export { NETWORK_ERROR, OwnSessionError, SESSION_ENDED } from './relay.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* techne-sdk/session — a session the app holds itself, for React Native (Hermes) as well as the browser:
|
|
3
|
+
* sign-in through the platform's phone door in own-session mode, the tokens and their DPoP key on the
|
|
4
|
+
* device, refresh and revoke through the platform's relay, and the narrow device credential with a proof
|
|
5
|
+
* per request. Plain JavaScript (`@noble/curves` P-256), no native module. The host provides
|
|
6
|
+
* `crypto.getRandomValues` (React Native: `react-native-get-random-values`) and `TextEncoder` (Hermes has it
|
|
7
|
+
* from React Native 0.74).
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { OwnSession, startPhoneSignIn, finishPhoneSignIn } from 'techne-sdk/session'
|
|
11
|
+
* import { connect } from 'techne-sdk/connect'
|
|
12
|
+
*
|
|
13
|
+
* const pending = await startPhoneSignIn({ platformUrl, phone })
|
|
14
|
+
* const { session: data } = await finishPhoneSignIn(pending, code, { platformUrl, device: { platform: 'ios' } })
|
|
15
|
+
* const session = await OwnSession.signedIn(data, { platformUrl, store }) // later: OwnSession.restore({ platformUrl, store })
|
|
16
|
+
* const techne = connect(session, TECHNE_PLATFORM_DID)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export { dpopProof, generateDpopKey, jwkThumbprint } from './dpop.js';
|
|
20
|
+
export { OwnSession, deviceProofHeaders } from './own-session.js';
|
|
21
|
+
export { adoptHeldSession, finishPhoneSignIn, startPhoneSignIn, } from './phone.js';
|
|
22
|
+
export { NETWORK_ERROR, OwnSessionError, SESSION_ENDED } from './relay.js';
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A session the app holds itself (#redesign 659c31df): the OAuth tokens for the person's PDS, bound to a
|
|
3
|
+
* DPoP key the app made, plus the platform's narrow device credential. It is a {@link Session}
|
|
4
|
+
* (`did` + `fetchHandler`), so `connect(session, platform)` takes it as it takes a browser OAuth session.
|
|
5
|
+
*
|
|
6
|
+
* - **Calls to the PDS** carry `Authorization: DPoP <access token>` and a fresh proof (with `ath`) per
|
|
7
|
+
* request. A `use_dpop_nonce` answer is signed again once with the nonce it names; nonces are kept per
|
|
8
|
+
* origin. An `invalid_token` answer refreshes and sends once more.
|
|
9
|
+
* - **Refresh** goes through the platform's `refreshOwnSession` (the platform adds its client assertion; the
|
|
10
|
+
* proof is the device's). The PDS rotates the refresh token on every refresh and ends the whole grant if
|
|
11
|
+
* an old one comes back or a refresh is signed by another key, so:
|
|
12
|
+
* - one refresh at a time: in this process by a single flight, across processes by the `lock` option
|
|
13
|
+
* the app backs with its own cross-process lock; inside it the store is read again, and a token set
|
|
14
|
+
* another process already rotated is taken instead of refreshing again;
|
|
15
|
+
* - early: a call starts a refresh when the access token has less than `refreshEarlyMs` to live; while
|
|
16
|
+
* the token still has more than {@link STILL_VALID_MS} the call goes on with it and the refresh runs
|
|
17
|
+
* (or fails) behind it, so a platform that is down does not hold the call;
|
|
18
|
+
* - the rotated set is saved to the store before it is used;
|
|
19
|
+
* - a refresh that got no answer (the network failed or timed out, a gateway answered 502/504, or the
|
|
20
|
+
* platform answered 503/429 while the PDS call with that token was still running) is sent again, the
|
|
21
|
+
* same refresh token with a fresh proof by the same key, starting up to {@link LOST_ANSWER_WINDOW_MS}
|
|
22
|
+
* after the first: the platform keeps each refresh's answer for 60 s and gives it to the same token
|
|
23
|
+
* and key again, so a lost answer does not sign the person out. Each send is given up
|
|
24
|
+
* {@link SEND_MARGIN_MS} before those 60 s end, so no resend reaches the platform after its answer is
|
|
25
|
+
* gone. A phone that stays offline past that window after the PDS rotated is signed out on its next
|
|
26
|
+
* refresh (the window is the platform's).
|
|
27
|
+
* - the store holding a set under another key (the person signed out and in again in another process)
|
|
28
|
+
* ends this session and leaves the store alone: a refresh or a revoke signed by this key would end the
|
|
29
|
+
* other process's grant;
|
|
30
|
+
* - a rotated set whose save failed is saved by the next refresh only while the store still holds the
|
|
31
|
+
* set it came from; otherwise the store's (newer) set is taken.
|
|
32
|
+
* - an answer the device cannot use after the token rotated (no access token, another `sub`) ends the
|
|
33
|
+
* session: the old token is spent, so keeping it would end the grant later anyway.
|
|
34
|
+
* - **Sign-out** waits for a refresh in flight, revokes the grant (`revokeOwnSession`) and the device
|
|
35
|
+
* credential (`signOutDevice`), each given up after {@link SIGN_OUT_TIMEOUT_MS}, and clears the store,
|
|
36
|
+
* whatever the network says. When the store holds another key's set it touches neither that set nor the
|
|
37
|
+
* store.
|
|
38
|
+
* - **The device credential** (`rsds_`, narrowed to this key) goes only with a per-request proof:
|
|
39
|
+
* {@link OwnSession.deviceFetchHandler} for registerDevicePush, listDeviceSessions and signOutDevice,
|
|
40
|
+
* {@link OwnSession.deviceHeaders} for the change stream's WebSocket.
|
|
41
|
+
*/
|
|
42
|
+
import type { FetchHandler } from '@atproto/xrpc';
|
|
43
|
+
import { type DpopKey } from './dpop.js';
|
|
44
|
+
import { OwnSessionError, type Fetch } from './relay.js';
|
|
45
|
+
/** What the app keeps for a signed-in device, in secure storage (every field but `did`, `pds` and the
|
|
46
|
+
* endpoints is a secret). Plain JSON, so a store writes it as a string. */
|
|
47
|
+
export interface OwnSessionData {
|
|
48
|
+
/** The person's DID (the tokens' `sub`). */
|
|
49
|
+
did: string;
|
|
50
|
+
/** The PDS the tokens are for (an origin). */
|
|
51
|
+
pds: string;
|
|
52
|
+
accessToken: string;
|
|
53
|
+
refreshToken?: string;
|
|
54
|
+
/** When the access token expires, in milliseconds since the epoch. */
|
|
55
|
+
expiresAt?: number;
|
|
56
|
+
scope?: string;
|
|
57
|
+
/** The authorization server. */
|
|
58
|
+
issuer?: string;
|
|
59
|
+
/** The `htu` of refresh proofs (`startPhoneSignup` names it). */
|
|
60
|
+
tokenEndpoint: string;
|
|
61
|
+
/** The `htu` of revoke proofs. */
|
|
62
|
+
revocationEndpoint: string;
|
|
63
|
+
/** The session's DPoP key (a P-256 private JWK). */
|
|
64
|
+
dpopKey: DpopKey;
|
|
65
|
+
/** The latest DPoP nonce the authorization server gave. */
|
|
66
|
+
dpopNonce?: string;
|
|
67
|
+
/** The platform's narrow device credential (`rsds_`), bound to `dpopKey`. */
|
|
68
|
+
deviceSession?: string;
|
|
69
|
+
/** That device's public handle (`listDeviceSessions`). */
|
|
70
|
+
deviceId?: string;
|
|
71
|
+
}
|
|
72
|
+
/** Where the app keeps its {@link OwnSessionData} (Beacon: expo-secure-store). `get` answers null when
|
|
73
|
+
* nobody is signed in. Every process of the app must see the same store. */
|
|
74
|
+
export interface TokenStore {
|
|
75
|
+
get(): Promise<OwnSessionData | null>;
|
|
76
|
+
set(data: OwnSessionData): Promise<void>;
|
|
77
|
+
delete(): Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
/** Runs `fn` while holding a lock every process of the app shares (a refresh rotates the token, so two at
|
|
80
|
+
* once end the session). The in-process single flight is always there; this adds the cross-process part. */
|
|
81
|
+
export type RefreshLock = <T>(fn: () => Promise<T>) => Promise<T>;
|
|
82
|
+
/** {@link OwnSession}'s options. */
|
|
83
|
+
export interface OwnSessionOptions {
|
|
84
|
+
/** The platform's origin (`https://...`): where `refreshOwnSession`, `revokeOwnSession` and the device calls go. */
|
|
85
|
+
platformUrl: string;
|
|
86
|
+
store: TokenStore;
|
|
87
|
+
/** The cross-process lock around a refresh and a sign-out; none by default. */
|
|
88
|
+
lock?: RefreshLock;
|
|
89
|
+
/** Refresh when the access token has less than this to live; 5 minutes by default. */
|
|
90
|
+
refreshEarlyMs?: number;
|
|
91
|
+
/** Called once when the session ends (the PDS dropped the grant, or sign-out): show sign-in. */
|
|
92
|
+
onEnded?: (error: OwnSessionError) => void;
|
|
93
|
+
/** The fetch every request goes through; `globalThis.fetch` by default. */
|
|
94
|
+
fetch?: Fetch;
|
|
95
|
+
/** Clock and timer (tests). */
|
|
96
|
+
now?: () => number;
|
|
97
|
+
sleep?: (ms: number) => Promise<void>;
|
|
98
|
+
}
|
|
99
|
+
/** How long the platform keeps a refresh's answer for the same token and key. */
|
|
100
|
+
export declare const PLATFORM_ANSWER_WINDOW_MS = 60000;
|
|
101
|
+
/** How long after the first send a refresh that got no answer may be sent again. */
|
|
102
|
+
export declare const LOST_ANSWER_WINDOW_MS = 50000;
|
|
103
|
+
/** Every send is given up this long before {@link PLATFORM_ANSWER_WINDOW_MS} ends (clock skew, transit). */
|
|
104
|
+
export declare const SEND_MARGIN_MS = 5000;
|
|
105
|
+
/** How long sign-out waits for each of its calls (revoke, signOutDevice). */
|
|
106
|
+
export declare const SIGN_OUT_TIMEOUT_MS = 10000;
|
|
107
|
+
/** An early refresh holds the call only when the access token has less than this to live. */
|
|
108
|
+
export declare const STILL_VALID_MS = 30000;
|
|
109
|
+
/** The origin of `url` (`scheme://host[:port]`), lower-cased: what a DPoP nonce is kept under. */
|
|
110
|
+
export declare function nonceKey(url: string): string;
|
|
111
|
+
/** The proof headers for a device-credential request: `Authorization: Bearer <rsds_>` and a `DPoP` proof by
|
|
112
|
+
* `key` for `method` `url` with `ath` over the credential (no server nonce is asked for). */
|
|
113
|
+
export declare function deviceProofHeaders(key: DpopKey, deviceSession: string, method: string, url: string): Record<string, string>;
|
|
114
|
+
/** The device's own session (module doc). Make one with {@link OwnSession.restore} or {@link OwnSession.signedIn}.
|
|
115
|
+
* The tokens and the key are in `#private` fields, so logging the session prints neither. */
|
|
116
|
+
export declare class OwnSession {
|
|
117
|
+
#private;
|
|
118
|
+
private constructor();
|
|
119
|
+
/** The session in the store, or null when nobody is signed in there. */
|
|
120
|
+
static restore(opts: OwnSessionOptions): Promise<OwnSession | null>;
|
|
121
|
+
/** A session just signed in (`finishPhoneSignIn`, `adoptHeldSession`): saved to the store first. */
|
|
122
|
+
static signedIn(data: OwnSessionData, opts: OwnSessionOptions): Promise<OwnSession>;
|
|
123
|
+
/** The signed-in person's DID. */
|
|
124
|
+
get did(): string;
|
|
125
|
+
/** The device credential's public handle, when there is one. */
|
|
126
|
+
get deviceId(): string | undefined;
|
|
127
|
+
/** When the access token expires (ms since the epoch), when known. */
|
|
128
|
+
get expiresAt(): number | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* A request to the person's PDS with the session: `url` is a path (`/xrpc/...`, how an XRPC client calls
|
|
131
|
+
* a fetch handler) on the PDS, or a full URL. `connect` calls this.
|
|
132
|
+
*/
|
|
133
|
+
fetchHandler: FetchHandler;
|
|
134
|
+
/** The access token. When it has less than `refreshEarlyMs` to live a refresh starts; the call waits for
|
|
135
|
+
* it only when the token has less than {@link STILL_VALID_MS} left. A refresh that fails while the token
|
|
136
|
+
* still lives does not fail the call (the next one tries again); one that ends the session does. */
|
|
137
|
+
accessToken(): Promise<string>;
|
|
138
|
+
/**
|
|
139
|
+
* Refresh now (one at a time; module doc). With `seen`, the access token the caller found stale: when the
|
|
140
|
+
* store already holds another (another process refreshed), that one is taken and nothing is sent.
|
|
141
|
+
* Rejects with `SessionEnded` when the session is over (the store is cleared and `onEnded` called; when
|
|
142
|
+
* the store holds another key's set, it is left alone), `NetworkError` when no answer came inside the
|
|
143
|
+
* lost-answer window (the session is kept), or the platform's refusal.
|
|
144
|
+
*/
|
|
145
|
+
refresh(seen?: string): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Sign out: wait for a refresh in flight (so the newest refresh token is the one revoked), revoke the grant
|
|
148
|
+
* at the PDS (`revokeOwnSession` with the refresh token, so the whole grant goes), sign the device
|
|
149
|
+
* credential out (`signOutDevice`), clear the store. Each call is given up after
|
|
150
|
+
* {@link SIGN_OUT_TIMEOUT_MS}. The store is cleared and the session ends whatever the network says; the
|
|
151
|
+
* answer says what the server took. When the store holds no set of this session's (signed out, or signed
|
|
152
|
+
* in under another key, in another process), nothing is sent and the store is left alone; when the store
|
|
153
|
+
* cannot be read, this session's own set is revoked and the store is left alone. A second call answers
|
|
154
|
+
* the first one's result.
|
|
155
|
+
*/
|
|
156
|
+
signOut(): Promise<{
|
|
157
|
+
revoked: boolean;
|
|
158
|
+
deviceSignedOut: boolean;
|
|
159
|
+
}>;
|
|
160
|
+
/** The headers for one device-credential request (`Authorization: Bearer <rsds_>` and its `DPoP` proof):
|
|
161
|
+
* for the change stream, `deviceHeaders('GET', 'wss://<platform>/xrpc/_stream/changes')` on the
|
|
162
|
+
* WebSocket. Each call makes a new proof; use it for one request. Null when the session has no device
|
|
163
|
+
* credential. */
|
|
164
|
+
deviceHeaders(method: string, url: string): Record<string, string> | null;
|
|
165
|
+
/** A fetch handler to the platform with the device credential and a fresh proof per request: for
|
|
166
|
+
* registerDevicePush, listDeviceSessions and signOutDevice (`new AtpBaseClient(session.deviceFetchHandler)`).
|
|
167
|
+
* `url` is a path on the platform (`/xrpc/...`) or a full URL. */
|
|
168
|
+
deviceFetchHandler: FetchHandler;
|
|
169
|
+
}
|