@tomato414941/foundation 0.27.0 → 1.0.0
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/dist/cli.mjs +111 -0
- package/package.json +4 -7
- package/env-name.mjs +0 -9
- package/envelope.mjs +0 -57
- package/runtime.mjs +0 -497
- package/webauthn.mjs +0 -44
package/webauthn.mjs
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { createHash, generateKeyPairSync, createPrivateKey, randomBytes, sign } from 'node:crypto';
|
|
2
|
-
|
|
3
|
-
// A WebAuthn authenticator in software, for wherever there is no browser: it makes a credential and answers challenges
|
|
4
|
-
// with it, writing what a browser writes. The origin it signs is the one it is actually talking to, so a challenge
|
|
5
|
-
// relayed from elsewhere is refused there. It reports the user present - the principal running it asked - and never
|
|
6
|
-
// verified, since it checks no face, fingerprint or PIN.
|
|
7
|
-
const b64 = bytes => Buffer.from(bytes).toString('base64url');
|
|
8
|
-
const sha256 = data => createHash('sha256').update(data).digest();
|
|
9
|
-
const PRESENT = 0x01, ATTESTED = 0x40;
|
|
10
|
-
|
|
11
|
-
// Just enough CBOR (RFC 8949) for an attestation object and a COSE key: maps, byte and text strings, small integers.
|
|
12
|
-
function cbor(value) {
|
|
13
|
-
const head = (major, length) => length < 24 ? Buffer.from([major << 5 | length])
|
|
14
|
-
: length < 256 ? Buffer.from([major << 5 | 24, length]) : Buffer.from([major << 5 | 25, length >> 8, length & 255]);
|
|
15
|
-
if (Number.isInteger(value)) return value >= 0 ? head(0, value) : head(1, -1 - value);
|
|
16
|
-
if (Buffer.isBuffer(value)) return Buffer.concat([head(2, value.length), value]);
|
|
17
|
-
if (typeof value === 'string') { const text = Buffer.from(value); return Buffer.concat([head(3, text.length), text]); }
|
|
18
|
-
const entries = value instanceof Map ? [...value] : Object.entries(value);
|
|
19
|
-
return Buffer.concat([head(5, entries.length), ...entries.flatMap(([key, item]) => [cbor(key), cbor(item)])]);
|
|
20
|
-
}
|
|
21
|
-
const counter = count => { const bytes = Buffer.alloc(4); bytes.writeUInt32BE(count); return bytes; };
|
|
22
|
-
const clientData = (type, challenge, origin) => Buffer.from(JSON.stringify({ type, challenge, origin, crossOrigin: false }));
|
|
23
|
-
|
|
24
|
-
// Makes a credential for the options a server gave. Returns what to send back, and what to keep: the credential's id, its
|
|
25
|
-
// private key, and whose it is.
|
|
26
|
-
export function createCredential(options, origin) {
|
|
27
|
-
const { privateKey, publicKey } = generateKeyPairSync('ec', { namedCurve: 'P-256' });
|
|
28
|
-
const point = publicKey.export({ format: 'jwk' }), id = randomBytes(32);
|
|
29
|
-
const cose = cbor(new Map([[1, 2], [3, -7], [-1, 1], [-2, Buffer.from(point.x, 'base64url')], [-3, Buffer.from(point.y, 'base64url')]]));
|
|
30
|
-
const authData = Buffer.concat([sha256(options.rp.id), Buffer.from([PRESENT | ATTESTED]), counter(0), Buffer.alloc(16),
|
|
31
|
-
Buffer.from([id.length >> 8, id.length & 255]), id, cose]);
|
|
32
|
-
const response = { id: b64(id), rawId: b64(id), type: 'public-key', clientExtensionResults: {},
|
|
33
|
-
response: { clientDataJSON: b64(clientData('webauthn.create', options.challenge, origin)), attestationObject: b64(cbor({ fmt: 'none', attStmt: {}, authData })), transports: [] } };
|
|
34
|
-
return { response, credential: { id: b64(id), user: options.user.id, private_key: privateKey.export({ format: 'jwk' }) } };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Answers a sign-in challenge with a kept credential.
|
|
38
|
-
export function answer(options, credential, origin) {
|
|
39
|
-
const data = clientData('webauthn.get', options.challenge, origin);
|
|
40
|
-
const authData = Buffer.concat([sha256(options.rpId), Buffer.from([PRESENT]), counter(0)]);
|
|
41
|
-
const signature = sign('sha256', Buffer.concat([authData, sha256(data)]), createPrivateKey({ key: credential.private_key, format: 'jwk' }));
|
|
42
|
-
return { id: credential.id, rawId: credential.id, type: 'public-key', clientExtensionResults: {},
|
|
43
|
-
response: { clientDataJSON: b64(data), authenticatorData: b64(authData), signature: b64(signature), userHandle: credential.user } };
|
|
44
|
-
}
|