@mrgnw/anahtar 0.0.6
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/LICENSE +21 -0
- package/README.md +78 -0
- package/dist/components/AuthFlow.svelte +351 -0
- package/dist/components/AuthFlow.svelte.d.ts +7 -0
- package/dist/components/OtpInput.svelte +102 -0
- package/dist/components/OtpInput.svelte.d.ts +11 -0
- package/dist/components/PasskeyPrompt.svelte +168 -0
- package/dist/components/PasskeyPrompt.svelte.d.ts +8 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +4 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +15 -0
- package/dist/db/d1.d.ts +22 -0
- package/dist/db/d1.js +202 -0
- package/dist/db/postgres.d.ts +12 -0
- package/dist/db/postgres.js +204 -0
- package/dist/db/sqlite.d.ts +7 -0
- package/dist/db/sqlite.js +187 -0
- package/dist/device.d.ts +1 -0
- package/dist/device.js +36 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +14 -0
- package/dist/kit/handle.d.ts +3 -0
- package/dist/kit/handle.js +18 -0
- package/dist/kit/handlers.d.ts +8 -0
- package/dist/kit/handlers.js +183 -0
- package/dist/otp.d.ts +6 -0
- package/dist/otp.js +35 -0
- package/dist/passkey.d.ts +20 -0
- package/dist/passkey.js +112 -0
- package/dist/session.d.ts +16 -0
- package/dist/session.js +31 -0
- package/dist/types.d.ts +93 -0
- package/dist/types.js +1 -0
- package/package.json +92 -0
package/dist/session.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { sha256 } from '@oslojs/crypto/sha2';
|
|
2
|
+
import { encodeHexLowerCase } from '@oslojs/encoding';
|
|
3
|
+
import { randomBytes } from 'node:crypto';
|
|
4
|
+
export async function createSession(db, userId, config) {
|
|
5
|
+
const tokenBytes = randomBytes(32);
|
|
6
|
+
const sessionToken = encodeHexLowerCase(tokenBytes);
|
|
7
|
+
const tokenHash = encodeHexLowerCase(sha256(tokenBytes));
|
|
8
|
+
const expiresAt = Date.now() + config.sessionDuration;
|
|
9
|
+
await db.createSession(tokenHash, userId, expiresAt);
|
|
10
|
+
return { sessionToken, expiresAt };
|
|
11
|
+
}
|
|
12
|
+
export async function validateSession(db, token) {
|
|
13
|
+
if (!token || token.length !== 64 || !/^[0-9a-f]+$/.test(token))
|
|
14
|
+
return null;
|
|
15
|
+
const tokenBytes = new Uint8Array(token.match(/.{2}/g).map((b) => Number.parseInt(b, 16)));
|
|
16
|
+
const sessionId = encodeHexLowerCase(sha256(tokenBytes));
|
|
17
|
+
const row = await db.getSession(sessionId);
|
|
18
|
+
if (!row)
|
|
19
|
+
return null;
|
|
20
|
+
if (row.expiresAt < Date.now()) {
|
|
21
|
+
await db.deleteSession(sessionId);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
user: { id: row.userId, email: row.email },
|
|
26
|
+
session: { id: row.id, expiresAt: row.expiresAt },
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export async function invalidateSession(db, sessionId) {
|
|
30
|
+
await db.deleteSession(sessionId);
|
|
31
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export interface AuthUser {
|
|
2
|
+
id: string;
|
|
3
|
+
email: string;
|
|
4
|
+
skipPasskeyPrompt: boolean;
|
|
5
|
+
createdAt: number;
|
|
6
|
+
}
|
|
7
|
+
export interface SessionRecord {
|
|
8
|
+
id: string;
|
|
9
|
+
userId: string;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
}
|
|
12
|
+
export interface OTPRecord {
|
|
13
|
+
id: string;
|
|
14
|
+
email: string;
|
|
15
|
+
code: string;
|
|
16
|
+
attempts: number;
|
|
17
|
+
expiresAt: number;
|
|
18
|
+
}
|
|
19
|
+
export interface PasskeyRecord {
|
|
20
|
+
id: string;
|
|
21
|
+
credentialId: string;
|
|
22
|
+
publicKey: Uint8Array;
|
|
23
|
+
counter: number;
|
|
24
|
+
transports: string | null;
|
|
25
|
+
name: string | null;
|
|
26
|
+
createdAt: number;
|
|
27
|
+
}
|
|
28
|
+
export interface FullPasskeyRecord extends PasskeyRecord {
|
|
29
|
+
userId: string;
|
|
30
|
+
email: string;
|
|
31
|
+
}
|
|
32
|
+
export interface NewPasskey {
|
|
33
|
+
id: string;
|
|
34
|
+
userId: string;
|
|
35
|
+
credentialId: string;
|
|
36
|
+
publicKey: Uint8Array;
|
|
37
|
+
counter: number;
|
|
38
|
+
transports: string | null;
|
|
39
|
+
name: string | null;
|
|
40
|
+
}
|
|
41
|
+
export type OtpResult = {
|
|
42
|
+
ok: true;
|
|
43
|
+
} | {
|
|
44
|
+
ok: false;
|
|
45
|
+
error: 'invalid';
|
|
46
|
+
} | {
|
|
47
|
+
ok: false;
|
|
48
|
+
error: 'expired';
|
|
49
|
+
} | {
|
|
50
|
+
ok: false;
|
|
51
|
+
error: 'rate_limited';
|
|
52
|
+
attemptsLeft: number;
|
|
53
|
+
};
|
|
54
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
55
|
+
export interface AuthDB {
|
|
56
|
+
init(): MaybePromise<void>;
|
|
57
|
+
getUserByEmail(email: string): MaybePromise<AuthUser | null>;
|
|
58
|
+
createUser(email: string): MaybePromise<AuthUser>;
|
|
59
|
+
setSkipPasskeyPrompt(userId: string, skip: boolean): MaybePromise<void>;
|
|
60
|
+
createSession(tokenHash: string, userId: string, expiresAt: number): MaybePromise<void>;
|
|
61
|
+
getSession(tokenHash: string): MaybePromise<(SessionRecord & {
|
|
62
|
+
email: string;
|
|
63
|
+
}) | null>;
|
|
64
|
+
deleteSession(tokenHash: string): MaybePromise<void>;
|
|
65
|
+
storeOTP(email: string, id: string, code: string, expiresAt: number): MaybePromise<void>;
|
|
66
|
+
getLatestOTP(email: string): MaybePromise<OTPRecord | null>;
|
|
67
|
+
updateOTPAttempts(id: string, attempts: number): MaybePromise<void>;
|
|
68
|
+
deleteOTP(id: string): MaybePromise<void>;
|
|
69
|
+
deleteOTPsForEmail(email: string): MaybePromise<void>;
|
|
70
|
+
storeChallenge(challenge: string, userId: string, expiresAt: number): MaybePromise<void>;
|
|
71
|
+
consumeChallenge(challenge: string): MaybePromise<{
|
|
72
|
+
userId: string;
|
|
73
|
+
} | null>;
|
|
74
|
+
getPasskeyByCredentialId(credentialId: string): MaybePromise<FullPasskeyRecord | null>;
|
|
75
|
+
getUserPasskeys(userId: string): MaybePromise<PasskeyRecord[]>;
|
|
76
|
+
storePasskey(passkey: NewPasskey): MaybePromise<void>;
|
|
77
|
+
updatePasskeyCounter(id: string, counter: number): MaybePromise<void>;
|
|
78
|
+
deletePasskey(id: string, userId: string): MaybePromise<boolean>;
|
|
79
|
+
}
|
|
80
|
+
export interface AuthConfig {
|
|
81
|
+
db: AuthDB;
|
|
82
|
+
tablePrefix?: string;
|
|
83
|
+
cookie?: string;
|
|
84
|
+
sessionDuration?: number;
|
|
85
|
+
otpExpiry?: number;
|
|
86
|
+
otpLength?: number;
|
|
87
|
+
otpMaxAttempts?: number;
|
|
88
|
+
rpName?: string;
|
|
89
|
+
onSendOTP: (email: string, code: string) => Promise<void>;
|
|
90
|
+
}
|
|
91
|
+
export interface ResolvedConfig extends Required<Omit<AuthConfig, 'onSendOTP'>> {
|
|
92
|
+
onSendOTP: (email: string, code: string) => Promise<void>;
|
|
93
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mrgnw/anahtar",
|
|
3
|
+
"version": "0.0.6",
|
|
4
|
+
"description": "Opinionated, reusable auth for SvelteKit. Email+OTP + passkeys.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/mrgnw/anahtar.git"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"registry": "https://registry.npmjs.org/",
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "svelte-package",
|
|
17
|
+
"check": "svelte-check --tsconfig ./tsconfig.json",
|
|
18
|
+
"test": "vitest run --config vitest.unit.ts && vitest run --config vitest.browser.ts",
|
|
19
|
+
"test:unit": "vitest run --config vitest.unit.ts",
|
|
20
|
+
"test:browser": "vitest run --config vitest.browser.ts",
|
|
21
|
+
"prepublishOnly": "pnpm build"
|
|
22
|
+
},
|
|
23
|
+
"svelte": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"svelte": "./dist/index.js",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./sqlite": {
|
|
32
|
+
"types": "./dist/db/sqlite.d.ts",
|
|
33
|
+
"default": "./dist/db/sqlite.js"
|
|
34
|
+
},
|
|
35
|
+
"./postgres": {
|
|
36
|
+
"types": "./dist/db/postgres.d.ts",
|
|
37
|
+
"default": "./dist/db/postgres.js"
|
|
38
|
+
},
|
|
39
|
+
"./d1": {
|
|
40
|
+
"types": "./dist/db/d1.d.ts",
|
|
41
|
+
"default": "./dist/db/d1.js"
|
|
42
|
+
},
|
|
43
|
+
"./components": {
|
|
44
|
+
"types": "./dist/components/index.d.ts",
|
|
45
|
+
"svelte": "./dist/components/index.js",
|
|
46
|
+
"default": "./dist/components/index.js"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"dist",
|
|
51
|
+
"!dist/**/*.test.*"
|
|
52
|
+
],
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@simplewebauthn/browser": "^13.0.0",
|
|
55
|
+
"@sveltejs/kit": "^2.0.0",
|
|
56
|
+
"svelte": "^5.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"svelte": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"@simplewebauthn/browser": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"@oslojs/crypto": "^1.0.1",
|
|
68
|
+
"@oslojs/encoding": "^1.1.0",
|
|
69
|
+
"@simplewebauthn/server": "^13.2.2"
|
|
70
|
+
},
|
|
71
|
+
"pnpm": {
|
|
72
|
+
"onlyBuiltDependencies": [
|
|
73
|
+
"better-sqlite3"
|
|
74
|
+
]
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@simplewebauthn/browser": "^13.2.2",
|
|
78
|
+
"@sveltejs/kit": "^2.31.1",
|
|
79
|
+
"@sveltejs/package": "^2.3.7",
|
|
80
|
+
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
|
81
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
82
|
+
"@testing-library/svelte": "^5.3.1",
|
|
83
|
+
"@testing-library/user-event": "^14.6.1",
|
|
84
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
85
|
+
"better-sqlite3": "^12.6.2",
|
|
86
|
+
"happy-dom": "^20.6.1",
|
|
87
|
+
"svelte": "^5.38.1",
|
|
88
|
+
"svelte-check": "^4.3.1",
|
|
89
|
+
"typescript": "^5.9.2",
|
|
90
|
+
"vitest": "^3.2.1"
|
|
91
|
+
}
|
|
92
|
+
}
|