@exortek/challenge 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.
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Token codec for `@exortek/challenge`.
3
+ *
4
+ * Format:
5
+ *
6
+ * <prefix>.<base64url(JSON payload)>.<base64url(HMAC-SHA256 tag)>
7
+ *
8
+ * The default prefix is {@link DEFAULT_PREFIX} (`chall_v1`) — deliberately
9
+ * unlike a JWT so the two token families cannot be confused at a call
10
+ * site. Callers may override it (e.g. `'server_challenge'`,
11
+ * `'myapp_v1'`) to brand the wire format for a specific service; the
12
+ * same prefix must be used at create and verify time, or verification
13
+ * returns `reason: 'malformed'`.
14
+ *
15
+ * HMAC covers `<prefix>.<b64u payload>` — the same string the caller
16
+ * received minus the trailing tag. Any change to prefix or payload
17
+ * invalidates the signature; a token minted with one prefix cannot be
18
+ * accepted under another even by the same secret.
19
+ */
20
+ export declare const DEFAULT_PREFIX = "chall_v1";
21
+ /**
22
+ * Assert a prefix is well-shaped. Throws via the caller's `invalidArg`
23
+ * function so `createChallenge` / `verifyChallenge` can raise
24
+ * `ChallengeError` with the right code — this module stays free of the
25
+ * error class dependency.
26
+ *
27
+ * @param {string} prefix
28
+ * @param {string} name
29
+ * @param {(msg: string) => Error} invalidArg
30
+ * @returns {string}
31
+ */
32
+ export declare function assertPrefix(prefix: string, name: string, invalidArg: (msg: string) => Error): string;
33
+ /**
34
+ * Random ID for the token's `jti` claim — 128 bits of entropy, encoded
35
+ * as 22 base64url characters. Used as the store key for single-use
36
+ * enforcement, so it must be unpredictable and unique per token.
37
+ *
38
+ * @returns {string}
39
+ */
40
+ export declare function newJti(): string;
41
+ /**
42
+ * Sign a payload with `secret` and return the compact token string.
43
+ *
44
+ * @param {object} payload
45
+ * @param {Buffer} secret 32+ raw bytes; caller validates length.
46
+ * @param {string} [prefix] Wire prefix; defaults to {@link DEFAULT_PREFIX}.
47
+ * @returns {string}
48
+ */
49
+ export declare function sign(payload: object, secret: Buffer, prefix?: string): string;
50
+ /**
51
+ * Parse + HMAC-verify a token. Returns the decoded payload on success,
52
+ * or a reason string on any failure. Never throws on user-input
53
+ * problems — a wrong token is a normal auth outcome.
54
+ *
55
+ * @param {string} token
56
+ * @param {Buffer} secret
57
+ * @param {string} [prefix] Expected prefix; defaults to {@link DEFAULT_PREFIX}.
58
+ * @returns {{ payload: object } | { reason: 'malformed' | 'bad_signature' }}
59
+ */
60
+ export declare function decode(token: string, secret: Buffer, prefix?: string): {
61
+ payload: object;
62
+ } | {
63
+ reason: 'malformed' | 'bad_signature';
64
+ };
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@exortek/challenge",
3
+ "version": "1.0.0",
4
+ "description": "Signed, single-use challenge tokens for multi-step auth flows on Node.js — carries userId / method / step / metadata across a redirect or a second request without a server-side session; HMAC-signed, expiring, optional IP-binding, single-use enforcement via any @exortek/security store.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.cjs"
15
+ },
16
+ "./stores": {
17
+ "types": "./dist/stores/index.d.ts",
18
+ "import": "./dist/stores.mjs",
19
+ "require": "./dist/stores.cjs"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
27
+ "scripts": {
28
+ "build": "rm -rf dist tsconfig.tsbuildinfo && rollup -c rollup.config.js && tsc -p tsconfig.json",
29
+ "build:watch": "rollup -c rollup.config.js --watch",
30
+ "build:types": "tsc -p tsconfig.json",
31
+ "typecheck": "tsc -p tsconfig.json --noEmit",
32
+ "test": "node --test 'tests/**/*.test.js'",
33
+ "test:coverage": "node --test --experimental-test-coverage 'tests/**/*.test.js'",
34
+ "clean": "rm -rf dist tsconfig.tsbuildinfo",
35
+ "prepack": "cp ../../LICENSE ./LICENSE"
36
+ },
37
+ "keywords": [
38
+ "backend",
39
+ "security",
40
+ "auth",
41
+ "mfa",
42
+ "2fa",
43
+ "challenge",
44
+ "multi-step",
45
+ "signed-token",
46
+ "hmac",
47
+ "single-use",
48
+ "flow-token",
49
+ "step-up",
50
+ "node-crypto"
51
+ ],
52
+ "license": "MIT",
53
+ "homepage": "https://github.com/ExorTek/auth/tree/master/packages/challenge#readme",
54
+ "repository": {
55
+ "type": "git",
56
+ "url": "git+https://github.com/ExorTek/auth.git",
57
+ "directory": "packages/challenge"
58
+ },
59
+ "bugs": {
60
+ "url": "https://github.com/ExorTek/auth/issues"
61
+ },
62
+ "engines": {
63
+ "node": ">=22.0.0"
64
+ },
65
+ "devDependencies": {
66
+ "@exortek/shared": "0.0.0"
67
+ },
68
+ "publishConfig": {
69
+ "access": "public"
70
+ }
71
+ }