@humanonlyweb/create-nuxtflare 0.1.3 → 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.
Files changed (70) hide show
  1. package/README.md +3 -1
  2. package/dist/cli.js +3 -1
  3. package/dist/template/.github/workflows/ci.yml +3 -1
  4. package/dist/template/CLAUDE.md +4 -2
  5. package/dist/template/app/components/turnstile-widget.vue +81 -0
  6. package/dist/template/app/composables/use-auth.ts +418 -0
  7. package/dist/template/app/pages/account.vue +125 -11
  8. package/dist/template/app/pages/email/[token].vue +18 -0
  9. package/dist/template/app/pages/forgot-password.vue +28 -0
  10. package/dist/template/app/pages/login.vue +9 -73
  11. package/dist/template/app/pages/register.vue +4 -26
  12. package/dist/template/app/pages/reset/[token].vue +31 -0
  13. package/dist/template/app/pages/verify/[token].vue +18 -0
  14. package/dist/template/nuxt.config.ts +4 -0
  15. package/dist/template/package.json +2 -0
  16. package/dist/template/scripts/admin-password.ts +13 -6
  17. package/dist/template/scripts/migrate.ts +67 -0
  18. package/dist/template/server/api/account/email/index.post.ts +1 -1
  19. package/dist/template/server/api/account/index.delete.ts +2 -2
  20. package/dist/template/server/api/account/password.post.ts +8 -0
  21. package/dist/template/server/api/auth/email/index.post.ts +1 -1
  22. package/dist/template/server/api/auth/login/totp.post.ts +6 -2
  23. package/dist/template/server/api/auth/login.post.ts +4 -2
  24. package/dist/template/server/api/auth/register.post.ts +1 -1
  25. package/dist/template/server/api/auth/verify/index.post.ts +1 -1
  26. package/dist/template/server/app/admin/auth.ts +2 -1
  27. package/dist/template/server/app/features.ts +4 -3
  28. package/dist/template/server/app/identity/email-change.ts +44 -7
  29. package/dist/template/server/app/identity/identity.test.ts +70 -0
  30. package/dist/template/server/app/identity/lockout.ts +8 -3
  31. package/dist/template/server/app/identity/password-reset.ts +3 -1
  32. package/dist/template/server/app/identity/service.ts +149 -30
  33. package/dist/template/server/app/identity/sessions.ts +43 -20
  34. package/dist/template/server/app/identity/totp-crypto.ts +12 -5
  35. package/dist/template/server/app/identity/totp.ts +24 -15
  36. package/dist/template/server/app/identity/verification.ts +3 -1
  37. package/dist/template/server/app/maintenance/cleanup.test.ts +56 -0
  38. package/dist/template/server/app/maintenance/cleanup.ts +63 -0
  39. package/dist/template/server/core/db/atomic.ts +9 -11
  40. package/dist/template/server/core/db/migrations.ts +7 -0
  41. package/dist/template/server/core/db/migrator.ts +5 -1
  42. package/dist/template/server/core/http/body.ts +1 -1
  43. package/dist/template/server/core/http/handle.ts +89 -81
  44. package/dist/template/server/core/http/security.test.ts +38 -0
  45. package/dist/template/server/core/http/security.ts +17 -5
  46. package/dist/template/server/core/http/session.ts +15 -0
  47. package/dist/template/server/core/platform/crypto-and-window.test.ts +75 -0
  48. package/dist/template/server/core/platform/database.ts +16 -1
  49. package/dist/template/server/core/platform/pii.ts +5 -8
  50. package/dist/template/server/core/platform/rate-limiter-strict.ts +35 -5
  51. package/dist/template/server/core/platform/rate-limiter.ts +2 -2
  52. package/dist/template/server/core/platform/seal.ts +20 -21
  53. package/dist/template/server/core/platform/sliding-window.ts +11 -2
  54. package/dist/template/server/core/platform/tokens.ts +3 -1
  55. package/dist/template/server/middleware/00-security-headers.ts +8 -4
  56. package/dist/template/server/middleware/01-user-session.ts +17 -5
  57. package/dist/template/server/plugins/page-csp.ts +24 -4
  58. package/dist/template/server/plugins/sessions.ts +4 -2
  59. package/dist/template/server/routes/auth/github.get.ts +4 -3
  60. package/dist/template/server/routes/auth/google.get.ts +4 -2
  61. package/dist/template/server/tasks/cleanup.ts +15 -0
  62. package/dist/template/server/test/invariants.test.ts +4 -2
  63. package/dist/template/server/utils/define-validated-handler.ts +14 -12
  64. package/dist/template/shared/domain/errors.ts +1 -0
  65. package/dist/template/shared/types/auth.d.ts +1 -1
  66. package/dist/template/shared/utils/auth.ts +10 -4
  67. package/dist/template/shared/utils/paths.ts +2 -0
  68. package/dist/template/shared/utils/schema-validation.ts +1 -1
  69. package/dist/template/wrangler.json +17 -0
  70. package/package.json +1 -1
@@ -0,0 +1,15 @@
1
+ import { defineTask } from 'nitro/task'
2
+
3
+ import { Maintenance } from '#server/app/maintenance/cleanup'
4
+ import { runtime } from '#server/core/http/runtime'
5
+
6
+ export default defineTask({
7
+ meta: {
8
+ name: 'cleanup',
9
+ description: 'Purges expired sessions, one-time tokens, stale login failure rows, and abandoned unverified accounts.',
10
+ },
11
+ async run() {
12
+ const result = await runtime.runPromise(Maintenance.use((maintenance) => maintenance.purgeExpired))
13
+ return { result }
14
+ },
15
+ })
@@ -62,11 +62,13 @@ describe('database invariants', () => {
62
62
 
63
63
  // counting with 0 failures is rejected
64
64
  await expect(async () =>
65
- db.exec(`INSERT INTO login_failures (subject, status, failures, locked_until) VALUES ('u1', 'counting', 0, NULL)`),
65
+ db.exec(`INSERT INTO login_failures (subject, status, failures, locked_until, updated_at) VALUES ('u1', 'counting', 0, NULL, '${now}')`),
66
66
  ).rejects.toThrow()
67
67
 
68
68
  // locked without locked_until is rejected
69
- await expect(async () => db.exec(`INSERT INTO login_failures (subject, status, failures, locked_until) VALUES ('u2', 'locked', 0, NULL)`)).rejects.toThrow()
69
+ await expect(async () =>
70
+ db.exec(`INSERT INTO login_failures (subject, status, failures, locked_until, updated_at) VALUES ('u2', 'locked', 0, NULL, '${now}')`),
71
+ ).rejects.toThrow()
70
72
 
71
73
  // active token with used_at set is rejected
72
74
  await db.exec(
@@ -58,21 +58,23 @@ type Validated<V extends Validate> = {
58
58
  readonly [K in 'params' | 'query' | 'body' | 'headers' as V[K] extends StandardSchema ? K : never]: V[K] extends StandardSchema ? SchemaOutput<V[K]> : never
59
59
  }
60
60
 
61
+ export async function validateRequest<const V extends Validate>(event: RequestEvent, validate: V): Promise<Validated<V>> {
62
+ const onError = validate.onError ?? validationFailed
63
+ const input: Record<string, unknown> = {}
64
+ if (validate.headers) input.headers = await validateInput(validate.headers, Object.fromEntries(event.req.headers), onError)
65
+ if (validate.params) input.params = await getValidatedRouterParams(event, validate.params, { onError })
66
+ if (validate.query) input.query = await validateInput(validate.query, getQuery(event), onError)
67
+ if (validate.body) input.body = await validateInput(validate.body, await readLimitedJson(event), onError)
68
+
69
+ // Each part was validated by its own schema above; TypeScript cannot narrow into the mapped type.
70
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
71
+ return input as Validated<V>
72
+ }
73
+
61
74
  export function defineValidatedHandler<const V extends Validate, Result>(def: {
62
75
  readonly validate: V
63
76
  readonly handler: (event: RequestEvent, input: Validated<V>) => Result | Promise<Result>
64
77
  }) {
65
78
  const { validate, handler } = def
66
- const onError = validate.onError ?? validationFailed
67
- return defineHandler(async (event) => {
68
- const input: Record<string, unknown> = {}
69
- if (validate.headers) input.headers = await validateInput(validate.headers, Object.fromEntries(event.req.headers), onError)
70
- if (validate.params) input.params = await getValidatedRouterParams(event, validate.params, { onError })
71
- if (validate.query) input.query = await validateInput(validate.query, getQuery(event), onError)
72
- if (validate.body) input.body = await validateInput(validate.body, await readLimitedJson(event), onError)
73
-
74
- // Each part was validated by its own schema above; TypeScript cannot narrow into the mapped type.
75
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion
76
- return handler(event, input as Validated<V>)
77
- })
79
+ return defineHandler(async (event) => handler(event, await validateRequest(event, validate)))
78
80
  }
@@ -30,4 +30,5 @@ export class TooLarge extends Schema.TaggedError<TooLarge, { readonly brand: uni
30
30
 
31
31
  export class RateLimited extends Schema.TaggedError<RateLimited, { readonly brand: unique symbol }>()('RateLimited', {
32
32
  message: Schema.String,
33
+ retryAfterSeconds: Schema.optionalKey(Schema.Int),
33
34
  }) {}
@@ -5,7 +5,7 @@ declare module '#auth-utils' {
5
5
  }
6
6
 
7
7
  interface SecureSessionData {
8
- /** The row in `sessions` this cookie stands for. Verified on every request so revocation takes effect immediately. */
8
+ /** The row in `sessions` this cookie stands for. Verified against D1 (with a 3s in-isolate cache) so revocation takes effect promptly. */
9
9
  sessionId: string
10
10
  }
11
11
  }
@@ -4,9 +4,6 @@ import { EmailField, NonEmptyPasswordField, PasswordField, SecondFactorCodeField
4
4
 
5
5
  export const EMAIL_CHANGE_DAYS = 14
6
6
 
7
- /** A relative path on this site: starts with one `/`, never `//`, never contains backslashes or control characters. */
8
- export const isLocalPath = (path: string): boolean => /^\/(?!\/)[^\r\n\\]*$/.test(path)
9
-
10
7
  export const EmailStatus = Schema.Literals(['unverified', 'verified'])
11
8
  export type EmailStatus = typeof EmailStatus.Type
12
9
 
@@ -26,7 +23,7 @@ export const LoginBody = Schema.Struct({
26
23
  })
27
24
 
28
25
  export const LoginTotpBody = Schema.Struct({
29
- challenge: Schema.String.check(Schema.isNonEmpty({ message: 'Missing sign-in challenge.' }), Schema.isMaxLength(2048)),
26
+ challenge: Schema.optionalKey(Schema.String.check(Schema.isNonEmpty({ message: 'Missing sign-in challenge.' }), Schema.isMaxLength(2048))),
30
27
  code: SecondFactorCodeField,
31
28
  })
32
29
 
@@ -74,6 +71,8 @@ export const UpdateProfileBody = Schema.Struct({
74
71
 
75
72
  export const DeleteAccountBody = Schema.Struct({
76
73
  confirm: Schema.Trim.check(Schema.isNonEmpty({ message: 'Type your username to confirm.' }), Schema.isMaxLength(64)),
74
+ password: Schema.optionalKey(NonEmptyPasswordField),
75
+ code: Schema.optionalKey(SecondFactorCodeField),
77
76
  })
78
77
 
79
78
  export const TokenBody = Schema.Struct({
@@ -92,6 +91,13 @@ export const ResetConfirmBody = Schema.Struct({
92
91
  export const EmailChangeRequestBody = Schema.Struct({
93
92
  email: EmailField,
94
93
  password: NonEmptyPasswordField,
94
+ code: Schema.optionalKey(SecondFactorCodeField),
95
+ })
96
+
97
+ export const SetPasswordBody = Schema.Struct({
98
+ currentPassword: Schema.optionalKey(NonEmptyPasswordField),
99
+ newPassword: PasswordField,
100
+ code: Schema.optionalKey(SecondFactorCodeField),
95
101
  })
96
102
 
97
103
  export const SessionSummaryView = Schema.Struct({
@@ -0,0 +1,2 @@
1
+ /** A relative path on this site: starts with one `/`, never `//`, never contains backslashes or control characters. */
2
+ export const isLocalPath = (path: string): boolean => /^\/(?!\/)[^\r\n\\]*$/.test(path)
@@ -27,7 +27,7 @@ export const NonEmptyPasswordField = Schema.String.check(
27
27
 
28
28
  export const TokenField = Schema.Trim.check(Schema.isPattern(/^[0-9a-f]{64}$/, { message: 'Invalid token.' }))
29
29
 
30
- /** A 6-digit authenticator code or a 10-hex recovery code (with optional dash/spaces). */
30
+ /** A 6-digit authenticator code or a 20-hex recovery code (with optional dashes/spaces). */
31
31
  export const SecondFactorCodeField = Schema.Trim.check(
32
32
  Schema.isNonEmpty({ message: 'Enter a verification code.' }),
33
33
  Schema.isMaxLength(32, { message: 'That code is too long.' }),
@@ -37,6 +37,23 @@
37
37
  }
38
38
  ]
39
39
  },
40
+ "ratelimits": [
41
+ {
42
+ "name": "RL_SIGN_IN",
43
+ "namespace_id": "1001",
44
+ "simple": { "limit": 30, "period": 60 }
45
+ },
46
+ {
47
+ "name": "RL_REGISTER",
48
+ "namespace_id": "1002",
49
+ "simple": { "limit": 20, "period": 60 }
50
+ },
51
+ {
52
+ "name": "RL_RESET",
53
+ "namespace_id": "1003",
54
+ "simple": { "limit": 5, "period": 60 }
55
+ }
56
+ ],
40
57
  "exports": {
41
58
  "RateLimitCounter": {
42
59
  "type": "durable-object",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@humanonlyweb/create-nuxtflare",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "description": "Scaffold a Nuxt 5 + Cloudflare Workers + D1 + Effect v4 app with explicit auth, TOTP 2FA, and admin.",
5
5
  "license": "MIT",
6
6
  "repository": {