@jgb-solutions/kit 0.5.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 +9 -0
- package/README.md +21 -1
- package/dist/auth/browser.d.ts +74 -0
- package/dist/auth/browser.js +83 -0
- package/dist/auth/expo.d.ts +29 -0
- package/dist/auth/expo.js +20 -1
- package/package.json +7 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `@jgb-solutions/kit`. Versions follow semver.
|
|
4
4
|
|
|
5
|
+
## 0.6.0
|
|
6
|
+
|
|
7
|
+
- `kit/auth/browser` (new, no dependencies):
|
|
8
|
+
- `passkeyClient({ webauthn })`: passkey sign-in and adding a passkey against Better Auth's endpoints, with the app's `@simplewebauthn/browser` passed in. Answers are codes (`CANCELLED`, `NETWORK`, `INVALID_OPTIONS`, or Better Auth's own).
|
|
9
|
+
- `createPendingAction<Kind>()`: what a signed-out visitor tapped, kept in this tab until sign-in.
|
|
10
|
+
- `afterSignIn(next, done)` adds the `#done=` flash.
|
|
11
|
+
- `readSignedInFlag(name)` reads the signed-in flag cookie.
|
|
12
|
+
- `kit/auth/expo`: `googleIdToken(module, webClientId)`, the ID token from the phone's Google account sheet (the app's native Credential Manager module). Errors are mapped to `CANCELLED`, `NO_GOOGLE_ACCOUNT`, `GOOGLE_UNAVAILABLE` or `GOOGLE_FAILED` (with a detail for the error tracker).
|
|
13
|
+
|
|
5
14
|
## 0.5.0
|
|
6
15
|
|
|
7
16
|
The account logic every app copied, now in the kit. Nothing existing changes.
|
package/README.md
CHANGED
|
@@ -146,6 +146,24 @@ CDN cache keys ignore cookies, so a signed-in page stored once reaches the next
|
|
|
146
146
|
|
|
147
147
|
`redisCounterStore(redis, 'app:')` is a counter store for `counterRateLimit`, with any client that has `incr` and `expire` (Bun's `RedisClient`, ioredis). For oRPC's Redis limiter, `evalWithFallback(() => redis.send('EVAL', …))` answers `RATE_LIMIT_SCRIPT_FALLBACK` (`[1, 0, 0]`: allowed, nothing counted) while the store is away. From oRPC 2.0.0-beta.53 the old `[0, 0]` reads as refused, and every call failed.
|
|
148
148
|
|
|
149
|
+
#### `kit/auth/browser`: passkeys and pending actions in the website
|
|
150
|
+
|
|
151
|
+
No dependencies: the app passes `@simplewebauthn/browser` in, loaded only when a passkey button is pressed.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { afterSignIn, createPendingAction, passkeyClient, readSignedInFlag } from '@jgb-solutions/kit/auth/browser'
|
|
155
|
+
|
|
156
|
+
const passkeys = passkeyClient({ webauthn: () => import('@simplewebauthn/browser') }) // Better Auth's /api/auth/passkey
|
|
157
|
+
await passkeys.signIn() // { ok: true } or { ok: false, code } (CANCELLED, NETWORK, Better Auth's codes)
|
|
158
|
+
await passkeys.add('My laptop')
|
|
159
|
+
|
|
160
|
+
export const pending = createPendingAction<'favorite' | 'follow'>() // what a signed-out visitor tapped, in this tab only
|
|
161
|
+
pending.set({ kind: 'favorite', name: song.title, run: () => addFavorite(song.id) })
|
|
162
|
+
const done = await pending.finish('signed-in') // after sign-in: runs it, answers its kind
|
|
163
|
+
location.assign(afterSignIn(redirect, done)) // …#done=favorite for the confirmation
|
|
164
|
+
readSignedInFlag('example_in') // the readable flag cookie standardAuthOptions sets (a hint; the server checks)
|
|
165
|
+
```
|
|
166
|
+
|
|
149
167
|
#### `kit/auth/expo`: the mobile app
|
|
150
168
|
|
|
151
169
|
```ts
|
|
@@ -171,6 +189,8 @@ await auth.signInWithPasskey(Passkeys)
|
|
|
171
189
|
new RPCLink({ url, headers: auth.authHeaders }) // Bearer on every RPC call
|
|
172
190
|
```
|
|
173
191
|
|
|
192
|
+
On Android, `googleIdToken(nativeModule, webClientId)` gets the ID token from the phone's Google account sheet (the app's own Credential Manager module: `signIn(serverClientId)` answering `{ idToken }`, rejecting with `ERR_CANCELLED` or `ERR_NO_ACCOUNT`). It answers `{ ok: true, idToken }`, or `CANCELLED`, `NO_GOOGLE_ACCOUNT`, `GOOGLE_UNAVAILABLE` (no module or client ID), or `GOOGLE_FAILED` with a `detail` for the error tracker.
|
|
193
|
+
|
|
174
194
|
Failures come back as codes (`INVALID_USERNAME_OR_PASSWORD`, `ACCOUNT_NOT_LINKED`, `GOOGLE_ALREADY_LINKED`, `TOO_MANY_REQUESTS`, `NETWORK`, `CANCELLED`…), and the app words them. The module has no dependencies: the app passes in its secure storage and passkey modules.
|
|
175
195
|
|
|
176
196
|
### `@jgb-solutions/kit/i18n`: four languages, one set of rules
|
|
@@ -349,7 +369,7 @@ Type: Bricolage Grotesque (700/800) for titles and numbers (`tabular-nums`), Fig
|
|
|
349
369
|
- **Infrastructure details:** hosts, IP addresses, server names, internal URLs, deploy setup.
|
|
350
370
|
- **Personal data, or real addresses outside `example.com`.**
|
|
351
371
|
|
|
352
|
-
`bun run check:package` scans every file that would be published, before each publish and in CI. It fails on an Access team domain other than the examples, on a 32- or 64-character hex ID, on an address outside `example.com`, on any file besides `dist/`, the README, the changelog, the license and `package.json`, and on a module importing more than it may (`kit/access` jose alone; `kit/auth/rules`, `kit/auth/cache`, `kit/auth/redis`, `kit/auth/expo` and `kit/auth/bun` nothing). `bun run check:install` installs the packed package without the optional peers and checks that `kit/access`, `kit/auth/rules` and `kit/auth/expo` load and type-check there.
|
|
372
|
+
`bun run check:package` scans every file that would be published, before each publish and in CI. It fails on an Access team domain other than the examples, on a 32- or 64-character hex ID, on an address outside `example.com`, on any file besides `dist/`, the README, the changelog, the license and `package.json`, and on a module importing more than it may (`kit/access` jose alone; `kit/auth/rules`, `kit/auth/browser`, `kit/auth/cache`, `kit/auth/redis`, `kit/auth/expo` and `kit/auth/bun` nothing). `bun run check:install` installs the packed package without the optional peers and checks that `kit/access`, `kit/auth/rules` and `kit/auth/expo` load and type-check there.
|
|
353
373
|
|
|
354
374
|
## Developing
|
|
355
375
|
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign-in pieces that run in the website's browser, the same in every app: passkeys against Better
|
|
3
|
+
* Auth's own endpoints, the action a visitor tried before signing in, and the signed-in flag. No
|
|
4
|
+
* dependencies: the app passes `@simplewebauthn/browser` in (loaded only when a passkey button is
|
|
5
|
+
* pressed), so no auth library ships to every visitor.
|
|
6
|
+
*
|
|
7
|
+
* import { passkeyClient } from '@jgb-solutions/kit/auth/browser'
|
|
8
|
+
* const passkeys = passkeyClient({ webauthn: () => import('@simplewebauthn/browser') })
|
|
9
|
+
* const result = await passkeys.signIn() // { ok: true } or { ok: false, code }
|
|
10
|
+
*/
|
|
11
|
+
/** What a passkey call answers: done, or a code (Better Auth's, or CANCELLED / NETWORK / INVALID_OPTIONS). */
|
|
12
|
+
export type PasskeyResult = {
|
|
13
|
+
ok: true;
|
|
14
|
+
} | {
|
|
15
|
+
ok: false;
|
|
16
|
+
code: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The two calls of `@simplewebauthn/browser` this module makes. The options come from Better Auth's
|
|
20
|
+
* passkey endpoints, which build them for these very functions.
|
|
21
|
+
*/
|
|
22
|
+
export type WebAuthnBrowser = {
|
|
23
|
+
startAuthentication(input: {
|
|
24
|
+
optionsJSON: never;
|
|
25
|
+
}): Promise<unknown>;
|
|
26
|
+
startRegistration(input: {
|
|
27
|
+
optionsJSON: never;
|
|
28
|
+
}): Promise<unknown>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Passkeys in the browser: the WebAuthn ceremony with the app's `@simplewebauthn/browser`, against
|
|
32
|
+
* Better Auth's endpoints under `base` (same origin, so its session and challenge cookies ride along).
|
|
33
|
+
*/
|
|
34
|
+
export declare function passkeyClient(config: {
|
|
35
|
+
webauthn: () => Promise<WebAuthnBrowser>;
|
|
36
|
+
base?: string;
|
|
37
|
+
fetch?: typeof globalThis.fetch;
|
|
38
|
+
}): {
|
|
39
|
+
/** Signs in with a passkey on this device. On success the session cookie is set: reload or refetch. */
|
|
40
|
+
signIn: () => Promise<PasskeyResult>;
|
|
41
|
+
/** Adds a passkey, named `name`, to the signed-in account. */
|
|
42
|
+
add: (name: string) => Promise<PasskeyResult>;
|
|
43
|
+
};
|
|
44
|
+
/** Something a signed-out visitor tapped (a heart, Follow), to do once they're signed in. */
|
|
45
|
+
export type PendingAction<Kind extends string> = {
|
|
46
|
+
kind: Kind;
|
|
47
|
+
name?: string;
|
|
48
|
+
run: () => Promise<unknown>;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The action a visitor tried before signing in, kept in this tab's memory only: no storage, nothing in
|
|
52
|
+
* the URL. The button sets it and opens sign-in; sign-in finishes it once the person is in. A full
|
|
53
|
+
* reload on the way (a Google round trip) drops it: the visitor is signed in all the same and can tap again.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createPendingAction<Kind extends string>(): {
|
|
56
|
+
set(action: PendingAction<Kind>): void;
|
|
57
|
+
/** The name of what the visitor tapped, for the sign-in notice. */
|
|
58
|
+
name(): string | undefined;
|
|
59
|
+
clear(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Runs the pending action once signed in and answers its kind, for the confirmation. With nothing
|
|
62
|
+
* pending it answers `otherwise` (for example 'signed-in' for a visitor sent from a page that needs
|
|
63
|
+
* an account). A failed action answers undefined: they're signed in and can tap again.
|
|
64
|
+
*/
|
|
65
|
+
finish<Otherwise extends string | undefined = undefined>(otherwise?: Otherwise): Promise<Kind | Otherwise | undefined>;
|
|
66
|
+
};
|
|
67
|
+
/** Where to go after signing in: `next`, with `#done=<kind>` when something was done. */
|
|
68
|
+
export declare function afterSignIn(next: string, done: string | undefined): string;
|
|
69
|
+
/**
|
|
70
|
+
* True when the signed-in flag cookie (`signedInFlag` in `standardAuthOptions`, readable by scripts,
|
|
71
|
+
* unlike the session) says this browser is signed in: a cached page can then fetch the person's own
|
|
72
|
+
* bits without a request for every visitor. A hint only; the server always checks the session.
|
|
73
|
+
*/
|
|
74
|
+
export declare function readSignedInFlag(name: string, cookie?: string): boolean;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/auth/browser.ts
|
|
2
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
|
+
var codeOf = (body, fallback) => isRecord(body) && typeof body.code === "string" && body.code ? body.code : fallback;
|
|
4
|
+
var looksLikeOptions = (body) => isRecord(body) && typeof body.challenge === "string";
|
|
5
|
+
function passkeyClient(config) {
|
|
6
|
+
const base = (config.base ?? "/api/auth/passkey").replace(/\/+$/, "");
|
|
7
|
+
const fetcher = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
8
|
+
async function call(path, body) {
|
|
9
|
+
try {
|
|
10
|
+
const response = await fetcher(`${base}/${path}`, {
|
|
11
|
+
credentials: "same-origin",
|
|
12
|
+
...body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }
|
|
13
|
+
});
|
|
14
|
+
return { ok: response.ok, body: await response.json().catch(() => null) };
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function ceremony(optionsPath, run, verifyPath, extra) {
|
|
20
|
+
const options = await call(optionsPath);
|
|
21
|
+
if (!options)
|
|
22
|
+
return { ok: false, code: "NETWORK" };
|
|
23
|
+
if (!options.ok)
|
|
24
|
+
return { ok: false, code: codeOf(options.body, "FAILED") };
|
|
25
|
+
if (!looksLikeOptions(options.body))
|
|
26
|
+
return { ok: false, code: "INVALID_OPTIONS" };
|
|
27
|
+
let response;
|
|
28
|
+
try {
|
|
29
|
+
response = await run(await config.webauthn(), options.body);
|
|
30
|
+
} catch {
|
|
31
|
+
return { ok: false, code: "CANCELLED" };
|
|
32
|
+
}
|
|
33
|
+
const verified = await call(verifyPath, { response, ...extra });
|
|
34
|
+
if (!verified)
|
|
35
|
+
return { ok: false, code: "NETWORK" };
|
|
36
|
+
return verified.ok ? { ok: true } : { ok: false, code: codeOf(verified.body, "FAILED") };
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
signIn: () => ceremony("generate-authenticate-options", (webauthn, optionsJSON) => webauthn.startAuthentication({ optionsJSON }), "verify-authentication", {}),
|
|
40
|
+
add: (name) => ceremony(`generate-register-options?name=${encodeURIComponent(name)}`, (webauthn, optionsJSON) => webauthn.startRegistration({ optionsJSON }), "verify-registration", { name })
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function createPendingAction() {
|
|
44
|
+
let pending;
|
|
45
|
+
return {
|
|
46
|
+
set(action) {
|
|
47
|
+
pending = action;
|
|
48
|
+
},
|
|
49
|
+
name() {
|
|
50
|
+
return pending?.name;
|
|
51
|
+
},
|
|
52
|
+
clear() {
|
|
53
|
+
pending = undefined;
|
|
54
|
+
},
|
|
55
|
+
async finish(otherwise) {
|
|
56
|
+
const action = pending;
|
|
57
|
+
pending = undefined;
|
|
58
|
+
if (!action)
|
|
59
|
+
return otherwise;
|
|
60
|
+
try {
|
|
61
|
+
await action.run();
|
|
62
|
+
return action.kind;
|
|
63
|
+
} catch {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function afterSignIn(next, done) {
|
|
70
|
+
return done ? `${next.split("#")[0]}#done=${encodeURIComponent(done)}` : next;
|
|
71
|
+
}
|
|
72
|
+
function readSignedInFlag(name, cookie = typeof document === "undefined" ? "" : document.cookie) {
|
|
73
|
+
return cookie.split(";").some((part) => {
|
|
74
|
+
const [key, value] = part.split("=");
|
|
75
|
+
return key?.trim() === name && value?.trim() === "1";
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
afterSignIn,
|
|
80
|
+
createPendingAction,
|
|
81
|
+
passkeyClient,
|
|
82
|
+
readSignedInFlag
|
|
83
|
+
};
|
package/dist/auth/expo.d.ts
CHANGED
|
@@ -83,3 +83,32 @@ export declare function createMobileAuth<U>(config: MobileAuthConfig<U>): {
|
|
|
83
83
|
forget: () => Promise<void>;
|
|
84
84
|
};
|
|
85
85
|
export type MobileAuth<U> = ReturnType<typeof createMobileAuth<U>>;
|
|
86
|
+
/**
|
|
87
|
+
* The native Google account sheet: a module with `signIn(serverClientId)` answering Google's ID token
|
|
88
|
+
* (Android's Credential Manager), rejecting with `error.code` ERR_CANCELLED, ERR_NO_ACCOUNT or another.
|
|
89
|
+
*/
|
|
90
|
+
export type GoogleCredentialModule = {
|
|
91
|
+
signIn(serverClientId: string): Promise<{
|
|
92
|
+
idToken: string;
|
|
93
|
+
}>;
|
|
94
|
+
};
|
|
95
|
+
export type GoogleTokenResult = {
|
|
96
|
+
ok: true;
|
|
97
|
+
idToken: string;
|
|
98
|
+
} | {
|
|
99
|
+
ok: false;
|
|
100
|
+
code: 'CANCELLED' | 'NO_GOOGLE_ACCOUNT' | 'GOOGLE_UNAVAILABLE';
|
|
101
|
+
}
|
|
102
|
+
/** `detail` (the native code and message) is for the error tracker, never for the screen. */
|
|
103
|
+
| {
|
|
104
|
+
ok: false;
|
|
105
|
+
code: 'GOOGLE_FAILED';
|
|
106
|
+
detail: string;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Google's ID token from the phone's account sheet, for `signInWithGoogle` or `linkGoogle`. `module`
|
|
110
|
+
* is the app's native module, or null where the installed app lacks it (iOS, older builds):
|
|
111
|
+
* GOOGLE_UNAVAILABLE then, as without `webClientId` (the website's OAuth client, whose tokens Better
|
|
112
|
+
* Auth accepts). CANCELLED means the person closed the sheet: nothing to say.
|
|
113
|
+
*/
|
|
114
|
+
export declare function googleIdToken(module: GoogleCredentialModule | null | undefined, webClientId: string | null | undefined): Promise<GoogleTokenResult>;
|
package/dist/auth/expo.js
CHANGED
|
@@ -285,6 +285,25 @@ function createMobileAuth(config) {
|
|
|
285
285
|
forget
|
|
286
286
|
};
|
|
287
287
|
}
|
|
288
|
+
async function googleIdToken(module, webClientId) {
|
|
289
|
+
if (!module || !webClientId)
|
|
290
|
+
return { ok: false, code: "GOOGLE_UNAVAILABLE" };
|
|
291
|
+
try {
|
|
292
|
+
const { idToken } = await module.signIn(webClientId);
|
|
293
|
+
if (typeof idToken !== "string" || !idToken)
|
|
294
|
+
return { ok: false, code: "GOOGLE_FAILED", detail: "NO_TOKEN" };
|
|
295
|
+
return { ok: true, idToken };
|
|
296
|
+
} catch (error) {
|
|
297
|
+
const code = typeof error === "object" && error !== null && "code" in error ? String(error.code) : "";
|
|
298
|
+
if (code === "ERR_CANCELLED")
|
|
299
|
+
return { ok: false, code: "CANCELLED" };
|
|
300
|
+
if (code === "ERR_NO_ACCOUNT")
|
|
301
|
+
return { ok: false, code: "NO_GOOGLE_ACCOUNT" };
|
|
302
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
303
|
+
return { ok: false, code: "GOOGLE_FAILED", detail: `${code || "UNKNOWN"}: ${message}` };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
288
306
|
export {
|
|
289
|
-
createMobileAuth
|
|
307
|
+
createMobileAuth,
|
|
308
|
+
googleIdToken
|
|
290
309
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jgb-solutions/kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "One package for JGB Solutions apps, in tree-shakable namespaces: Cloudflare Access, Better Auth (kit/access, kit/auth
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "One package for JGB Solutions apps, in tree-shakable namespaces: Cloudflare Access, Better Auth (kit/access, kit/auth…) and the admin panel kit (kit/admin, React 19). An app bundles only the subpaths it imports.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "JGB Solutions",
|
|
7
7
|
"repository": {
|
|
@@ -69,6 +69,10 @@
|
|
|
69
69
|
"types": "./dist/auth/redis.d.ts",
|
|
70
70
|
"import": "./dist/auth/redis.js"
|
|
71
71
|
},
|
|
72
|
+
"./auth/browser": {
|
|
73
|
+
"types": "./dist/auth/browser.d.ts",
|
|
74
|
+
"import": "./dist/auth/browser.js"
|
|
75
|
+
},
|
|
72
76
|
"./auth/expo": {
|
|
73
77
|
"types": "./dist/auth/expo.d.ts",
|
|
74
78
|
"import": "./dist/auth/expo.js"
|
|
@@ -137,6 +141,7 @@
|
|
|
137
141
|
"@better-auth/passkey": "1.7.29",
|
|
138
142
|
"@orpc/server": "2.0.0-beta.53",
|
|
139
143
|
"@playwright/test": "1.65.1",
|
|
144
|
+
"@simplewebauthn/browser": "13.3.0",
|
|
140
145
|
"@types/bun": "1.4.5",
|
|
141
146
|
"@types/react": "19.3.3",
|
|
142
147
|
"@types/react-dom": "19.3.0",
|