@talajs/auth 0.0.0-stage → 0.2.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 +202 -0
- package/README.md +77 -2
- package/dist/emails.d.ts +16 -0
- package/dist/emails.d.ts.map +1 -0
- package/dist/emails.js +35 -0
- package/dist/emails.js.map +1 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +238 -0
- package/dist/index.js.map +1 -0
- package/dist/oauth.d.ts +2721 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +208 -0
- package/dist/oauth.js.map +1 -0
- package/package.json +57 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,78 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @talajs/auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Optional built-in sign-in for the people who run a business on Tala: owners, managers and staff. Powered by [Better Auth](https://better-auth.com), running on Tala's own database client, inside your Worker.
|
|
4
|
+
|
|
5
|
+
- **Magic link by default.** People sign in with a link emailed to them. It needs no password hashing, so it runs on Workers Free.
|
|
6
|
+
- **Email and password, opt in.** Includes email confirmation and reset. Secure password hashing needs more CPU per request than Workers Free allows, so turn it on with Workers Paid.
|
|
7
|
+
- **Google, opt in.**
|
|
8
|
+
- **Invite-only.** Nobody can create an account without a pending invitation, unless you enable self-serve businesses.
|
|
9
|
+
- **Revocation is immediate.** Sessions live in the database, with no cookie cache: signing out everywhere, a password reset or a removed membership take effect on the next request.
|
|
10
|
+
- **Rate limited without extra infrastructure.** An atomic Postgres counter, keyed on Cloudflare's `cf-connecting-ip`, plus per-email limits on outgoing links.
|
|
11
|
+
|
|
12
|
+
Better Auth only answers *who is this person*. What they may do is decided by their Tala membership (OWNER, MANAGER or STAFF, and which stores), as for every other identity source.
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { createTalaApp } from '@talajs/api'
|
|
16
|
+
import { serviceToken } from '@talajs/api/auth/service-token'
|
|
17
|
+
import { resend } from '@talajs/api/email/resend'
|
|
18
|
+
import { createTalaAuth } from '@talajs/auth'
|
|
19
|
+
import { createDb } from '@talajs/db'
|
|
20
|
+
import { Hono } from 'hono'
|
|
21
|
+
|
|
22
|
+
const db = (env: Env) => createDb({ url: env.DATABASE_URL }) // or env.HYPERDRIVE.connectionString
|
|
23
|
+
const mailer = (env: Env) => resend({ apiKey: env.RESEND_API_KEY, from: 'Café <hello@cafe.example>' })
|
|
24
|
+
|
|
25
|
+
const auth = createTalaAuth<Env>({
|
|
26
|
+
db,
|
|
27
|
+
secret: (env) => env.BETTER_AUTH_SECRET, // wrangler secret put BETTER_AUTH_SECRET
|
|
28
|
+
baseURL: (env) => `${env.PUBLIC_ORIGIN}/tala/auth`,
|
|
29
|
+
mailer,
|
|
30
|
+
appName: 'Café',
|
|
31
|
+
methods: { magicLink: true, password: false }, // password: Workers Paid
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const app = new Hono<{ Bindings: Env }>()
|
|
35
|
+
app.on(['GET', 'POST'], '/tala/auth/*', (c) => auth.handler(c.req.raw, c.env, c.executionCtx))
|
|
36
|
+
app.route('/tala', createTalaApp<Env>({
|
|
37
|
+
db,
|
|
38
|
+
auth: [auth.adapter, serviceToken({ token: (env) => env.TALA_SERVICE_TOKEN })],
|
|
39
|
+
invitations: { acceptUrl: (token, env) => `${env.PUBLIC_ORIGIN}/admin/invite/${token}`, mailer },
|
|
40
|
+
}))
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Serve the admin with `auth: { mode: 'better-auth', methods: { magicLink: true, password: false } }` in its runtime config (match `methods`), on the same origin.
|
|
44
|
+
|
|
45
|
+
## Setup
|
|
46
|
+
|
|
47
|
+
1. **Migrate.** `tala-db migrate` creates Better Auth's tables in the `tala_auth` schema (migration `0002`). People and sessions aren't tenant data, so they live apart from the `tala` schema.
|
|
48
|
+
2. **Secret.** `BETTER_AUTH_SECRET`: at least 32 random characters (`openssl rand -base64 32`). To rotate it without signing everyone out, see Better Auth's `BETTER_AUTH_SECRETS`.
|
|
49
|
+
3. **Email.** A mailer is required: without email nobody can sign in. Any `Mailer` from `@talajs/api` works; `resend()` is built in.
|
|
50
|
+
4. **Origins.** `baseURL`'s origin is trusted for CSRF checks. List any other origin that calls the handler in `trustedOrigins`.
|
|
51
|
+
5. **The first owner.** Provision a business with a service token (`POST /tala/v1/tenants`) and invite its owner (`POST /tala/v1/tenants/{id}/invitations` with `role: "OWNER"`). Or set `allowSelfServeTenants: true` on both `createTalaAuth` and `createTalaApp` to let anyone with a verified email create a business.
|
|
52
|
+
|
|
53
|
+
## Options
|
|
54
|
+
|
|
55
|
+
| Option | |
|
|
56
|
+
|---|---|
|
|
57
|
+
| `db` | The same per-request client factory as `createTalaApp`. |
|
|
58
|
+
| `secret` | `BETTER_AUTH_SECRET`. |
|
|
59
|
+
| `baseURL` | Public URL of the handler; its path is the mount point. |
|
|
60
|
+
| `trustedOrigins` | Other origins allowed to call the handler. |
|
|
61
|
+
| `mailer` | Sends sign-in links, confirmations and resets. |
|
|
62
|
+
| `appName`, `renderEmail` | Branding for the emails. |
|
|
63
|
+
| `methods` | `{ magicLink = true, password = false, google? }`. `google` takes `{ clientId, clientSecret }`. |
|
|
64
|
+
| `allowSelfServeTenants` | Let anyone sign up. Match `createTalaApp`. Default off: invite only. |
|
|
65
|
+
| `bearer` | Accept the session token as `Authorization: Bearer` (clients without cookies). Default off. |
|
|
66
|
+
| `configure` | Last word on the Better Auth options, for anything else. |
|
|
67
|
+
|
|
68
|
+
The adapter's identity is `{ kind: 'staff', subject: 'ba:<user id>', email, emailVerified, name }`. The `ba:` prefix can't collide with Cloudflare Access emails or OIDC subjects.
|
|
69
|
+
|
|
70
|
+
## Operators and Cloudflare Access
|
|
71
|
+
|
|
72
|
+
Don't put a Cloudflare Access application in front of the paths merchants use (`/admin`, `/tala`): they could never reach the sign-in screen. Route a second hostname to the same Worker for operators and protect that one with Access. Keep `cloudflareAccess()` in the adapter list; it only matches requests that carry an Access token. [`examples/worker`](../../examples/worker) does exactly this.
|
|
73
|
+
|
|
74
|
+
## Notes
|
|
75
|
+
|
|
76
|
+
- One Better Auth instance is created per request, on that request's database client. Workers can't share database connections between requests, even through Hyperdrive.
|
|
77
|
+
- Better Auth requires Prisma 7; Tala stays on Prisma 7 until Better Auth supports 8.
|
|
78
|
+
- Tested in workerd with node-postgres through Hyperdrive. The Neon serverless driver sits below Prisma, so Better Auth sees the same client either way.
|
package/dist/emails.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface AuthEmail {
|
|
2
|
+
subject: string;
|
|
3
|
+
html: string;
|
|
4
|
+
text: string;
|
|
5
|
+
}
|
|
6
|
+
export type AuthEmailKind = 'magic-link' | 'verify-email' | 'reset-password';
|
|
7
|
+
export interface AuthEmailData {
|
|
8
|
+
kind: AuthEmailKind;
|
|
9
|
+
to: string;
|
|
10
|
+
url: string;
|
|
11
|
+
/** `appName` from the options, e.g. the café's name. */
|
|
12
|
+
appName: string;
|
|
13
|
+
}
|
|
14
|
+
/** Plain sign-in emails. Pass `renderEmail` to `createTalaAuth` to brand them. */
|
|
15
|
+
export declare function renderAuthEmail({ kind, url, appName }: AuthEmailData): AuthEmail;
|
|
16
|
+
//# sourceMappingURL=emails.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emails.d.ts","sourceRoot":"","sources":["../src/emails.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;CACb;AAED,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG,cAAc,GAAG,gBAAgB,CAAA;AAE5E,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,aAAa,CAAA;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,GAAG,EAAE,MAAM,CAAA;IACX,wDAAwD;IACxD,OAAO,EAAE,MAAM,CAAA;CAChB;AA8BD,kFAAkF;AAClF,wBAAgB,eAAe,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,aAAa,GAAG,SAAS,CAWhF"}
|
package/dist/emails.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const escapeHtml = (s) => s.replace(/[&<>"']/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[ch] ?? ch);
|
|
2
|
+
const COPY = {
|
|
3
|
+
'magic-link': {
|
|
4
|
+
subject: 'Your sign-in link',
|
|
5
|
+
lead: 'Use this link to sign in.',
|
|
6
|
+
button: 'Sign in',
|
|
7
|
+
tail: "It works once and expires in 5 minutes. If you didn't ask for it, you can ignore this email.",
|
|
8
|
+
},
|
|
9
|
+
'verify-email': {
|
|
10
|
+
subject: 'Confirm your email',
|
|
11
|
+
lead: 'Confirm this is your email address.',
|
|
12
|
+
button: 'Confirm email',
|
|
13
|
+
tail: "If you didn't create an account, you can ignore this email.",
|
|
14
|
+
},
|
|
15
|
+
'reset-password': {
|
|
16
|
+
subject: 'Reset your password',
|
|
17
|
+
lead: 'Use this link to choose a new password. Resetting signs you out everywhere.',
|
|
18
|
+
button: 'Reset password',
|
|
19
|
+
tail: "It expires in 1 hour. If you didn't ask for it, you can ignore this email.",
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
/** Plain sign-in emails. Pass `renderEmail` to `createTalaAuth` to brand them. */
|
|
23
|
+
export function renderAuthEmail({ kind, url, appName }) {
|
|
24
|
+
const copy = COPY[kind];
|
|
25
|
+
const subject = `${appName}: ${copy.subject}`;
|
|
26
|
+
const text = `${copy.lead}\n\n${url}\n\n${copy.tail}`;
|
|
27
|
+
const html = `<!doctype html><html><body style="font-family:system-ui,sans-serif;max-width:480px;margin:auto;padding:24px;color:#111">
|
|
28
|
+
<p><strong>${escapeHtml(appName)}</strong></p>
|
|
29
|
+
<p>${escapeHtml(copy.lead)}</p>
|
|
30
|
+
<p><a href="${escapeHtml(url)}" style="display:inline-block;padding:10px 16px;background:#111;color:#fff;border-radius:6px;text-decoration:none">${escapeHtml(copy.button)}</a></p>
|
|
31
|
+
<p style="color:#555;font-size:14px">${escapeHtml(copy.tail)}</p>
|
|
32
|
+
</body></html>`;
|
|
33
|
+
return { subject, html, text };
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=emails.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emails.js","sourceRoot":"","sources":["../src/emails.ts"],"names":[],"mappings":"AAgBA,MAAM,UAAU,GAAG,CAAC,CAAS,EAAE,EAAE,CAC/B,CAAC,CAAC,OAAO,CACP,UAAU,EACV,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,CAC5F,CAAA;AAEH,MAAM,IAAI,GACR;IACE,YAAY,EAAE;QACZ,OAAO,EAAE,mBAAmB;QAC5B,IAAI,EAAE,2BAA2B;QACjC,MAAM,EAAE,SAAS;QACjB,IAAI,EAAE,8FAA8F;KACrG;IACD,cAAc,EAAE;QACd,OAAO,EAAE,oBAAoB;QAC7B,IAAI,EAAE,qCAAqC;QAC3C,MAAM,EAAE,eAAe;QACvB,IAAI,EAAE,6DAA6D;KACpE;IACD,gBAAgB,EAAE;QAChB,OAAO,EAAE,qBAAqB;QAC9B,IAAI,EAAE,6EAA6E;QACnF,MAAM,EAAE,gBAAgB;QACxB,IAAI,EAAE,4EAA4E;KACnF;CACF,CAAA;AAEH,kFAAkF;AAClF,MAAM,UAAU,eAAe,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAiB;IACnE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,CAAA;IACvB,MAAM,OAAO,GAAG,GAAG,OAAO,KAAK,IAAI,CAAC,OAAO,EAAE,CAAA;IAC7C,MAAM,IAAI,GAAG,GAAG,IAAI,CAAC,IAAI,OAAO,GAAG,OAAO,IAAI,CAAC,IAAI,EAAE,CAAA;IACrD,MAAM,IAAI,GAAG;aACF,UAAU,CAAC,OAAO,CAAC;KAC3B,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC;cACZ,UAAU,CAAC,GAAG,CAAC,sHAAsH,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC;uCACnI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC;eAC7C,CAAA;IACb,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAA;AAChC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type Configurable, type Identity, type Mailer } from '@talajs/api';
|
|
2
|
+
import type { TalaDb } from '@talajs/db';
|
|
3
|
+
import { type BetterAuthOptions } from 'better-auth';
|
|
4
|
+
import { type AuthEmailData } from './emails.js';
|
|
5
|
+
import { type OAuthProviderOptions } from './oauth.js';
|
|
6
|
+
export { type AuthEmail, type AuthEmailData, type AuthEmailKind, renderAuthEmail, } from './emails.js';
|
|
7
|
+
export { ACCESS_TOKEN_PREFIX, cleanupOAuthClients, OAUTH_SCOPES, type OAuthAccess, type OAuthProviderOptions, resolveAccessToken, } from './oauth.js';
|
|
8
|
+
export interface GoogleOptions {
|
|
9
|
+
clientId: string;
|
|
10
|
+
clientSecret: string;
|
|
11
|
+
}
|
|
12
|
+
export interface TalaAuthOptions<E> {
|
|
13
|
+
/** The same per-request client factory you give `createTalaApp`. */
|
|
14
|
+
db: (env: E) => TalaDb;
|
|
15
|
+
/** `BETTER_AUTH_SECRET`: at least 32 random characters. Signs cookies and tokens. */
|
|
16
|
+
secret: Configurable<string, E>;
|
|
17
|
+
/**
|
|
18
|
+
* The public URL the handler is mounted at, e.g. `https://cafe.example/tala/auth`. Its origin
|
|
19
|
+
* is trusted for CSRF checks and its path is the base path.
|
|
20
|
+
*/
|
|
21
|
+
baseURL: Configurable<string, E>;
|
|
22
|
+
/** Other origins allowed to call the handler (the same origin always is). */
|
|
23
|
+
trustedOrigins?: Configurable<string[], E>;
|
|
24
|
+
/** Sends sign-in links, email confirmations and password resets. */
|
|
25
|
+
mailer: Configurable<Mailer, E>;
|
|
26
|
+
/** Shown in emails, e.g. your business name. Default "Sign in". */
|
|
27
|
+
appName?: string;
|
|
28
|
+
renderEmail?: (data: AuthEmailData) => {
|
|
29
|
+
subject: string;
|
|
30
|
+
html: string;
|
|
31
|
+
text: string;
|
|
32
|
+
};
|
|
33
|
+
methods?: {
|
|
34
|
+
/** Email sign-in links. Default on; needs no password hashing, so it runs on Workers Free. */
|
|
35
|
+
magicLink?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Email and password, with email confirmation and reset. Default off: secure password hashing
|
|
38
|
+
* needs more CPU than Workers Free allows per request, so enable it on Workers Paid.
|
|
39
|
+
*/
|
|
40
|
+
password?: boolean;
|
|
41
|
+
/** Sign in with Google. Off unless configured. */
|
|
42
|
+
google?: Configurable<GoogleOptions | undefined, E>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Who may create an account. By default only people with a pending invitation, so nobody can
|
|
46
|
+
* sign up to an empty account. Set it to match `createTalaApp`'s `allowSelfServeTenants`.
|
|
47
|
+
*/
|
|
48
|
+
allowSelfServeTenants?: boolean;
|
|
49
|
+
/** Accept the session token as `Authorization: Bearer` for clients without cookies. Default off. */
|
|
50
|
+
bearer?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Turn this deployment into an OAuth 2.1 authorization server for MCP clients (Claude custom
|
|
53
|
+
* connectors and others), used by `@talajs/mcp`. Off unless set.
|
|
54
|
+
*/
|
|
55
|
+
oauth?: Configurable<OAuthProviderOptions, E>;
|
|
56
|
+
/** Final say over the Better Auth options, for anything not covered here. */
|
|
57
|
+
configure?: (options: BetterAuthOptions, env: E) => BetterAuthOptions;
|
|
58
|
+
}
|
|
59
|
+
/** Adapter subjects are `ba:<user id>`, which can't collide with Access emails or OIDC subjects. */
|
|
60
|
+
export declare const SUBJECT_PREFIX = "ba:";
|
|
61
|
+
export declare function createTalaAuth<E>(options: TalaAuthOptions<E>): {
|
|
62
|
+
/**
|
|
63
|
+
* Serve Better Auth's endpoints. Mount at the `baseURL` path:
|
|
64
|
+
*
|
|
65
|
+
* app.on(['GET', 'POST'], '/tala/auth/*', (c) => auth.handler(c.req.raw, c.env, c.executionCtx))
|
|
66
|
+
*/
|
|
67
|
+
handler(request: Request, env: E, ctx?: {
|
|
68
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
69
|
+
}): Promise<Response>;
|
|
70
|
+
/** For `createTalaApp({ auth: [auth.adapter, …] })`: a signed-in person, from the session cookie. */
|
|
71
|
+
adapter: {
|
|
72
|
+
name: string;
|
|
73
|
+
authenticate(c: import("@talajs/api").TalaContext<E>): Promise<Identity | null>;
|
|
74
|
+
};
|
|
75
|
+
/** The Better Auth handler's public URL, for OAuth metadata. */
|
|
76
|
+
baseURL: (env: E) => string;
|
|
77
|
+
/** The OAuth settings, when the provider is on. */
|
|
78
|
+
oauth: (env: E) => OAuthProviderOptions | undefined;
|
|
79
|
+
/** A Better Auth instance for server-side calls (`auth.api.*`) within one request. */
|
|
80
|
+
instance: (env: E, db: TalaDb) => import("better-auth").Auth<BetterAuthOptions>;
|
|
81
|
+
};
|
|
82
|
+
export type TalaAuth<E> = ReturnType<typeof createTalaAuth<E>>;
|
|
83
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EAGjB,KAAK,QAAQ,EACb,KAAK,MAAM,EACZ,MAAM,aAAa,CAAA;AAEpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EAAY,KAAK,iBAAiB,EAAc,MAAM,aAAa,CAAA;AAG1E,OAAO,EAAE,KAAK,aAAa,EAAmB,MAAM,aAAa,CAAA;AACjE,OAAO,EAIL,KAAK,oBAAoB,EAG1B,MAAM,YAAY,CAAA;AAEnB,OAAO,EACL,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,eAAe,GAChB,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,YAAY,EACZ,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,kBAAkB,GACnB,MAAM,YAAY,CAAA;AAKnB,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,oEAAoE;IACpE,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,MAAM,CAAA;IACtB,qFAAqF;IACrF,MAAM,EAAE,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;IAC/B;;;OAGG;IACH,OAAO,EAAE,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;IAChC,6EAA6E;IAC7E,cAAc,CAAC,EAAE,YAAY,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAA;IAC1C,oEAAoE;IACpE,MAAM,EAAE,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;IAC/B,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,aAAa,KAAK;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAA;IACtF,OAAO,CAAC,EAAE;QACR,8FAA8F;QAC9F,SAAS,CAAC,EAAE,OAAO,CAAA;QACnB;;;WAGG;QACH,QAAQ,CAAC,EAAE,OAAO,CAAA;QAClB,kDAAkD;QAClD,MAAM,CAAC,EAAE,YAAY,CAAC,aAAa,GAAG,SAAS,EAAE,CAAC,CAAC,CAAA;KACpD,CAAA;IACD;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B,oGAAoG;IACpG,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;OAGG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC,oBAAoB,EAAE,CAAC,CAAC,CAAA;IAC7C,6EAA6E;IAC7E,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,iBAAiB,EAAE,GAAG,EAAE,CAAC,KAAK,iBAAiB,CAAA;CACtE;AAED,oGAAoG;AACpG,eAAO,MAAM,cAAc,QAAQ,CAAA;AAanC,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC;IAmKzD;;;;OAIG;qBAEQ,OAAO,OACX,CAAC,QACA;QAAE,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;KAAE,GACnD,OAAO,CAAC,QAAQ,CAAC;IAoBpB,qGAAqG;;;+DAG5E,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;;IAoBjD,gEAAgE;mBACjD,CAAC;IAChB,mDAAmD;iBACtC,CAAC;IAEd,sFAAsF;oBACtE,CAAC,MAAM,MAAM;EAEhC;AAED,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,CAAC,CAAC,CAAA"}
|