@crombolo/auth 0.33.1 → 0.35.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/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # @crombolo/auth
2
+
3
+ **Internal to crombolo.** This package is published because
4
+ [`@crombolo/crombolo`](https://www.npmjs.com/package/@crombolo/crombolo)
5
+ depends on it, and its exports can change in any release, a patch included.
6
+
7
+ Import from `@crombolo/crombolo` instead:
8
+
9
+ - `@crombolo/crombolo` for a site: its config, its boot and its views.
10
+ - `@crombolo/crombolo/bundle` for a server bundle's author.
11
+ - `@crombolo/crombolo/host` for whoever runs sites.
12
+
13
+ Those three entry points carry the compatibility promise in
14
+ [`docs/api.md`](https://github.com/kineco-au/crombolo-cms/blob/main/docs/api.md).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@crombolo/auth",
3
- "version": "0.33.1",
4
- "description": "OAuth2 authorization server, password hashing, cookies and member tickets for crombolo.",
3
+ "version": "0.35.2",
4
+ "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. OAuth2 authorization server, password hashing, cookies and member tickets for crombolo.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
7
7
  "homepage": "https://github.com/kineco-au/crombolo-cms#readme",
@@ -28,8 +28,8 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.33.1",
32
- "@crombolo/data": "0.33.1",
31
+ "@crombolo/core": "0.35.2",
32
+ "@crombolo/data": "0.35.2",
33
33
  "jose": "^6.2.12",
34
34
  "uqr": "^0.1.3"
35
35
  }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * The anti-forgery token a member's browser sends with a cookie-authenticated
3
+ * write to a bundle's site route (story 186).
4
+ *
5
+ * An HMAC over the member ticket's member, security stamp and expiry, so
6
+ * nothing is stored: any node holding the signing key can check it, and it
7
+ * stops matching when the session ends or the stamp rotates. Another site can
8
+ * make the browser send the cookie but cannot read the page to learn the token.
9
+ */
10
+ import type { MemberTicket } from './member-session.ts'
11
+
12
+ const ENCODER = new TextEncoder()
13
+ /** Keeps a token from ever matching another HMAC made with the same key. */
14
+ const PURPOSE = 'crombolo-antiforgery'
15
+
16
+ const base64url = (bytes: Uint8Array): string =>
17
+ btoa(String.fromCharCode(...bytes))
18
+ .replaceAll('+', '-')
19
+ .replaceAll('/', '_')
20
+ .replace(/=+$/, '')
21
+
22
+ const fromBase64url = (value: string): Uint8Array => {
23
+ const padded = value.replaceAll('-', '+').replaceAll('_', '/')
24
+ const binary = atob(padded + '='.repeat((4 - (padded.length % 4)) % 4))
25
+ return Uint8Array.from(binary, (c) => c.charCodeAt(0))
26
+ }
27
+
28
+ const hmacKey = (secret: string) =>
29
+ crypto.subtle.importKey('raw', ENCODER.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, [
30
+ 'sign',
31
+ 'verify',
32
+ ])
33
+
34
+ const material = (ticket: MemberTicket) =>
35
+ ENCODER.encode(`${PURPOSE}\n${ticket.id.toLowerCase()}\n${ticket.stamp}\n${ticket.expires}`)
36
+
37
+ /** The token for a member's session. */
38
+ export async function antiforgeryToken(secret: string, ticket: MemberTicket): Promise<string> {
39
+ const signature = await crypto.subtle.sign('HMAC', await hmacKey(secret), material(ticket))
40
+ return base64url(new Uint8Array(signature))
41
+ }
42
+
43
+ /** Whether `token` was made for this session; compared in constant time. */
44
+ export async function verifyAntiforgery(
45
+ secret: string,
46
+ ticket: MemberTicket,
47
+ token: string | null | undefined,
48
+ ): Promise<boolean> {
49
+ if (!token || !/^[A-Za-z0-9_-]{43}$/.test(token)) return false
50
+ return crypto.subtle.verify(
51
+ 'HMAC',
52
+ await hmacKey(secret),
53
+ fromBase64url(token) as unknown as ArrayBuffer,
54
+ material(ticket),
55
+ )
56
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './admin.ts'
2
+ export * from './antiforgery.ts'
2
3
  export * from './cookies.ts'
3
4
  export * from './device.ts'
4
5
  export * from './member-session.ts'