@crombolo/auth 0.34.0 → 0.36.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/package.json +3 -3
- package/src/antiforgery.ts +56 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.2",
|
|
4
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",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@crombolo/core": "0.
|
|
32
|
-
"@crombolo/data": "0.
|
|
31
|
+
"@crombolo/core": "0.36.2",
|
|
32
|
+
"@crombolo/data": "0.36.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
|
+
}
|