@velocity-astro/cms-auth 0.0.0-stage → 0.1.0-beta.2
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 +12 -2
- package/dist/auth.d.ts +95 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +265 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +44 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +87 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/mailer.d.ts +40 -0
- package/dist/mailer.d.ts.map +1 -0
- package/dist/mailer.js +15 -0
- package/dist/mailer.js.map +1 -0
- package/dist/team.d.ts +13 -0
- package/dist/team.d.ts.map +1 -0
- package/dist/team.js +20 -0
- package/dist/team.js.map +1 -0
- package/package.json +47 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Southwell Media
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @velocity-astro/cms-auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sign-in for Velocity CMS: email links and passkeys, kept in the CMS's own database. No passwords and no outside account.
|
|
4
|
+
|
|
5
|
+
Part of [Velocity CMS](https://github.com/Velocity-Astro/velocity-cms), a content management system for Astro sites. A site installs `@velocity-astro/cms-astro`, which brings the rest:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npx velocity-cms init
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Docs: https://docs.deployvelocity.com/cms/
|
|
12
|
+
|
|
13
|
+
MIT
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { ApiSession, Authenticator } from '@velocity-astro/cms-api';
|
|
2
|
+
import { AccessControl, CmsError, type Actor, type Role } from '@velocity-astro/cms-core';
|
|
3
|
+
import { type Db } from '@velocity-astro/cms-db';
|
|
4
|
+
import type { Mailer } from './mailer.js';
|
|
5
|
+
/** Someone who can sign in to the editor. */
|
|
6
|
+
export interface AuthUser {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly email: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly role: Role;
|
|
11
|
+
/** Whether they have ever signed in. Until then they were only invited. */
|
|
12
|
+
readonly hasSignedIn: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface Invitation {
|
|
15
|
+
readonly email: string;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly role: Role;
|
|
18
|
+
}
|
|
19
|
+
export interface CmsAuthOptions {
|
|
20
|
+
/** The CMS database, opened with `openDatabase` from `cms-db`. Sign-in keeps its tables there. */
|
|
21
|
+
readonly db: Db;
|
|
22
|
+
/** Signs sessions and links. At least 32 characters, random, and never committed. */
|
|
23
|
+
readonly secret: string;
|
|
24
|
+
/** The site's own address, such as `https://example.com`. Passkeys are tied to its host name. */
|
|
25
|
+
readonly origin: string;
|
|
26
|
+
/** Shown in emails and in the browser's passkey prompt. */
|
|
27
|
+
readonly siteName: string;
|
|
28
|
+
readonly mailer: Mailer;
|
|
29
|
+
/** Where sign-in is mounted. Defaults to `/api/cms/auth`. */
|
|
30
|
+
readonly basePath?: string;
|
|
31
|
+
/** Where the editor is mounted, which is where a sign-in link lands. Defaults to `/admin`. */
|
|
32
|
+
readonly adminPath?: string;
|
|
33
|
+
/** How long a sign-in link works for. Defaults to 10 minutes. */
|
|
34
|
+
readonly linkLifetimeMinutes?: number;
|
|
35
|
+
/**
|
|
36
|
+
* The least time a request for a sign-in link takes to answer, whoever the address belongs to.
|
|
37
|
+
* Defaults to one second, which is longer than an email usually takes to hand over.
|
|
38
|
+
*/
|
|
39
|
+
readonly answerAfterMs?: number;
|
|
40
|
+
/** Decides who may invite people. Defaults to the CMS's own rules; pass `cms.access` to share them. */
|
|
41
|
+
readonly access?: AccessControl;
|
|
42
|
+
}
|
|
43
|
+
/** The sign-in settings are wrong in a way that would leave the site unsafe or unreachable. */
|
|
44
|
+
export declare class AuthSetupError extends CmsError {
|
|
45
|
+
readonly code = "auth_setup";
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Sign-in for the CMS: email links and passkeys, kept in the CMS's own database. There are no
|
|
49
|
+
* passwords, and nobody can create their own account: people are invited.
|
|
50
|
+
*
|
|
51
|
+
* This class is the only place that knows which library does the work (Better Auth). The rest
|
|
52
|
+
* of the CMS sees an `Authenticator` for the API, `handle` to mount, and a few methods for
|
|
53
|
+
* managing who has access.
|
|
54
|
+
*/
|
|
55
|
+
export declare class CmsAuth implements Authenticator {
|
|
56
|
+
#private;
|
|
57
|
+
constructor(options: CmsAuthOptions);
|
|
58
|
+
/** Answers a request to one of the sign-in addresses under `basePath`. Mount it there. */
|
|
59
|
+
readonly handle: (request: Request) => Promise<Response>;
|
|
60
|
+
/** Who a request comes from, by its session cookie. This is what `CmsApi` asks. */
|
|
61
|
+
authenticate(request: Request): Promise<ApiSession | null>;
|
|
62
|
+
/** Everyone who can sign in, by name. */
|
|
63
|
+
listUsers(actor: Actor): Promise<AuthUser[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Lets someone in and tells them so by email. The email carries no link that signs them in:
|
|
66
|
+
* it points at the editor, where they ask for one, so an invitation read by someone else, or
|
|
67
|
+
* weeks later, opens nothing. If it cannot be sent, nobody is added and the owner is told.
|
|
68
|
+
*/
|
|
69
|
+
invite(actor: Actor, invitation: Invitation): Promise<AuthUser>;
|
|
70
|
+
/**
|
|
71
|
+
* Changes what someone may do. It takes effect on their next request. A site always keeps an
|
|
72
|
+
* owner, so the last one cannot be made anything less.
|
|
73
|
+
*/
|
|
74
|
+
changeRole(actor: Actor, userId: string, role: Role): Promise<AuthUser>;
|
|
75
|
+
/**
|
|
76
|
+
* Takes away someone's access: the person, their passkeys and every session of theirs, at
|
|
77
|
+
* once. What they wrote stays. Nobody removes themselves, and the last owner stays.
|
|
78
|
+
*/
|
|
79
|
+
remove(actor: Actor, userId: string): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Creates the site's first owner and a link that signs them in, without sending an email.
|
|
82
|
+
* This is how a new site is reached before it has an email provider: the installer prints the
|
|
83
|
+
* link. It refuses once anyone exists, so it cannot be used to take a site over.
|
|
84
|
+
*/
|
|
85
|
+
createFirstOwner(owner: Omit<Invitation, 'role'>): Promise<{
|
|
86
|
+
user: AuthUser;
|
|
87
|
+
link: string;
|
|
88
|
+
}>;
|
|
89
|
+
/**
|
|
90
|
+
* A link that signs one person in, handed back instead of emailed. For the command line and
|
|
91
|
+
* for support: whoever holds it is that person, so it is never shown in the editor.
|
|
92
|
+
*/
|
|
93
|
+
createSignInLink(email: string): Promise<string>;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAwD,KAAK,KAAK,EAAE,KAAK,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAChJ,OAAO,EAAqE,KAAK,EAAE,EAAE,MAAM,wBAAwB,CAAC;AAKpH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,6CAA6C;AAC7C,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,2EAA2E;IAC3E,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;CAC/B;AAED,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,kGAAkG;IAClG,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC;IAChB,qFAAqF;IACrF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iGAAiG;IACjG,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2DAA2D;IAC3D,QAAQ,CAAC,QAAQ,E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E,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,QAAQ,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IASlG;;;OAGG;IACG,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CA2FvD"}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { passkey } from '@better-auth/passkey';
|
|
2
|
+
import { AccessControl, CmsError, ConflictError, NotFoundError, ROLES, ValidationError } from '@velocity-astro/cms-core';
|
|
3
|
+
import { authAccount, authPasskey, authSession, authUser, authVerification } from '@velocity-astro/cms-db';
|
|
4
|
+
import { betterAuth } from 'better-auth';
|
|
5
|
+
import { eq } from 'drizzle-orm';
|
|
6
|
+
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
|
|
7
|
+
import { magicLink } from 'better-auth/plugins';
|
|
8
|
+
/** The sign-in settings are wrong in a way that would leave the site unsafe or unreachable. */
|
|
9
|
+
export class AuthSetupError extends CmsError {
|
|
10
|
+
code = 'auth_setup';
|
|
11
|
+
}
|
|
12
|
+
/** How many sign-in links one address is emailed in any ten minutes. More are asked for in vain. */
|
|
13
|
+
const LINKS_PER_ADDRESS = 3;
|
|
14
|
+
const LINKS_WINDOW_MS = 10 * 60_000;
|
|
15
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
16
|
+
const isRole = (value) => ROLES.includes(value);
|
|
17
|
+
const normalised = (email) => email.trim().toLowerCase();
|
|
18
|
+
/** Whether everywhere a sign-in link can send the person afterwards is a path on this site. */
|
|
19
|
+
function staysOnSite(link) {
|
|
20
|
+
const { searchParams } = new URL(link);
|
|
21
|
+
return ['callbackURL', 'newUserCallbackURL', 'errorCallbackURL'].every((name) => {
|
|
22
|
+
const target = searchParams.get(name);
|
|
23
|
+
// `//host` and `/\host` look like paths and are read by browsers as other sites.
|
|
24
|
+
return target === null || /^\/(?![/\\])/.test(target);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Sign-in for the CMS: email links and passkeys, kept in the CMS's own database. There are no
|
|
29
|
+
* passwords, and nobody can create their own account: people are invited.
|
|
30
|
+
*
|
|
31
|
+
* This class is the only place that knows which library does the work (Better Auth). The rest
|
|
32
|
+
* of the CMS sees an `Authenticator` for the API, `handle` to mount, and a few methods for
|
|
33
|
+
* managing who has access.
|
|
34
|
+
*/
|
|
35
|
+
export class CmsAuth {
|
|
36
|
+
#options;
|
|
37
|
+
#access;
|
|
38
|
+
#adminPath;
|
|
39
|
+
#origin;
|
|
40
|
+
#lifetime;
|
|
41
|
+
#auth;
|
|
42
|
+
/** Links being made for `createSignInLink`, by email: they are handed back instead of emailed. */
|
|
43
|
+
#handBack = new Map();
|
|
44
|
+
/** When links were last emailed, by address. */
|
|
45
|
+
#emailed = new Map();
|
|
46
|
+
constructor(options) {
|
|
47
|
+
if (options.secret.length < 32) {
|
|
48
|
+
throw new AuthSetupError('The sign-in secret is too short. Use at least 32 random characters, for example from `openssl rand -base64 32`.');
|
|
49
|
+
}
|
|
50
|
+
let origin;
|
|
51
|
+
try {
|
|
52
|
+
origin = new URL(options.origin);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
throw new AuthSetupError(`"${options.origin}" is not an address. Give the site's own, such as https://example.com.`);
|
|
56
|
+
}
|
|
57
|
+
this.#options = options;
|
|
58
|
+
this.#access = options.access ?? new AccessControl();
|
|
59
|
+
this.#adminPath = options.adminPath ?? '/admin';
|
|
60
|
+
this.#origin = origin.origin;
|
|
61
|
+
this.#lifetime = options.linkLifetimeMinutes ?? 10;
|
|
62
|
+
this.#auth = betterAuth({
|
|
63
|
+
baseURL: origin.origin,
|
|
64
|
+
basePath: options.basePath ?? '/api/cms/auth',
|
|
65
|
+
secret: options.secret,
|
|
66
|
+
database: drizzleAdapter(options.db, {
|
|
67
|
+
provider: 'sqlite',
|
|
68
|
+
schema: { user: authUser, session: authSession, account: authAccount, verification: authVerification, passkey: authPasskey },
|
|
69
|
+
}),
|
|
70
|
+
// The CMS role lives on the person. `input: false` means no sign-in request can set it.
|
|
71
|
+
user: { additionalFields: { role: { type: 'string', required: true, defaultValue: 'author', input: false } } },
|
|
72
|
+
telemetry: { enabled: false },
|
|
73
|
+
plugins: [
|
|
74
|
+
magicLink({
|
|
75
|
+
// Only people who were invited can sign in. A stranger's address creates nothing.
|
|
76
|
+
disableSignUp: true,
|
|
77
|
+
storeToken: 'hashed',
|
|
78
|
+
expiresIn: this.#lifetime * 60,
|
|
79
|
+
sendMagicLink: ({ email, url }) => this.#deliver(email, url),
|
|
80
|
+
}),
|
|
81
|
+
passkey({ rpID: origin.hostname, rpName: options.siteName, origin: origin.origin }),
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** Answers a request to one of the sign-in addresses under `basePath`. Mount it there. */
|
|
86
|
+
handle = (request) => this.#auth.handler(request);
|
|
87
|
+
/** Who a request comes from, by its session cookie. This is what `CmsApi` asks. */
|
|
88
|
+
async authenticate(request) {
|
|
89
|
+
const session = await this.#auth.api.getSession({ headers: request.headers });
|
|
90
|
+
if (!session)
|
|
91
|
+
return null;
|
|
92
|
+
const { id, email, name, role } = session.user;
|
|
93
|
+
// A role this version does not know grants nothing, rather than something by accident.
|
|
94
|
+
if (!isRole(role))
|
|
95
|
+
return null;
|
|
96
|
+
return { user: { id, email, name }, actor: { kind: 'user', id, role } };
|
|
97
|
+
}
|
|
98
|
+
/** Everyone who can sign in, by name. */
|
|
99
|
+
async listUsers(actor) {
|
|
100
|
+
this.#access.require(actor, 'user:manage');
|
|
101
|
+
const rows = await this.#options.db.select().from(authUser);
|
|
102
|
+
return rows
|
|
103
|
+
.map(({ id, email, name, role, emailVerified }) => ({ id, email, name, role: isRole(role) ? role : 'author', hasSignedIn: emailVerified }))
|
|
104
|
+
.sort((a, b) => a.name.localeCompare(b.name) || a.email.localeCompare(b.email));
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Lets someone in and tells them so by email. The email carries no link that signs them in:
|
|
108
|
+
* it points at the editor, where they ask for one, so an invitation read by someone else, or
|
|
109
|
+
* weeks later, opens nothing. If it cannot be sent, nobody is added and the owner is told.
|
|
110
|
+
*/
|
|
111
|
+
async invite(actor, invitation) {
|
|
112
|
+
this.#access.require(actor, 'user:manage');
|
|
113
|
+
const user = await this.#create(invitation);
|
|
114
|
+
try {
|
|
115
|
+
await this.#options.mailer.sendInvitation({ email: user.email, name: user.name, url: `${this.#origin}${this.#adminPath}`, siteName: this.#options.siteName });
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
await this.#options.db.delete(authUser).where(eq(authUser.id, user.id));
|
|
119
|
+
console.error('[velocity-cms] An invitation could not be sent.', error);
|
|
120
|
+
throw new ConflictError(`The invitation to ${user.email} could not be sent, so nobody was added. Check the site's email settings and try again.`);
|
|
121
|
+
}
|
|
122
|
+
return user;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Changes what someone may do. It takes effect on their next request. A site always keeps an
|
|
126
|
+
* owner, so the last one cannot be made anything less.
|
|
127
|
+
*/
|
|
128
|
+
async changeRole(actor, userId, role) {
|
|
129
|
+
this.#access.require(actor, 'user:manage');
|
|
130
|
+
if (!isRole(role))
|
|
131
|
+
throw new ValidationError(`Choose a role: ${ROLES.join(', ')}.`, [{ path: ['role'], message: `Choose a role: ${ROLES.join(', ')}.` }]);
|
|
132
|
+
const user = await this.#get(userId);
|
|
133
|
+
if (user.role === 'owner' && role !== 'owner')
|
|
134
|
+
await this.#requireAnotherOwner(userId, 'Make someone else an owner first.');
|
|
135
|
+
await this.#options.db.update(authUser).set({ role }).where(eq(authUser.id, userId));
|
|
136
|
+
return { ...user, role };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Takes away someone's access: the person, their passkeys and every session of theirs, at
|
|
140
|
+
* once. What they wrote stays. Nobody removes themselves, and the last owner stays.
|
|
141
|
+
*/
|
|
142
|
+
async remove(actor, userId) {
|
|
143
|
+
this.#access.require(actor, 'user:manage');
|
|
144
|
+
const user = await this.#get(userId);
|
|
145
|
+
if (actor.kind === 'user' && actor.id === userId) {
|
|
146
|
+
throw new ConflictError('You cannot remove yourself. Ask another owner to do it.');
|
|
147
|
+
}
|
|
148
|
+
if (user.role === 'owner')
|
|
149
|
+
await this.#requireAnotherOwner(userId, 'Make someone else an owner first.');
|
|
150
|
+
// Sessions, passkeys and accounts go with the person: their rows are tied to this one.
|
|
151
|
+
await this.#options.db.delete(authUser).where(eq(authUser.id, userId));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Creates the site's first owner and a link that signs them in, without sending an email.
|
|
155
|
+
* This is how a new site is reached before it has an email provider: the installer prints the
|
|
156
|
+
* link. It refuses once anyone exists, so it cannot be used to take a site over.
|
|
157
|
+
*/
|
|
158
|
+
async createFirstOwner(owner) {
|
|
159
|
+
const [existing] = await this.#options.db.select({ id: authUser.id }).from(authUser).limit(1);
|
|
160
|
+
if (existing) {
|
|
161
|
+
throw new ConflictError('This site already has people who can sign in. Ask an owner to invite you.');
|
|
162
|
+
}
|
|
163
|
+
const user = await this.#create({ ...owner, role: 'owner' });
|
|
164
|
+
return { user, link: await this.createSignInLink(user.email) };
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* A link that signs one person in, handed back instead of emailed. For the command line and
|
|
168
|
+
* for support: whoever holds it is that person, so it is never shown in the editor.
|
|
169
|
+
*/
|
|
170
|
+
async createSignInLink(email) {
|
|
171
|
+
const address = normalised(email);
|
|
172
|
+
if (!(await this.#find(address)))
|
|
173
|
+
throw new NotFoundError('Person with the email', address);
|
|
174
|
+
const link = new Promise((resolve) => this.#handBack.set(address, resolve));
|
|
175
|
+
try {
|
|
176
|
+
await this.#auth.api.signInMagicLink({ body: { email: address, callbackURL: this.#adminPath }, headers: new Headers() });
|
|
177
|
+
return await link;
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
this.#handBack.delete(address);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async #create({ email, name, role }) {
|
|
184
|
+
const address = normalised(email);
|
|
185
|
+
const issues = [
|
|
186
|
+
...(EMAIL.test(address) ? [] : [{ path: ['email'], message: 'Enter an email address, such as name@example.com.' }]),
|
|
187
|
+
...(name.trim() === '' ? [{ path: ['name'], message: 'Enter the person\'s name.' }] : []),
|
|
188
|
+
...(isRole(role) ? [] : [{ path: ['role'], message: `Choose a role: ${ROLES.join(', ')}.` }]),
|
|
189
|
+
];
|
|
190
|
+
const [first] = issues;
|
|
191
|
+
if (first)
|
|
192
|
+
throw new ValidationError(first.message, issues);
|
|
193
|
+
if (await this.#find(address))
|
|
194
|
+
throw new ConflictError(`${address} can already sign in.`);
|
|
195
|
+
// Written straight to the table this package owns, not through the library's internals,
|
|
196
|
+
// which change between its versions. The address starts out unverified: the only way in is
|
|
197
|
+
// a link sent to it, and using one is what verifies it. That is also how the People screen
|
|
198
|
+
// tells who was invited from who has signed in.
|
|
199
|
+
const id = globalThis.crypto.randomUUID();
|
|
200
|
+
await this.#options.db.insert(authUser).values({ id, email: address, name: name.trim(), emailVerified: false, role });
|
|
201
|
+
return { id, email: address, name: name.trim(), role, hasSignedIn: false };
|
|
202
|
+
}
|
|
203
|
+
async #get(userId) {
|
|
204
|
+
const [user] = await this.#options.db.select().from(authUser).where(eq(authUser.id, userId)).limit(1);
|
|
205
|
+
if (!user)
|
|
206
|
+
throw new NotFoundError('Person', userId);
|
|
207
|
+
return { id: user.id, email: user.email, name: user.name, role: isRole(user.role) ? user.role : 'author', hasSignedIn: user.emailVerified };
|
|
208
|
+
}
|
|
209
|
+
/** Refuses a change that would leave the site with no owner, who alone can let people in. */
|
|
210
|
+
async #requireAnotherOwner(userId, whatToDo) {
|
|
211
|
+
const owners = await this.#options.db.select({ id: authUser.id }).from(authUser).where(eq(authUser.role, 'owner'));
|
|
212
|
+
if (!owners.some((owner) => owner.id !== userId)) {
|
|
213
|
+
throw new ConflictError(`This is the site's only owner, and a site needs one. ${whatToDo}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
async #find(address) {
|
|
217
|
+
const [user] = await this.#options.db.select().from(authUser).where(eq(authUser.email, address)).limit(1);
|
|
218
|
+
return user ?? null;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Sends a sign-in link, but only to someone who has an account. The library asks for one to be
|
|
222
|
+
* sent whoever the address belongs to; a stranger gets no email, and the request is answered
|
|
223
|
+
* the same way, so nobody can use the form to learn who works on a site or to send mail.
|
|
224
|
+
*/
|
|
225
|
+
async #deliver(email, url) {
|
|
226
|
+
// Checked here whatever the library checks: a link must never carry someone, freshly signed
|
|
227
|
+
// in, to another site. The request is answered as usual, and nothing is sent.
|
|
228
|
+
if (!staysOnSite(url))
|
|
229
|
+
return;
|
|
230
|
+
const address = normalised(email);
|
|
231
|
+
const handBack = this.#handBack.get(address);
|
|
232
|
+
if (handBack)
|
|
233
|
+
return handBack(url);
|
|
234
|
+
// No request is answered sooner than this wait. Without it, the time an email takes to
|
|
235
|
+
// hand over would say whether the address belongs to someone here. The email is still
|
|
236
|
+
// waited for, since a site may stop working the moment it has answered.
|
|
237
|
+
const wait = new Promise((resolve) => setTimeout(resolve, this.#options.answerAfterMs ?? 1000));
|
|
238
|
+
const user = await this.#find(address);
|
|
239
|
+
if (user && this.#mayEmail(address)) {
|
|
240
|
+
// A link that cannot be sent is the owner's to hear of, in the log. Telling the person
|
|
241
|
+
// asking would tell a stranger the same thing.
|
|
242
|
+
const sending = this.#options.mailer
|
|
243
|
+
.sendSignInLink({ email: address, name: user.name, url, expiresInMinutes: this.#lifetime, siteName: this.#options.siteName })
|
|
244
|
+
.catch((error) => console.error('[velocity-cms] A sign-in link could not be sent.', error));
|
|
245
|
+
await sending;
|
|
246
|
+
}
|
|
247
|
+
await wait;
|
|
248
|
+
}
|
|
249
|
+
/** Whether one more link may be emailed to an address, which counts it. Nobody's inbox is filled by a stranger asking. */
|
|
250
|
+
#mayEmail(address) {
|
|
251
|
+
const now = Date.now();
|
|
252
|
+
const since = now - LINKS_WINDOW_MS;
|
|
253
|
+
if (this.#emailed.size > 5000) {
|
|
254
|
+
for (const [known, times] of this.#emailed)
|
|
255
|
+
if ((times.at(-1) ?? 0) <= since)
|
|
256
|
+
this.#emailed.delete(known);
|
|
257
|
+
}
|
|
258
|
+
const times = (this.#emailed.get(address) ?? []).filter((time) => time > since);
|
|
259
|
+
if (times.length >= LINKS_PER_ADDRESS)
|
|
260
|
+
return false;
|
|
261
|
+
this.#emailed.set(address, [...times, now]);
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,KAAK,EAAE,eAAe,EAAyB,MAAM,0BAA0B,CAAC;AAChJ,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,gBAAgB,EAAW,MAAM,wBAAwB,CAAC;AACpH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AA4ChD,+FAA+F;AAC/F,MAAM,OAAO,cAAe,SAAQ,QAAQ;IACjC,IAAI,GAAG,YAAY,CAAC;CAC9B;AAED,oGAAoG;AACpG,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,eA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C;YAC9B,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,QAAQ;gBAAE,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK;oBAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC5G,CAAC;QACD,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,KAAK,CAAC,CAAC;QAChF,IAAI,KAAK,CAAC,MAAM,IAAI,iBAAiB;YAAE,OAAO,KAAK,CAAC;QACpD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,GAAG,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;QAC5C,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser's side of sign-in. It talks to the addresses `CmsAuth.handle` answers, and to the
|
|
3
|
+
* browser's own passkey prompt. Nothing here runs on the server, and nothing from the server is
|
|
4
|
+
* imported, so the editor's bundle stays small.
|
|
5
|
+
*/
|
|
6
|
+
/** The person signed in, as the editor shows them. */
|
|
7
|
+
export interface SignedInUser {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly email: string;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
}
|
|
12
|
+
export interface AuthClientOptions {
|
|
13
|
+
/** Where sign-in is mounted. Defaults to `/api/cms/auth`. */
|
|
14
|
+
readonly basePath?: string;
|
|
15
|
+
/** Where the editor is mounted, which is where a sign-in link lands. Defaults to `/admin`. */
|
|
16
|
+
readonly adminPath?: string;
|
|
17
|
+
/** Defaults to the browser's own `fetch`. */
|
|
18
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
19
|
+
}
|
|
20
|
+
/** One of the signed-in person's passkeys, by the name they gave it. Nothing secret is in it. */
|
|
21
|
+
export interface PasskeySummary {
|
|
22
|
+
readonly id: string;
|
|
23
|
+
readonly name: string;
|
|
24
|
+
/** When it was added, as an ISO date, where the server knows. */
|
|
25
|
+
readonly createdAt?: string;
|
|
26
|
+
}
|
|
27
|
+
/** What the editor's `HttpAdminClient` takes as `auth`, plus adding a passkey once signed in. */
|
|
28
|
+
export interface AuthClient {
|
|
29
|
+
requestSignInLink(email: string): Promise<void>;
|
|
30
|
+
signInWithPasskey(): Promise<SignedInUser>;
|
|
31
|
+
signOut(): Promise<void>;
|
|
32
|
+
/** Adds a passkey for the person signed in. `name` tells their passkeys apart: "Work laptop". */
|
|
33
|
+
registerPasskey(name: string): Promise<void>;
|
|
34
|
+
/** The signed-in person's own passkeys, oldest first. */
|
|
35
|
+
listPasskeys(): Promise<PasskeySummary[]>;
|
|
36
|
+
/** Removes one of the signed-in person's own passkeys. The server refuses anyone else's. */
|
|
37
|
+
removePasskey(id: string): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/** Signing in could not be completed. The message can be shown as it is. */
|
|
40
|
+
export declare class SignInError extends Error {
|
|
41
|
+
constructor(message: string);
|
|
42
|
+
}
|
|
43
|
+
export declare function createAuthClient(options?: AuthClientOptions): AuthClient;
|
|
44
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,sDAAsD;AACtD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,6DAA6D;IAC7D,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,8FAA8F;IAC9F,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CAC1C;AAED,iGAAiG;AACjG,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,iEAAiE;IACjE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,iGAAiG;AACjG,MAAM,WAAW,UAAU;IACzB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD,iBAAiB,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC;IAC3C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,iGAAiG;IACjG,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,yDAAyD;IACzD,YAAY,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;IAC1C,4FAA4F;IAC5F,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C;AAED,4EAA4E;AAC5E,qBAAa,WAAY,SAAQ,KAAK;gBACxB,OAAO,EAAE,MAAM;CAI5B;AAED,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,iBAAsB,GAAG,UAAU,CA2E5E"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser's side of sign-in. It talks to the addresses `CmsAuth.handle` answers, and to the
|
|
3
|
+
* browser's own passkey prompt. Nothing here runs on the server, and nothing from the server is
|
|
4
|
+
* imported, so the editor's bundle stays small.
|
|
5
|
+
*/
|
|
6
|
+
/** Signing in could not be completed. The message can be shown as it is. */
|
|
7
|
+
export class SignInError extends Error {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'SignInError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function createAuthClient(options = {}) {
|
|
14
|
+
const basePath = (options.basePath ?? '/api/cms/auth').replace(/\/+$/, '');
|
|
15
|
+
const adminPath = options.adminPath ?? '/admin';
|
|
16
|
+
const send = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
17
|
+
const SIGN_IN_FAILED = 'Signing in did not work. Try again, or ask for a new sign-in link.';
|
|
18
|
+
async function call(path, body, refused = SIGN_IN_FAILED) {
|
|
19
|
+
let response;
|
|
20
|
+
try {
|
|
21
|
+
response = await send(`${basePath}${path}`, {
|
|
22
|
+
method: body === undefined ? 'GET' : 'POST',
|
|
23
|
+
credentials: 'same-origin',
|
|
24
|
+
headers: { accept: 'application/json', ...(body !== undefined && { 'content-type': 'application/json' }) },
|
|
25
|
+
...(body !== undefined && { body: JSON.stringify(body) }),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
throw new SignInError('The server could not be reached. Check your connection and try again.');
|
|
30
|
+
}
|
|
31
|
+
const answer = await response.json().catch(() => null);
|
|
32
|
+
if (response.ok)
|
|
33
|
+
return answer;
|
|
34
|
+
if (response.status === 429)
|
|
35
|
+
throw new SignInError('Too many attempts. Wait a minute, then try again.');
|
|
36
|
+
throw new SignInError(refused);
|
|
37
|
+
}
|
|
38
|
+
/** The browser's passkey support, or an explanation of why it cannot be used here. */
|
|
39
|
+
function passkeys() {
|
|
40
|
+
if (typeof PublicKeyCredential === 'undefined' || typeof PublicKeyCredential.parseRequestOptionsFromJSON !== 'function') {
|
|
41
|
+
throw new SignInError('This browser cannot use passkeys. Use a sign-in link instead.');
|
|
42
|
+
}
|
|
43
|
+
return PublicKeyCredential;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
async requestSignInLink(email) {
|
|
47
|
+
// The answer is the same whether or not the address belongs to someone.
|
|
48
|
+
await call('/sign-in/magic-link', { email, callbackURL: adminPath, errorCallbackURL: `${adminPath}/sign-in?link=expired` });
|
|
49
|
+
},
|
|
50
|
+
async signInWithPasskey() {
|
|
51
|
+
const support = passkeys();
|
|
52
|
+
const offered = (await call('/passkey/generate-authenticate-options'));
|
|
53
|
+
const credential = (await navigator.credentials.get({ publicKey: support.parseRequestOptionsFromJSON(offered) }));
|
|
54
|
+
if (!credential)
|
|
55
|
+
throw new SignInError('No passkey was chosen.');
|
|
56
|
+
await call('/passkey/verify-authentication', { response: credential.toJSON() });
|
|
57
|
+
const session = (await call('/get-session'));
|
|
58
|
+
if (!session?.user)
|
|
59
|
+
throw new SignInError('Signing in did not work. Try again, or ask for a new sign-in link.');
|
|
60
|
+
const { id, email, name } = session.user;
|
|
61
|
+
return { id, email, name };
|
|
62
|
+
},
|
|
63
|
+
async signOut() {
|
|
64
|
+
await call('/sign-out', {});
|
|
65
|
+
},
|
|
66
|
+
async registerPasskey(name) {
|
|
67
|
+
const support = passkeys();
|
|
68
|
+
const offered = (await call('/passkey/generate-register-options'));
|
|
69
|
+
const credential = (await navigator.credentials.create({ publicKey: support.parseCreationOptionsFromJSON(offered) }));
|
|
70
|
+
if (!credential)
|
|
71
|
+
throw new SignInError('No passkey was created.');
|
|
72
|
+
await call('/passkey/verify-registration', { response: credential.toJSON(), name });
|
|
73
|
+
},
|
|
74
|
+
async listPasskeys() {
|
|
75
|
+
const rows = await call('/passkey/list-user-passkeys', undefined, 'Your passkeys could not be read. Try again.');
|
|
76
|
+
if (!Array.isArray(rows))
|
|
77
|
+
return [];
|
|
78
|
+
return rows
|
|
79
|
+
.map(({ id, name, createdAt }) => ({ id, name: name ?? '', ...(typeof createdAt === 'string' && { createdAt }) }))
|
|
80
|
+
.sort((a, b) => (a.createdAt ?? '').localeCompare(b.createdAt ?? ''));
|
|
81
|
+
},
|
|
82
|
+
async removePasskey(id) {
|
|
83
|
+
await call('/passkey/delete-passkey', { id }, 'The passkey could not be removed. Try again.');
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAuCH,4EAA4E;AAC5E,MAAM,OAAO,WAAY,SAAQ,KAAK;IACpC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AAED,MAAM,UAAU,gBAAgB,CAAC,UAA6B,EAAE;IAC9D,MAAM,QAAQ,GAAG,CAAC,OAAO,CAAC,QAAQ,IAAI,eAAe,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC3E,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,QAAQ,CAAC;IAChD,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;IAE/E,MAAM,cAAc,GAAG,oEAAoE,CAAC;IAE5F,KAAK,UAAU,IAAI,CAAC,IAAY,EAAE,IAAc,EAAE,OAAO,GAAG,cAAc;QACxE,IAAI,QAAkB,CAAC;QACvB,IAAI,CAAC;YACH,QAAQ,GAAG,MAAM,IAAI,CAAC,GAAG,QAAQ,GAAG,IAAI,EAAE,EAAE;gBAC1C,MAAM,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM;gBAC3C,WAAW,EAAE,aAAa;gBAC1B,OAAO,EAAE,EAAE,MAAM,EAAE,kBAAkB,EAAE,GAAG,CAAC,IAAI,KAAK,SAAS,IAAI,EAAE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AAM,IAAI,CAAC,6BAA6B,EAAE,SAAS,EAAE,6CAA6C,CAAC,CAAC;YACjH,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;gBAAE,OAAO,EAAE,CAAC;YACpC,OAAQ,IAA0E;iBAC/E,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,IAAI,EAAE,EAAE,GAAG,CAAC,OAAO,SAAS,KAAK,QAAQ,IAAI,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC;iBACjH,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,CAAC;QAC1E,CAAC;QAED,KAAK,CAAC,aAAa,CAAC,EAAE;YACpB,MAAM,IAAI,CAAC,yBAAyB,EAAE,EAAE,EAAE,EAAE,EAAE,8CAA8C,CAAC,CAAC;QAChG,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { AuthSetupError, CmsAuth, type AuthUser, type CmsAuthOptions, type Invitation } from './auth.js';
|
|
2
|
+
export { MemoryMailer, type InvitationMail, type Mailer, type SignInMail } from './mailer.js';
|
|
3
|
+
export { AuthTeam } from './team.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,WAAW,CAAC;AACzG,OAAO,EAAE,YAAY,EAAE,KAAK,cAAc,EAAE,KAAK,MAAM,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAuD,MAAM,WAAW,CAAC;AACzG,OAAO,EAAE,YAAY,EAAqD,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/mailer.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** One sign-in link to deliver. */
|
|
2
|
+
export interface SignInMail {
|
|
3
|
+
readonly email: string;
|
|
4
|
+
/** The person's name, for the greeting. */
|
|
5
|
+
readonly name: string;
|
|
6
|
+
/** Opens the editor, signed in. Works once. */
|
|
7
|
+
readonly url: string;
|
|
8
|
+
/** How long the link works for, to say so in the email. */
|
|
9
|
+
readonly expiresInMinutes: number;
|
|
10
|
+
/** The site's name, for the subject line. */
|
|
11
|
+
readonly siteName: string;
|
|
12
|
+
}
|
|
13
|
+
/** Word to someone that they were let in. It carries no secret: they ask for a link themselves. */
|
|
14
|
+
export interface InvitationMail {
|
|
15
|
+
readonly email: string;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
/** The editor's address, where they ask for their first sign-in link. */
|
|
18
|
+
readonly url: string;
|
|
19
|
+
readonly siteName: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Delivers sign-in links and invitations. A site supplies one for its email provider. It is only
|
|
23
|
+
* ever called for someone who has an account, so it never needs to check. A mail that cannot be
|
|
24
|
+
* sent is thrown, so the person waiting for it is told.
|
|
25
|
+
*/
|
|
26
|
+
export interface Mailer {
|
|
27
|
+
sendSignInLink(mail: SignInMail): Promise<void>;
|
|
28
|
+
sendInvitation(mail: InvitationMail): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A `Mailer` that keeps what it is given instead of sending it. For tests, and for local
|
|
32
|
+
* development, where reading the link from `sent` stands in for opening an email.
|
|
33
|
+
*/
|
|
34
|
+
export declare class MemoryMailer implements Mailer {
|
|
35
|
+
readonly sent: SignInMail[];
|
|
36
|
+
readonly invitations: InvitationMail[];
|
|
37
|
+
sendSignInLink(mail: SignInMail): Promise<void>;
|
|
38
|
+
sendInvitation(mail: InvitationMail): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=mailer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailer.d.ts","sourceRoot":"","sources":["../src/mailer.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,2CAA2C;IAC3C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,+CAA+C;IAC/C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,2DAA2D;IAC3D,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,6CAA6C;IAC7C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,mGAAmG;AACnG,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,yEAAyE;IACzE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED;;;;GAIG;AACH,MAAM,WAAW,MAAM;IACrB,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD,cAAc,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACrD;AAED;;;GAGG;AACH,qBAAa,YAAa,YAAW,MAAM;IACzC,QAAQ,CAAC,IAAI,EAAE,UAAU,EAAE,CAAM;IACjC,QAAQ,CAAC,WAAW,EAAE,cAAc,EAAE,CAAM;IAEtC,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/C,cAAc,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAG1D"}
|
package/dist/mailer.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `Mailer` that keeps what it is given instead of sending it. For tests, and for local
|
|
3
|
+
* development, where reading the link from `sent` stands in for opening an email.
|
|
4
|
+
*/
|
|
5
|
+
export class MemoryMailer {
|
|
6
|
+
sent = [];
|
|
7
|
+
invitations = [];
|
|
8
|
+
async sendSignInLink(mail) {
|
|
9
|
+
this.sent.push(mail);
|
|
10
|
+
}
|
|
11
|
+
async sendInvitation(mail) {
|
|
12
|
+
this.invitations.push(mail);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=mailer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailer.js","sourceRoot":"","sources":["../src/mailer.ts"],"names":[],"mappings":"AAgCA;;;GAGG;AACH,MAAM,OAAO,YAAY;IACd,IAAI,GAAiB,EAAE,CAAC;IACxB,WAAW,GAAqB,EAAE,CAAC;IAE5C,KAAK,CAAC,cAAc,CAAC,IAAgB;QACnC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,cAAc,CAAC,IAAoB;QACvC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;CACF"}
|
package/dist/team.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Team, type TeamMember } from '@velocity-astro/cms-core';
|
|
2
|
+
import { type Db } from '@velocity-astro/cms-db';
|
|
3
|
+
/**
|
|
4
|
+
* Who is on the team, for the CMS to give work to: ids, names and roles, and no email. It reads
|
|
5
|
+
* the sign-in table and nothing of the sign-in library, so a site can hand it to the CMS as soon
|
|
6
|
+
* as its database is open.
|
|
7
|
+
*/
|
|
8
|
+
export declare class AuthTeam implements Team {
|
|
9
|
+
private readonly db;
|
|
10
|
+
constructor(db: Db);
|
|
11
|
+
members(): Promise<TeamMember[]>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=team.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"team.d.ts","sourceRoot":"","sources":["../src/team.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,IAAI,EAAE,KAAK,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACxF,OAAO,EAAY,KAAK,EAAE,EAAE,MAAM,wBAAwB,CAAC;AAI3D;;;;GAIG;AACH,qBAAa,QAAS,YAAW,IAAI;IACvB,OAAO,CAAC,QAAQ,CAAC,EAAE;gBAAF,EAAE,EAAE,EAAE;IAE7B,OAAO,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;CAKvC"}
|
package/dist/team.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ROLES } from '@velocity-astro/cms-core';
|
|
2
|
+
import { authUser } from '@velocity-astro/cms-db';
|
|
3
|
+
const isRole = (value) => ROLES.includes(value);
|
|
4
|
+
/**
|
|
5
|
+
* Who is on the team, for the CMS to give work to: ids, names and roles, and no email. It reads
|
|
6
|
+
* the sign-in table and nothing of the sign-in library, so a site can hand it to the CMS as soon
|
|
7
|
+
* as its database is open.
|
|
8
|
+
*/
|
|
9
|
+
export class AuthTeam {
|
|
10
|
+
db;
|
|
11
|
+
constructor(db) {
|
|
12
|
+
this.db = db;
|
|
13
|
+
}
|
|
14
|
+
async members() {
|
|
15
|
+
const rows = await this.db.select({ id: authUser.id, name: authUser.name, role: authUser.role }).from(authUser);
|
|
16
|
+
// A role this version does not know grants nothing, so it is the least there is.
|
|
17
|
+
return rows.map(({ id, name, role }) => ({ id, name, role: isRole(role) ? role : 'author' })).sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=team.js.map
|
package/dist/team.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"team.js","sourceRoot":"","sources":["../src/team.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAyC,MAAM,0BAA0B,CAAC;AACxF,OAAO,EAAE,QAAQ,EAAW,MAAM,wBAAwB,CAAC;AAE3D,MAAM,MAAM,GAAG,CAAC,KAAc,EAAiB,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAa,CAAC,CAAC;AAEhF;;;;GAIG;AACH,MAAM,OAAO,QAAQ;IACU;IAA7B,YAA6B,EAAM;QAAN,OAAE,GAAF,EAAE,CAAI;IAAG,CAAC;IAEvC,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAChH,iFAAiF;QACjF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAE,QAAkB,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACpL,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@velocity-astro/cms-auth",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-beta.2",
|
|
4
|
+
"description": "Sign-in for Velocity CMS: email links and passkeys, kept in the CMS's own database. No passwords and no outside account.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Velocity-Astro/velocity-cms.git",
|
|
10
|
+
"directory": "packages/cms-auth"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://docs.deployvelocity.com/cms/",
|
|
13
|
+
"bugs": "https://github.com/Velocity-Astro/velocity-cms/issues",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=22.23.4"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./team": {
|
|
27
|
+
"types": "./dist/team.d.ts",
|
|
28
|
+
"default": "./dist/team.js"
|
|
29
|
+
},
|
|
30
|
+
"./client": {
|
|
31
|
+
"types": "./dist/client.d.ts",
|
|
32
|
+
"default": "./dist/client.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"!dist/**/*.test.*"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@better-auth/passkey": "1.7.25",
|
|
41
|
+
"better-auth": "1.7.25",
|
|
42
|
+
"drizzle-orm": "^0.45.4",
|
|
43
|
+
"@velocity-astro/cms-core": "0.1.0-beta.2",
|
|
44
|
+
"@velocity-astro/cms-db": "0.1.0-beta.2",
|
|
45
|
+
"@velocity-astro/cms-api": "0.1.0-beta.2"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@libsql/client": "^0.18.0"
|
|
49
|
+
}
|
|
6
50
|
}
|