@assistant-ui/better-auth-dbsc 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +364 -2
- package/dist/client.d.ts +27 -0
- package/dist/client.js +14 -0
- package/dist/core-Crgd63SH.js +409 -0
- package/dist/core.d.ts +3 -0
- package/dist/core.js +1 -0
- package/dist/dbsc-plugin-D9MKOgZP.d.ts +784 -0
- package/dist/headers-KXqmHcXY.d.ts +161 -0
- package/dist/index-s7R-j-qI.d.ts +94 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +977 -0
- package/dist/package-DTjet4zW.js +19 -0
- package/docs/threat-model.md +48 -0
- package/migrations/sqlite/0001_dbsc_session.sql +4 -0
- package/package.json +78 -4
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { JWK } from "jose";
|
|
2
|
+
/** A rule in `scope.scope_specification`. */
|
|
3
|
+
interface DbscScopeRule {
|
|
4
|
+
type: "include" | "exclude";
|
|
5
|
+
/** Host pattern such as `example.com` or `*.example.com`. Defaults to every host. */
|
|
6
|
+
domain?: string;
|
|
7
|
+
/** Path prefix. Defaults to `/`. */
|
|
8
|
+
path?: string;
|
|
9
|
+
}
|
|
10
|
+
interface DbscCredential {
|
|
11
|
+
type: "cookie";
|
|
12
|
+
name: string;
|
|
13
|
+
/** Must match the bound cookie's `Set-Cookie` attributes, without `Max-Age`/`Expires`. */
|
|
14
|
+
attributes: string;
|
|
15
|
+
}
|
|
16
|
+
/** The session config JSON returned from registration and refresh (W3C DBSC §9.6). */
|
|
17
|
+
interface DbscSessionConfig {
|
|
18
|
+
session_identifier: string;
|
|
19
|
+
refresh_url: string;
|
|
20
|
+
scope: {
|
|
21
|
+
origin?: string;
|
|
22
|
+
include_site: boolean;
|
|
23
|
+
scope_specification?: DbscScopeRule[];
|
|
24
|
+
};
|
|
25
|
+
credentials: DbscCredential[];
|
|
26
|
+
allowed_refresh_initiators?: string[];
|
|
27
|
+
}
|
|
28
|
+
/** The body that tells the browser to drop the DBSC session. */
|
|
29
|
+
declare const DBSC_TERMINATE: {
|
|
30
|
+
readonly continue: false;
|
|
31
|
+
};
|
|
32
|
+
interface DbscCookieAttributes {
|
|
33
|
+
domain?: string | undefined;
|
|
34
|
+
path?: string | undefined;
|
|
35
|
+
secure?: boolean | undefined;
|
|
36
|
+
httpOnly?: boolean | undefined;
|
|
37
|
+
sameSite?: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Serializes cookie attributes in the form DBSC compares against `Set-Cookie`:
|
|
41
|
+
* `Domain=…; Path=/; HttpOnly; Secure; SameSite=Lax`. `Max-Age` and `Expires` are
|
|
42
|
+
* left out on purpose; they change on every refresh.
|
|
43
|
+
*/
|
|
44
|
+
declare function formatCookieAttributes(attributes: DbscCookieAttributes): string;
|
|
45
|
+
type DbscAlgorithm = "ES256" | "RS256";
|
|
46
|
+
declare const DBSC_ALGORITHMS: readonly DbscAlgorithm[];
|
|
47
|
+
/** A device public key as stored after registration: a public JWK with `alg` set. */
|
|
48
|
+
type DbscPublicKey = JWK & {
|
|
49
|
+
alg: DbscAlgorithm;
|
|
50
|
+
};
|
|
51
|
+
interface DbscProofClaims {
|
|
52
|
+
jti: string;
|
|
53
|
+
aud?: string | string[];
|
|
54
|
+
iat?: number | string;
|
|
55
|
+
authorization?: string;
|
|
56
|
+
[claim: string]: unknown;
|
|
57
|
+
}
|
|
58
|
+
interface BaseVerifyInput {
|
|
59
|
+
/** The challenge the server issued. The proof's `jti` must equal it. */
|
|
60
|
+
challenge: string;
|
|
61
|
+
/**
|
|
62
|
+
* Origin the proof is for, e.g. `https://app.example.com`. When the JWT carries `aud`,
|
|
63
|
+
* every audience must be a URL on this origin. Omit to skip the audience check.
|
|
64
|
+
*/
|
|
65
|
+
expectedOrigin?: string;
|
|
66
|
+
}
|
|
67
|
+
interface VerifyRegistrationProofInput extends BaseVerifyInput {
|
|
68
|
+
/** Algorithms the server offered in `Secure-Session-Registration`. */
|
|
69
|
+
algorithms?: readonly DbscAlgorithm[];
|
|
70
|
+
/** Expected `authorization` claim, when the registration header carried one. */
|
|
71
|
+
authorization?: string;
|
|
72
|
+
}
|
|
73
|
+
interface VerifyRegistrationProofResult {
|
|
74
|
+
publicKey: DbscPublicKey;
|
|
75
|
+
/** RFC 7638 thumbprint of `publicKey`, for comparing keys. */
|
|
76
|
+
thumbprint: string;
|
|
77
|
+
algorithm: DbscAlgorithm;
|
|
78
|
+
claims: DbscProofClaims;
|
|
79
|
+
}
|
|
80
|
+
interface VerifyRefreshProofInput extends BaseVerifyInput {
|
|
81
|
+
/** The key bound at registration. */
|
|
82
|
+
publicKey: DbscPublicKey;
|
|
83
|
+
}
|
|
84
|
+
interface VerifyRefreshProofResult {
|
|
85
|
+
claims: DbscProofClaims;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Verifies the registration proof the browser sends in `Secure-Session-Response`.
|
|
89
|
+
* The JWT is self-signed by the new device key, which travels in the JOSE header as
|
|
90
|
+
* `jwk` (current spec) or in the payload as `jwk` / `key` (earlier Chrome builds).
|
|
91
|
+
*/
|
|
92
|
+
declare function verifyRegistrationProof(jwt: string, input: VerifyRegistrationProofInput): Promise<VerifyRegistrationProofResult>;
|
|
93
|
+
/** Verifies a refresh proof against the key bound at registration. */
|
|
94
|
+
declare function verifyRefreshProof(jwt: string, input: VerifyRefreshProofInput): Promise<VerifyRefreshProofResult>;
|
|
95
|
+
/**
|
|
96
|
+
* The proof JWT from a `Secure-Session-Response` value: an sf-string, or a bare JWT as
|
|
97
|
+
* earlier drafts sent it. Parameters are ignored.
|
|
98
|
+
*/
|
|
99
|
+
declare function parseProofHeader(header: string | null | undefined): string | null;
|
|
100
|
+
/** The proof's `jti`, read without verifying anything. Use it only to find the challenge. */
|
|
101
|
+
declare function readUnverifiedChallenge(jwt: string): string | null;
|
|
102
|
+
/** Parses a stored public key, or returns `null` when it is missing or not a DBSC key. */
|
|
103
|
+
declare function parseStoredPublicKey(value: unknown): DbscPublicKey | null;
|
|
104
|
+
/**
|
|
105
|
+
* Which response header names to send.
|
|
106
|
+
*
|
|
107
|
+
* - `"standard"`: `Secure-Session-Registration` / `Secure-Session-Challenge`, as in the
|
|
108
|
+
* W3C draft and shipping Chrome.
|
|
109
|
+
* - `"legacy"`: `Sec-Session-Registration` / `Sec-Session-Challenge`, from the original
|
|
110
|
+
* Chrome origin trial.
|
|
111
|
+
* - `"both"`: send both. A browser registers once because the challenge is single-use.
|
|
112
|
+
*/
|
|
113
|
+
type DbscHeaderNames = "standard" | "legacy" | "both";
|
|
114
|
+
declare const DBSC_HEADERS: {
|
|
115
|
+
readonly registration: {
|
|
116
|
+
readonly standard: "Secure-Session-Registration";
|
|
117
|
+
readonly legacy: "Sec-Session-Registration";
|
|
118
|
+
};
|
|
119
|
+
readonly challenge: {
|
|
120
|
+
readonly standard: "Secure-Session-Challenge";
|
|
121
|
+
readonly legacy: "Sec-Session-Challenge";
|
|
122
|
+
};
|
|
123
|
+
readonly response: {
|
|
124
|
+
readonly standard: "Secure-Session-Response";
|
|
125
|
+
readonly legacy: "Sec-Session-Response";
|
|
126
|
+
};
|
|
127
|
+
readonly sessionId: {
|
|
128
|
+
readonly standard: "Sec-Secure-Session-Id";
|
|
129
|
+
readonly legacy: "Sec-Session-Id";
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
type HeaderKind = keyof typeof DBSC_HEADERS;
|
|
133
|
+
/** Response header names to write for `kind`, in send order. */
|
|
134
|
+
declare function getDbscHeaderNames(kind: HeaderKind, names: DbscHeaderNames): string[];
|
|
135
|
+
/** Reads a request header, preferring the standard name over the legacy one. */
|
|
136
|
+
declare function readDbscHeader(headers: Headers, kind: HeaderKind): string | null;
|
|
137
|
+
interface DbscRegistrationHeaderInput {
|
|
138
|
+
algorithms: readonly DbscAlgorithm[];
|
|
139
|
+
challenge: string;
|
|
140
|
+
/** Registration endpoint, absolute path or URL. */
|
|
141
|
+
path: string;
|
|
142
|
+
/** Opaque value the browser copies into the registration JWT as `authorization`. */
|
|
143
|
+
authorization?: string;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Formats a `Secure-Session-Registration` value (RFC 8941 structured field):
|
|
147
|
+
* `(ES256 RS256);path="/api/auth/dbsc/register";challenge="..."`.
|
|
148
|
+
*/
|
|
149
|
+
declare function formatRegistrationHeader(input: DbscRegistrationHeaderInput): string;
|
|
150
|
+
interface DbscChallengeHeaderInput {
|
|
151
|
+
challenge: string;
|
|
152
|
+
sessionId: string;
|
|
153
|
+
}
|
|
154
|
+
/** Formats a `Secure-Session-Challenge` value: `"<challenge>";id="<session id>"`. */
|
|
155
|
+
declare function formatChallengeHeader(input: DbscChallengeHeaderInput): string;
|
|
156
|
+
/**
|
|
157
|
+
* Reads an RFC 8941 sf-string such as the `Sec-Secure-Session-Id` value. Bare tokens are
|
|
158
|
+
* accepted too, since older Chrome builds sent the id unquoted.
|
|
159
|
+
*/
|
|
160
|
+
declare function parseSfString(value: string | null): string | null;
|
|
161
|
+
export { DBSC_TERMINATE as C, DbscSessionConfig as D, DbscScopeRule as E, formatCookieAttributes as O, verifyRegistrationProof as S, DbscCredential as T, VerifyRegistrationProofResult as _, formatChallengeHeader as a, readUnverifiedChallenge as b, parseSfString as c, DbscAlgorithm as d, DbscProofClaims as f, VerifyRegistrationProofInput as g, VerifyRefreshProofResult as h, DbscRegistrationHeaderInput as i, readDbscHeader as l, VerifyRefreshProofInput as m, DbscChallengeHeaderInput as n, formatRegistrationHeader as o, DbscPublicKey as p, DbscHeaderNames as r, getDbscHeaderNames as s, DBSC_HEADERS as t, DBSC_ALGORITHMS as u, parseProofHeader as v, DbscCookieAttributes as w, verifyRefreshProof as x, parseStoredPublicKey as y };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import "./headers-KXqmHcXY.js";
|
|
2
|
+
type DbscErrorCode = "MALFORMED_PROOF" | "UNSUPPORTED_ALGORITHM" | "UNSUPPORTED_TYPE" | "INVALID_PUBLIC_KEY" | "INVALID_SIGNATURE" | "CHALLENGE_MISMATCH" | "AUTHORIZATION_MISMATCH" | "AUDIENCE_MISMATCH";
|
|
3
|
+
/**
|
|
4
|
+
* Thrown when a DBSC proof (`Secure-Session-Response` JWT) cannot be accepted.
|
|
5
|
+
* `code` is stable across releases; `message` says what to fix.
|
|
6
|
+
*/
|
|
7
|
+
declare class DbscError extends Error {
|
|
8
|
+
readonly code: DbscErrorCode;
|
|
9
|
+
constructor(code: DbscErrorCode, message: string, options?: {
|
|
10
|
+
cause?: unknown;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
declare function isDbscError(error: unknown): error is DbscError;
|
|
14
|
+
/** What a challenge is for. A registration challenge never answers a refresh, and the reverse. */
|
|
15
|
+
type DbscChallengePurpose = "register" | "refresh";
|
|
16
|
+
interface CreateDbscChallengeInput {
|
|
17
|
+
secret: string;
|
|
18
|
+
purpose: DbscChallengePurpose;
|
|
19
|
+
sessionToken: string;
|
|
20
|
+
nowMs: number;
|
|
21
|
+
}
|
|
22
|
+
/** A challenge the browser signs as its proof's `jti`: `<issued ms>.<nonce>.<mac>`. */
|
|
23
|
+
declare function createDbscChallenge(input: CreateDbscChallengeInput): Promise<string>;
|
|
24
|
+
interface ReadDbscChallengeInput {
|
|
25
|
+
secret: string;
|
|
26
|
+
purpose: DbscChallengePurpose;
|
|
27
|
+
sessionToken: string;
|
|
28
|
+
challenge: unknown;
|
|
29
|
+
nowMs: number;
|
|
30
|
+
ttlSeconds: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* When the challenge was issued, or `null` when it was not made by this server for this
|
|
34
|
+
* purpose and session, or is older than `ttlSeconds`. Up to 30 seconds of clock skew
|
|
35
|
+
* between instances is tolerated.
|
|
36
|
+
*/
|
|
37
|
+
declare function readDbscChallenge(input: ReadDbscChallengeInput): Promise<{
|
|
38
|
+
issuedAtMs: number;
|
|
39
|
+
} | null>;
|
|
40
|
+
interface CreateBoundTokenInput {
|
|
41
|
+
secret: string;
|
|
42
|
+
sessionToken: string;
|
|
43
|
+
expiresAtMs: number;
|
|
44
|
+
/**
|
|
45
|
+
* The nonce of the fresh-proof challenge the browser held when its key signed the
|
|
46
|
+
* refresh that minted this cookie, if any. Only a cookie that names a challenge answers
|
|
47
|
+
* it, so whether a cookie came after a challenge never rests on comparing clocks.
|
|
48
|
+
*/
|
|
49
|
+
answers?: string | null;
|
|
50
|
+
}
|
|
51
|
+
/** The bound cookie's value: `<expires s>.<answered nonce or ->.<nonce>.<mac>`. */
|
|
52
|
+
declare function createBoundToken(input: CreateBoundTokenInput): Promise<string>;
|
|
53
|
+
interface ReadBoundTokenInput {
|
|
54
|
+
secret: string;
|
|
55
|
+
sessionToken: string;
|
|
56
|
+
token: string | null | undefined;
|
|
57
|
+
nowMs: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The cookie's expiry and the fresh-proof challenge it answers, or `null` unless it is
|
|
61
|
+
* live and minted for this session.
|
|
62
|
+
*/
|
|
63
|
+
declare function readBoundToken(input: ReadBoundTokenInput): Promise<{
|
|
64
|
+
expiresAtMs: number;
|
|
65
|
+
answers: string | null;
|
|
66
|
+
} | null>;
|
|
67
|
+
interface CreateFreshProofTokenInput {
|
|
68
|
+
secret: string;
|
|
69
|
+
sessionToken: string;
|
|
70
|
+
action: string;
|
|
71
|
+
nowMs: number;
|
|
72
|
+
}
|
|
73
|
+
/** A fresh-proof challenge for one action: `<issued ms>.<nonce>.<mac>`. */
|
|
74
|
+
declare function createFreshProofToken(input: CreateFreshProofTokenInput): Promise<string>;
|
|
75
|
+
interface ReadFreshProofTokenInput extends Omit<CreateFreshProofTokenInput, "nowMs"> {
|
|
76
|
+
token: string | null | undefined;
|
|
77
|
+
nowMs: number;
|
|
78
|
+
ttlSeconds: number;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* When the fresh-proof challenge was issued and its nonce, or `null` unless it is live and
|
|
82
|
+
* for this action.
|
|
83
|
+
*/
|
|
84
|
+
declare function readFreshProofToken(input: ReadFreshProofTokenInput): Promise<{
|
|
85
|
+
issuedAtMs: number;
|
|
86
|
+
nonce: string;
|
|
87
|
+
} | null>;
|
|
88
|
+
/**
|
|
89
|
+
* The nonce in a fresh-proof challenge, unverified, for `createBoundToken`'s `answers`.
|
|
90
|
+
* A refresh doesn't know the action the challenge is for, so it can't check the MAC; the
|
|
91
|
+
* answer is checked in full when the bound cookie is compared against the challenge.
|
|
92
|
+
*/
|
|
93
|
+
declare function readFreshProofNonce(token: string | null | undefined): string | null;
|
|
94
|
+
export { isDbscError as _, ReadBoundTokenInput as a, createBoundToken as c, readBoundToken as d, readDbscChallenge as f, DbscErrorCode as g, DbscError as h, DbscChallengePurpose as i, createDbscChallenge as l, readFreshProofToken as m, CreateDbscChallengeInput as n, ReadDbscChallengeInput as o, readFreshProofNonce as p, CreateFreshProofTokenInput as r, ReadFreshProofTokenInput as s, CreateBoundTokenInput as t, createFreshProofToken as u };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { C as DBSC_TERMINATE, D as DbscSessionConfig, E as DbscScopeRule, O as formatCookieAttributes, S as verifyRegistrationProof, T as DbscCredential, _ as VerifyRegistrationProofResult, a as formatChallengeHeader, b as readUnverifiedChallenge, c as parseSfString, d as DbscAlgorithm, f as DbscProofClaims, g as VerifyRegistrationProofInput, h as VerifyRefreshProofResult, i as DbscRegistrationHeaderInput, l as readDbscHeader, m as VerifyRefreshProofInput, n as DbscChallengeHeaderInput, o as formatRegistrationHeader, p as DbscPublicKey, r as DbscHeaderNames, s as getDbscHeaderNames, t as DBSC_HEADERS, u as DBSC_ALGORITHMS, v as parseProofHeader, w as DbscCookieAttributes, x as verifyRefreshProof, y as parseStoredPublicKey } from "./headers-KXqmHcXY.js";
|
|
2
|
+
import { C as dbscSchema, S as createMemoryDbscStore, _ as DbscBinding, a as DbscEvent, b as DbscStoreAdapter, c as DbscRefusalReason, d as FreshProofState, f as ResolvedDbscOptions, g as DBSC_MODEL, h as DBSC_ERROR_CODES, i as DbscCarrier, l as DbscStatusState, m as resolveDbscOptions, n as DBSC_REFRESH_PATH, o as DbscFreshProofRule, p as dbsc, r as DBSC_REGISTER_PATH, s as DbscOptions, t as DBSC_COOKIE_KEYS, u as DbscUnboundPolicy, v as DbscProofKind, x as createAdapterDbscStore, y as DbscStore } from "./dbsc-plugin-D9MKOgZP.js";
|
|
3
|
+
import { _ as isDbscError, a as ReadBoundTokenInput, c as createBoundToken, d as readBoundToken, f as readDbscChallenge, g as DbscErrorCode, h as DbscError, i as DbscChallengePurpose, l as createDbscChallenge, m as readFreshProofToken, n as CreateDbscChallengeInput, o as ReadDbscChallengeInput, p as readFreshProofNonce, r as CreateFreshProofTokenInput, s as ReadFreshProofTokenInput, t as CreateBoundTokenInput, u as createFreshProofToken } from "./index-s7R-j-qI.js";
|
|
4
|
+
export { type CreateBoundTokenInput, type CreateDbscChallengeInput, type CreateFreshProofTokenInput, DBSC_ALGORITHMS, DBSC_COOKIE_KEYS, DBSC_ERROR_CODES, DBSC_HEADERS, DBSC_MODEL, DBSC_REFRESH_PATH, DBSC_REGISTER_PATH, DBSC_TERMINATE, type DbscAlgorithm, type DbscBinding, type DbscCarrier, type DbscChallengeHeaderInput, type DbscChallengePurpose, type DbscCookieAttributes, type DbscCredential, DbscError, type DbscErrorCode, type DbscEvent, type DbscFreshProofRule, type DbscHeaderNames, type DbscOptions, type DbscProofClaims, type DbscProofKind, type DbscPublicKey, type DbscRefusalReason, type DbscRegistrationHeaderInput, type DbscScopeRule, type DbscSessionConfig, type DbscStatusState, type DbscStore, type DbscStoreAdapter, type DbscUnboundPolicy, type FreshProofState, type ReadBoundTokenInput, type ReadDbscChallengeInput, type ReadFreshProofTokenInput, type ResolvedDbscOptions, type VerifyRefreshProofInput, type VerifyRefreshProofResult, type VerifyRegistrationProofInput, type VerifyRegistrationProofResult, createAdapterDbscStore, createBoundToken, createDbscChallenge, createFreshProofToken, createMemoryDbscStore, dbsc, dbscSchema, formatChallengeHeader, formatCookieAttributes, formatRegistrationHeader, getDbscHeaderNames, isDbscError, parseProofHeader, parseSfString, parseStoredPublicKey, readBoundToken, readDbscChallenge, readDbscHeader, readFreshProofNonce, readFreshProofToken, readUnverifiedChallenge, resolveDbscOptions, verifyRefreshProof, verifyRegistrationProof };
|