@runlot/auth 0.1.0-rc.100

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,41 @@
1
+ import { type User } from "./index.js";
2
+ export interface NextOpts {
3
+ next?: string;
4
+ }
5
+ /** The social providers a project can register (docs/auth.md §9). */
6
+ export type AuthProvider = "github" | "google" | "kakao";
7
+ /** The sign-in page. Given a provider, it goes straight to that provider's
8
+ * authorize. */
9
+ export declare function signInUrl(provider?: AuthProvider, opts?: NextOpts): string;
10
+ /**
11
+ * The sign-up page.
12
+ *
13
+ * **There is no provider argument.** For social logins, signing up and signing
14
+ * in are one path (docs/auth.md §5 — there is only `GET sign-in/{provider}`,
15
+ * and the identity upsert rides the linking rules of §6), so a URL like
16
+ * `signUpUrl("github")` does not exist on the server. To sign somebody up with
17
+ * a social provider, use `signInUrl(provider)`.
18
+ */
19
+ export declare function signUpUrl(opts?: NextOpts): string;
20
+ /**
21
+ * Sign-out.
22
+ *
23
+ * **It is a POST target, not a link** (docs/auth.md §5). Use it as `<form
24
+ * method="post" action={signOutUrl()}>` — if a GET could sign somebody out, a
25
+ * single `<img>` embedded in somebody else's page could throw the user out.
26
+ */
27
+ export declare function signOutUrl(opts?: NextOpts): string;
28
+ /**
29
+ * Who is signed in right now. This is what a client-rendered app calls while
30
+ * booting.
31
+ *
32
+ * Anything other than a 200 gives `{ user: null }` — being signed out is an
33
+ * answer, not an exception (a 401 is an answer too), and throwing would turn
34
+ * every page into a try/catch.
35
+ *
36
+ * `fetchImpl` is the hole through which the tests inject a fake.
37
+ */
38
+ export declare function session(fetchImpl?: typeof fetch): Promise<{
39
+ user: User | null;
40
+ }>;
41
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAa,KAAK,IAAI,EAAE,MAAM,YAAY,CAAC;AAElD,MAAM,WAAW,QAAQ;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,qEAAqE;AACrE,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAC;AAsBzD;iBACiB;AACjB,wBAAgB,SAAS,CAAC,QAAQ,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAE1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAEjD;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAElD;AAED;;;;;;;;;GASG;AACH,wBAAsB,OAAO,CAAC,SAAS,CAAC,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CAAA;CAAE,CAAC,CAKtF"}
package/dist/client.js ADDED
@@ -0,0 +1,70 @@
1
+ // Browser helpers (docs/auth.md §10). Building links and one `GET /session` is
2
+ // all there is, so it has zero dependencies, and it does not claim to know
3
+ // again what the server already knows.
4
+ import { AUTH_BASE } from "./index.js";
5
+ // `next` lets through only a same-origin relative path — the same rule as
6
+ // `safeNext` in docs/auth.md §5. It has to start with `/` and must not start
7
+ // with `//`: the browser reads `//evil.example` as an absolute URL, which makes
8
+ // it an open redirect.
9
+ //
10
+ // Filtering here is a convenience, not a security boundary. A link can also be
11
+ // made by hand, so the server runs the same check again.
12
+ function safeNext(next) {
13
+ if (next === undefined || next === "")
14
+ return null;
15
+ if (!next.startsWith("/"))
16
+ return null;
17
+ if (next.startsWith("//"))
18
+ return null;
19
+ return next;
20
+ }
21
+ function authUrl(path, provider, opts) {
22
+ const base = `${AUTH_BASE}/${path}${provider ? `/${provider}` : ""}`;
23
+ const next = safeNext(opts?.next);
24
+ return next === null ? base : `${base}?next=${encodeURIComponent(next)}`;
25
+ }
26
+ /** The sign-in page. Given a provider, it goes straight to that provider's
27
+ * authorize. */
28
+ export function signInUrl(provider, opts) {
29
+ return authUrl("sign-in", provider, opts);
30
+ }
31
+ /**
32
+ * The sign-up page.
33
+ *
34
+ * **There is no provider argument.** For social logins, signing up and signing
35
+ * in are one path (docs/auth.md §5 — there is only `GET sign-in/{provider}`,
36
+ * and the identity upsert rides the linking rules of §6), so a URL like
37
+ * `signUpUrl("github")` does not exist on the server. To sign somebody up with
38
+ * a social provider, use `signInUrl(provider)`.
39
+ */
40
+ export function signUpUrl(opts) {
41
+ return authUrl("sign-up", undefined, opts);
42
+ }
43
+ /**
44
+ * Sign-out.
45
+ *
46
+ * **It is a POST target, not a link** (docs/auth.md §5). Use it as `<form
47
+ * method="post" action={signOutUrl()}>` — if a GET could sign somebody out, a
48
+ * single `<img>` embedded in somebody else's page could throw the user out.
49
+ */
50
+ export function signOutUrl(opts) {
51
+ return authUrl("sign-out", undefined, opts);
52
+ }
53
+ /**
54
+ * Who is signed in right now. This is what a client-rendered app calls while
55
+ * booting.
56
+ *
57
+ * Anything other than a 200 gives `{ user: null }` — being signed out is an
58
+ * answer, not an exception (a 401 is an answer too), and throwing would turn
59
+ * every page into a try/catch.
60
+ *
61
+ * `fetchImpl` is the hole through which the tests inject a fake.
62
+ */
63
+ export async function session(fetchImpl) {
64
+ const f = fetchImpl ?? fetch;
65
+ const res = await f(`${AUTH_BASE}/session`, { credentials: "same-origin" });
66
+ if (!res.ok)
67
+ return { user: null };
68
+ return (await res.json());
69
+ }
70
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,2EAA2E;AAC3E,uCAAuC;AAEvC,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AASlD,0EAA0E;AAC1E,6EAA6E;AAC7E,gFAAgF;AAChF,uBAAuB;AACvB,EAAE;AACF,+EAA+E;AAC/E,yDAAyD;AACzD,SAAS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AAM,CAAC,CAAC,GAAG,SAAS,UAAU,EAAE,EAAE,WAAW,EAAE,aAAa,EAAE,CAAC,CAAC;IAC5E,IAAI,CAAC,GAAG,CAAC,EAAE;QAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACnC,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAA0B,CAAC;AACrD,CAAC"}
@@ -0,0 +1,97 @@
1
+ /** The path prefix the system worker terminates (docs/auth.md §5). */
2
+ export declare const AUTH_BASE = "/__runlot/auth";
3
+ export interface User {
4
+ /** uuid — the PK of `runlot_auth.users` and the value RLS looks at. */
5
+ id: string;
6
+ email: string | null;
7
+ emailVerified: boolean;
8
+ name: string | null;
9
+ avatarUrl: string | null;
10
+ /** The sign-in methods linked to this user. For example:
11
+ * `["password", "github"]`. */
12
+ providers: string[];
13
+ /** ISO 8601. */
14
+ createdAt: string;
15
+ /** ISO 8601. `null` if they have never signed in. */
16
+ lastSignInAt: string | null;
17
+ }
18
+ /**
19
+ * The one deletion-cookie line `signOut` returns. Used as a `HeadersInit` as
20
+ * is.
21
+ */
22
+ export interface SignOutHeaders {
23
+ "set-cookie": string;
24
+ }
25
+ /** One page of `users.list`. A `null` `cursor` means it is the last page. */
26
+ export interface UserPage {
27
+ users: User[];
28
+ cursor: string | null;
29
+ }
30
+ export interface UserUpdate {
31
+ name?: string | null;
32
+ avatarUrl?: string | null;
33
+ /** `true` also revokes every session of that user (docs/auth.md §4). */
34
+ banned?: boolean;
35
+ }
36
+ /** Administrative reads and writes. They do not go through the gateway; they
37
+ * read directly through the `DB` binding. */
38
+ export interface AuthUsers {
39
+ get(id: string): Promise<User | null>;
40
+ getByEmail(email: string): Promise<User | null>;
41
+ list(opts?: {
42
+ cursor?: string;
43
+ limit?: number;
44
+ }): Promise<UserPage>;
45
+ update(id: string, patch: UserUpdate): Promise<User>;
46
+ /** The user, the identities, the sessions and the tokens. The app's own
47
+ * tables are joined up by the app with `ON DELETE`. */
48
+ delete(id: string): Promise<void>;
49
+ }
50
+ export interface AuthSessions {
51
+ /** The idiom after a password change. */
52
+ revokeAll(userId: string): Promise<void>;
53
+ }
54
+ export interface AuthBinding {
55
+ /**
56
+ * Cookie → session → user. `null` when there is none.
57
+ * **It does not throw** — not being signed in is not an error.
58
+ */
59
+ user(request: Request): Promise<User | null>;
60
+ /**
61
+ * Erases that session and returns the deletion cookie. Putting it on the
62
+ * response is the caller's job.
63
+ *
64
+ * ```ts
65
+ * return new Response(null, { headers: await env.auth.signOut(request) });
66
+ * ```
67
+ *
68
+ * It is a plain object rather than a `Headers` because of a measurement —
69
+ * `Headers` cannot cross the RPC boundary (docs/auth.md §4). It is still a
70
+ * `HeadersInit`, so it goes on exactly as above.
71
+ */
72
+ signOut(request: Request): Promise<SignOutHeaders>;
73
+ users: AuthUsers;
74
+ sessions: AuthSessions;
75
+ getUser?(id: string): Promise<User | null>;
76
+ getUserByEmail?(email: string): Promise<User | null>;
77
+ listUsers?(opts?: {
78
+ cursor?: string;
79
+ limit?: number;
80
+ }): Promise<UserPage>;
81
+ updateUser?(id: string, patch: UserUpdate): Promise<User>;
82
+ deleteUser?(id: string): Promise<void>;
83
+ revokeSessions?(userId: string): Promise<void>;
84
+ }
85
+ /**
86
+ * The fragment to attach to your own `Env`.
87
+ *
88
+ * ```ts
89
+ * interface Env extends AuthEnv {
90
+ * db: Fetcher;
91
+ * }
92
+ * ```
93
+ */
94
+ export interface AuthEnv {
95
+ auth: AuthBinding;
96
+ }
97
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAeA,sEAAsE;AACtE,eAAO,MAAM,SAAS,mBAAmB,CAAC;AAE1C,MAAM,WAAW,IAAI;IACnB,uEAAuE;IACvE,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,aAAa,EAAE,OAAO,CAAC;IACvB,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AG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C;AAED,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAC7C;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IACnD,KAAK,EAAE,SAAS,CAAC;IACjB,QAAQ,EAAE,YAAY,CAAC;IAOvB,OAAO,CAAC,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IAC3C,cAAc,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IACrD,SAAS,CAAC,CAAC,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1E,UAAU,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1D,UAAU,CAAC,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,cAAc,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAChD;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,WAAW,CAAC;CACnB"}
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ // The types for `env.auth` (docs/auth.md §4, §10).
2
+ //
3
+ // This package ships no runtime. The binding is injected by the auth system
4
+ // worker inside workerd, so all a user gets from npm is the type to attach to
5
+ // their own `Env` and the helpers called from the browser
6
+ // (`@runlot/auth/client`). That is also why it has zero dependencies — it must
7
+ // not add a single byte to the worker bundle.
8
+ //
9
+ // `user(request)` taking a request is a constraint, not a taste (the same
10
+ // argument as docs/access.md §4.2): `env` is a bundle fixed for the lifetime of
11
+ // the worker, and the identity is a different value on every request. The
12
+ // argument-free shape only holds inside round 6's Next.js adapter, which hides
13
+ // the request in `AsyncLocalStorage`; anywhere else it is a `TypeError` — if
14
+ // "cannot be done" quietly became `null`, every page would look logged out.
15
+ /** The path prefix the system worker terminates (docs/auth.md §5). */
16
+ export const AUTH_BASE = "/__runlot/auth";
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,0DAA0D;AAC1D,+EAA+E;AAC/E,8CAA8C;AAC9C,EAAE;AACF,0EAA0E;AAC1E,gFAAgF;AAChF,0EAA0E;AAC1E,+EAA+E;AAC/E,6EAA6E;AAC7E,4EAA4E;AAE5E,sEAAsE;AACtE,MAAM,CAAC,MAAM,SAAS,GAAG,gBAAgB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@runlot/auth",
3
+ "version": "0.1.0-rc.100",
4
+ "description": "Types and browser helpers for env.auth — Runlot provides the runtime",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/runlot/runlot",
11
+ "directory": "web/packages/auth"
12
+ },
13
+ "type": "module",
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./client": {
22
+ "types": "./dist/client.d.ts",
23
+ "default": "./dist/client.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "devDependencies": {
30
+ "@types/node": "^22.10.2",
31
+ "esbuild": "^0.24.2",
32
+ "typescript": "^5.7.2",
33
+ "@runlot/config": "0.1.0-rc.100"
34
+ },
35
+ "scripts": {
36
+ "test": "node build.mjs",
37
+ "typecheck": "tsc --noEmit",
38
+ "build": "tsc -p tsconfig.build.json"
39
+ }
40
+ }