@hammadj/better-auth-passkey 1.5.0-beta.9

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.md ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2024 - present, Bereket Engida
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the “Software”), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8
+ the Software, and to permit persons to whom the Software is furnished to do so,
9
+ subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
+ DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Better Auth Passkey Plugin
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ # Using npm
7
+ npm install better-auth @better-auth/passkey
8
+
9
+ # Using yarn
10
+ yarn add better-auth @better-auth/passkey
11
+
12
+ # Using pnpm
13
+ pnpm add better-auth @better-auth/passkey
14
+
15
+ # Using bun
16
+ bun add better-auth @better-auth/passkey
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Server
22
+
23
+ ```typescript
24
+ import { betterAuth } from 'better-auth';
25
+ import { passkey } from '@better-auth/passkey';
26
+
27
+ export const auth = betterAuth({
28
+ plugins: [
29
+ passkey({
30
+ rpID: 'example.com',
31
+ rpName: 'My App',
32
+ }),
33
+ ],
34
+ });
35
+ ```
36
+
37
+ ### Client
38
+
39
+ ```typescript
40
+ import { createAuthClient } from 'better-auth/client';
41
+ import { passkeyClient } from '@better-auth/passkey/client';
42
+
43
+ export const authClient = createAuthClient({
44
+ plugins: [passkeyClient()],
45
+ });
46
+ ```
47
+
48
+ ## Documentation
49
+
50
+ For more information, visit the [Better Auth Passkey documentation](https://better-auth.com/docs/plugins/passkey).
51
+
52
+ ## License
53
+
54
+ MIT
@@ -0,0 +1,101 @@
1
+ import { n as PasskeyOptions, r as WebAuthnChallengeValue, t as Passkey } from "./types-DYj_eloX.mjs";
2
+ import { atom } from "nanostores";
3
+ import { ClientFetchOption, ClientStore } from "@better-auth/core";
4
+ import { BetterFetch } from "@better-fetch/fetch";
5
+ import { Session, User } from "better-auth/types";
6
+ export * from "@simplewebauthn/server";
7
+
8
+ //#region src/error-codes.d.ts
9
+ declare const PASSKEY_ERROR_CODES: any;
10
+ //#endregion
11
+ //#region src/client.d.ts
12
+ declare const getPasskeyActions: ($fetch: BetterFetch, {
13
+ $listPasskeys,
14
+ $store
15
+ }: {
16
+ $listPasskeys: ReturnType<typeof atom<any>>;
17
+ $store: ClientStore;
18
+ }) => {
19
+ signIn: {
20
+ /**
21
+ * Sign in with a registered passkey
22
+ */
23
+ passkey: (opts?: {
24
+ autoFill?: boolean;
25
+ fetchOptions?: ClientFetchOption;
26
+ } | undefined, options?: ClientFetchOption | undefined) => Promise<{
27
+ data: null;
28
+ error: {
29
+ message?: string | undefined;
30
+ status: number;
31
+ statusText: string;
32
+ };
33
+ } | {
34
+ data: {
35
+ session: Session;
36
+ user: User;
37
+ };
38
+ error: null;
39
+ } | {
40
+ data: null;
41
+ error: {
42
+ code: string;
43
+ message: string;
44
+ status: number;
45
+ statusText: string;
46
+ };
47
+ }>;
48
+ };
49
+ passkey: {
50
+ /**
51
+ * Add a passkey to the user account
52
+ */
53
+ addPasskey: (opts?: {
54
+ fetchOptions?: ClientFetchOption;
55
+ /**
56
+ * The name of the passkey. This is used to
57
+ * identify the passkey in the UI.
58
+ */
59
+ name?: string;
60
+ /**
61
+ * The type of attachment for the passkey. Defaults to both
62
+ * platform and cross-platform allowed, with platform preferred.
63
+ */
64
+ authenticatorAttachment?: "platform" | "cross-platform";
65
+ /**
66
+ * Try to silently create a passkey with the password manager that the user just signed
67
+ * in with.
68
+ * @default false
69
+ */
70
+ useAutoRegister?: boolean;
71
+ } | undefined, fetchOpts?: ClientFetchOption | undefined) => Promise<{
72
+ data: null;
73
+ error: {
74
+ message?: string | undefined;
75
+ status: number;
76
+ statusText: string;
77
+ };
78
+ } | {
79
+ data: Passkey;
80
+ error: null;
81
+ } | {
82
+ data: null;
83
+ error: {
84
+ code: string;
85
+ message: string;
86
+ status: number;
87
+ statusText: string;
88
+ };
89
+ }>;
90
+ };
91
+ /**
92
+ * Inferred Internal Types
93
+ */
94
+ $Infer: {
95
+ Passkey: Passkey;
96
+ };
97
+ };
98
+ declare const passkeyClient: () => BetterAuthClientPlugin;
99
+ //#endregion
100
+ export { PASSKEY_ERROR_CODES, Passkey, PasskeyOptions, WebAuthnChallengeValue, getPasskeyActions, passkeyClient };
101
+ //# sourceMappingURL=client.d.mts.map
@@ -0,0 +1,150 @@
1
+ import { t as PASSKEY_ERROR_CODES } from "./error-codes-Dvu2mv33.mjs";
2
+ import { WebAuthnError, startAuthentication, startRegistration } from "@simplewebauthn/browser";
3
+ import { useAuthQuery } from "better-auth/client";
4
+ import { atom } from "nanostores";
5
+
6
+ //#region src/client.ts
7
+ const getPasskeyActions = ($fetch, { $listPasskeys, $store }) => {
8
+ const signInPasskey = async (opts, options) => {
9
+ const response = await $fetch("/passkey/generate-authenticate-options", {
10
+ method: "GET",
11
+ throw: false
12
+ });
13
+ if (!response.data) return response;
14
+ try {
15
+ const verified = await $fetch("/passkey/verify-authentication", {
16
+ body: { response: await startAuthentication({
17
+ optionsJSON: response.data,
18
+ useBrowserAutofill: opts?.autoFill
19
+ }) },
20
+ ...opts?.fetchOptions,
21
+ ...options,
22
+ method: "POST",
23
+ throw: false
24
+ });
25
+ $listPasskeys.set(Math.random());
26
+ $store.notify("$sessionSignal");
27
+ return verified;
28
+ } catch (err) {
29
+ console.error(`[Better Auth] Error verifying passkey`, err);
30
+ return {
31
+ data: null,
32
+ error: {
33
+ code: "AUTH_CANCELLED",
34
+ message: "auth cancelled",
35
+ status: 400,
36
+ statusText: "BAD_REQUEST"
37
+ }
38
+ };
39
+ }
40
+ };
41
+ const registerPasskey = async (opts, fetchOpts) => {
42
+ const options = await $fetch("/passkey/generate-register-options", {
43
+ method: "GET",
44
+ query: {
45
+ ...opts?.authenticatorAttachment && { authenticatorAttachment: opts.authenticatorAttachment },
46
+ ...opts?.name && { name: opts.name }
47
+ },
48
+ throw: false
49
+ });
50
+ if (!options.data) return options;
51
+ try {
52
+ const res = await startRegistration({
53
+ optionsJSON: options.data,
54
+ useAutoRegister: opts?.useAutoRegister
55
+ });
56
+ const verified = await $fetch("/passkey/verify-registration", {
57
+ ...opts?.fetchOptions,
58
+ ...fetchOpts,
59
+ body: {
60
+ response: res,
61
+ name: opts?.name
62
+ },
63
+ method: "POST",
64
+ throw: false
65
+ });
66
+ if (!verified.data) return verified;
67
+ $listPasskeys.set(Math.random());
68
+ return verified;
69
+ } catch (e) {
70
+ if (e instanceof WebAuthnError) {
71
+ if (e.code === "ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED") return {
72
+ data: null,
73
+ error: {
74
+ code: e.code,
75
+ message: "previously registered",
76
+ status: 400,
77
+ statusText: "BAD_REQUEST"
78
+ }
79
+ };
80
+ if (e.code === "ERROR_CEREMONY_ABORTED") return {
81
+ data: null,
82
+ error: {
83
+ code: e.code,
84
+ message: "registration cancelled",
85
+ status: 400,
86
+ statusText: "BAD_REQUEST"
87
+ }
88
+ };
89
+ return {
90
+ data: null,
91
+ error: {
92
+ code: e.code,
93
+ message: e.message,
94
+ status: 400,
95
+ statusText: "BAD_REQUEST"
96
+ }
97
+ };
98
+ }
99
+ return {
100
+ data: null,
101
+ error: {
102
+ code: "UNKNOWN_ERROR",
103
+ message: e instanceof Error ? e.message : "unknown error",
104
+ status: 500,
105
+ statusText: "INTERNAL_SERVER_ERROR"
106
+ }
107
+ };
108
+ }
109
+ };
110
+ return {
111
+ signIn: { passkey: signInPasskey },
112
+ passkey: { addPasskey: registerPasskey },
113
+ $Infer: {}
114
+ };
115
+ };
116
+ const passkeyClient = () => {
117
+ const $listPasskeys = atom();
118
+ return {
119
+ id: "passkey",
120
+ $InferServerPlugin: {},
121
+ getActions: ($fetch, $store) => getPasskeyActions($fetch, {
122
+ $listPasskeys,
123
+ $store
124
+ }),
125
+ getAtoms($fetch) {
126
+ return {
127
+ listPasskeys: useAuthQuery($listPasskeys, "/passkey/list-user-passkeys", $fetch, { method: "GET" }),
128
+ $listPasskeys
129
+ };
130
+ },
131
+ pathMethods: {
132
+ "/passkey/register": "POST",
133
+ "/passkey/authenticate": "POST"
134
+ },
135
+ atomListeners: [{
136
+ matcher(path) {
137
+ return path === "/passkey/verify-registration" || path === "/passkey/delete-passkey" || path === "/passkey/update-passkey" || path === "/sign-out";
138
+ },
139
+ signal: "$listPasskeys"
140
+ }, {
141
+ matcher: (path) => path === "/passkey/verify-authentication",
142
+ signal: "$sessionSignal"
143
+ }],
144
+ $ERROR_CODES: PASSKEY_ERROR_CODES
145
+ };
146
+ };
147
+
148
+ //#endregion
149
+ export { PASSKEY_ERROR_CODES, getPasskeyActions, passkeyClient };
150
+ //# sourceMappingURL=client.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["import type {\n\tBetterAuthClientPlugin,\n\tClientFetchOption,\n\tClientStore,\n} from \"@better-auth/core\";\nimport type { BetterFetch } from \"@better-fetch/fetch\";\nimport type {\n\tPublicKeyCredentialCreationOptionsJSON,\n\tPublicKeyCredentialRequestOptionsJSON,\n} from \"@simplewebauthn/browser\";\nimport {\n\tstartAuthentication,\n\tstartRegistration,\n\tWebAuthnError,\n} from \"@simplewebauthn/browser\";\nimport { useAuthQuery } from \"better-auth/client\";\nimport type { Session, User } from \"better-auth/types\";\nimport { atom } from \"nanostores\";\nimport type { passkey } from \".\";\nimport { PASSKEY_ERROR_CODES } from \"./error-codes\";\nimport type { Passkey } from \"./types\";\n\nexport const getPasskeyActions = (\n\t$fetch: BetterFetch,\n\t{\n\t\t$listPasskeys,\n\t\t$store,\n\t}: {\n\t\t$listPasskeys: ReturnType<typeof atom<any>>;\n\t\t$store: ClientStore;\n\t},\n) => {\n\tconst signInPasskey = async (\n\t\topts?:\n\t\t\t| {\n\t\t\t\t\tautoFill?: boolean;\n\t\t\t\t\tfetchOptions?: ClientFetchOption;\n\t\t\t }\n\t\t\t| undefined,\n\t\toptions?: ClientFetchOption | undefined,\n\t) => {\n\t\tconst response = await $fetch<PublicKeyCredentialRequestOptionsJSON>(\n\t\t\t\"/passkey/generate-authenticate-options\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tthrow: false,\n\t\t\t},\n\t\t);\n\t\tif (!response.data) {\n\t\t\treturn response;\n\t\t}\n\t\ttry {\n\t\t\tconst res = await startAuthentication({\n\t\t\t\toptionsJSON: response.data,\n\t\t\t\tuseBrowserAutofill: opts?.autoFill,\n\t\t\t});\n\t\t\tconst verified = await $fetch<{\n\t\t\t\tsession: Session;\n\t\t\t\tuser: User;\n\t\t\t}>(\"/passkey/verify-authentication\", {\n\t\t\t\tbody: {\n\t\t\t\t\tresponse: res,\n\t\t\t\t},\n\t\t\t\t...opts?.fetchOptions,\n\t\t\t\t...options,\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tthrow: false,\n\t\t\t});\n\t\t\t$listPasskeys.set(Math.random());\n\t\t\t$store.notify(\"$sessionSignal\");\n\n\t\t\treturn verified;\n\t\t} catch (err) {\n\t\t\t// Error logs ran on the front-end\n\t\t\tconsole.error(`[Better Auth] Error verifying passkey`, err);\n\t\t\treturn {\n\t\t\t\tdata: null,\n\t\t\t\terror: {\n\t\t\t\t\tcode: \"AUTH_CANCELLED\",\n\t\t\t\t\tmessage: \"auth cancelled\",\n\t\t\t\t\tstatus: 400,\n\t\t\t\t\tstatusText: \"BAD_REQUEST\",\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t};\n\n\tconst registerPasskey = async (\n\t\topts?:\n\t\t\t| {\n\t\t\t\t\tfetchOptions?: ClientFetchOption;\n\t\t\t\t\t/**\n\t\t\t\t\t * The name of the passkey. This is used to\n\t\t\t\t\t * identify the passkey in the UI.\n\t\t\t\t\t */\n\t\t\t\t\tname?: string;\n\n\t\t\t\t\t/**\n\t\t\t\t\t * The type of attachment for the passkey. Defaults to both\n\t\t\t\t\t * platform and cross-platform allowed, with platform preferred.\n\t\t\t\t\t */\n\t\t\t\t\tauthenticatorAttachment?: \"platform\" | \"cross-platform\";\n\n\t\t\t\t\t/**\n\t\t\t\t\t * Try to silently create a passkey with the password manager that the user just signed\n\t\t\t\t\t * in with.\n\t\t\t\t\t * @default false\n\t\t\t\t\t */\n\t\t\t\t\tuseAutoRegister?: boolean;\n\t\t\t }\n\t\t\t| undefined,\n\t\tfetchOpts?: ClientFetchOption | undefined,\n\t) => {\n\t\tconst options = await $fetch<PublicKeyCredentialCreationOptionsJSON>(\n\t\t\t\"/passkey/generate-register-options\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tquery: {\n\t\t\t\t\t...(opts?.authenticatorAttachment && {\n\t\t\t\t\t\tauthenticatorAttachment: opts.authenticatorAttachment,\n\t\t\t\t\t}),\n\t\t\t\t\t...(opts?.name && {\n\t\t\t\t\t\tname: opts.name,\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t\tthrow: false,\n\t\t\t},\n\t\t);\n\n\t\tif (!options.data) {\n\t\t\treturn options;\n\t\t}\n\t\ttry {\n\t\t\tconst res = await startRegistration({\n\t\t\t\toptionsJSON: options.data,\n\t\t\t\tuseAutoRegister: opts?.useAutoRegister,\n\t\t\t});\n\t\t\tconst verified = await $fetch<Passkey>(\"/passkey/verify-registration\", {\n\t\t\t\t...opts?.fetchOptions,\n\t\t\t\t...fetchOpts,\n\t\t\t\tbody: {\n\t\t\t\t\tresponse: res,\n\t\t\t\t\tname: opts?.name,\n\t\t\t\t},\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tthrow: false,\n\t\t\t});\n\n\t\t\tif (!verified.data) {\n\t\t\t\treturn verified;\n\t\t\t}\n\t\t\t$listPasskeys.set(Math.random());\n\t\t\treturn verified;\n\t\t} catch (e) {\n\t\t\tif (e instanceof WebAuthnError) {\n\t\t\t\tif (e.code === \"ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED\") {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata: null,\n\t\t\t\t\t\terror: {\n\t\t\t\t\t\t\tcode: e.code,\n\t\t\t\t\t\t\tmessage: \"previously registered\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\tstatusText: \"BAD_REQUEST\",\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tif (e.code === \"ERROR_CEREMONY_ABORTED\") {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata: null,\n\t\t\t\t\t\terror: {\n\t\t\t\t\t\t\tcode: e.code,\n\t\t\t\t\t\t\tmessage: \"registration cancelled\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\tstatusText: \"BAD_REQUEST\",\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tdata: null,\n\t\t\t\t\terror: {\n\t\t\t\t\t\tcode: e.code,\n\t\t\t\t\t\tmessage: e.message,\n\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\tstatusText: \"BAD_REQUEST\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tdata: null,\n\t\t\t\terror: {\n\t\t\t\t\tcode: \"UNKNOWN_ERROR\",\n\t\t\t\t\tmessage: e instanceof Error ? e.message : \"unknown error\",\n\t\t\t\t\tstatus: 500,\n\t\t\t\t\tstatusText: \"INTERNAL_SERVER_ERROR\",\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t};\n\n\treturn {\n\t\tsignIn: {\n\t\t\t/**\n\t\t\t * Sign in with a registered passkey\n\t\t\t */\n\t\t\tpasskey: signInPasskey,\n\t\t},\n\t\tpasskey: {\n\t\t\t/**\n\t\t\t * Add a passkey to the user account\n\t\t\t */\n\t\t\taddPasskey: registerPasskey,\n\t\t},\n\t\t/**\n\t\t * Inferred Internal Types\n\t\t */\n\t\t$Infer: {} as {\n\t\t\tPasskey: Passkey;\n\t\t},\n\t};\n};\n\nexport const passkeyClient = () => {\n\tconst $listPasskeys = atom<any>();\n\treturn {\n\t\tid: \"passkey\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof passkey>,\n\t\tgetActions: ($fetch, $store) =>\n\t\t\tgetPasskeyActions($fetch, {\n\t\t\t\t$listPasskeys,\n\t\t\t\t$store,\n\t\t\t}),\n\t\tgetAtoms($fetch) {\n\t\t\tconst listPasskeys = useAuthQuery<Passkey[]>(\n\t\t\t\t$listPasskeys,\n\t\t\t\t\"/passkey/list-user-passkeys\",\n\t\t\t\t$fetch,\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t},\n\t\t\t);\n\t\t\treturn {\n\t\t\t\tlistPasskeys,\n\t\t\t\t$listPasskeys,\n\t\t\t};\n\t\t},\n\t\tpathMethods: {\n\t\t\t\"/passkey/register\": \"POST\",\n\t\t\t\"/passkey/authenticate\": \"POST\",\n\t\t},\n\t\tatomListeners: [\n\t\t\t{\n\t\t\t\tmatcher(path) {\n\t\t\t\t\treturn (\n\t\t\t\t\t\tpath === \"/passkey/verify-registration\" ||\n\t\t\t\t\t\tpath === \"/passkey/delete-passkey\" ||\n\t\t\t\t\t\tpath === \"/passkey/update-passkey\" ||\n\t\t\t\t\t\tpath === \"/sign-out\"\n\t\t\t\t\t);\n\t\t\t\t},\n\t\t\t\tsignal: \"$listPasskeys\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tmatcher: (path) => path === \"/passkey/verify-authentication\",\n\t\t\t\tsignal: \"$sessionSignal\",\n\t\t\t},\n\t\t],\n\t\t$ERROR_CODES: PASSKEY_ERROR_CODES,\n\t} satisfies BetterAuthClientPlugin;\n};\n\nexport type * from \"@simplewebauthn/server\";\nexport * from \"./error-codes\";\nexport type * from \"./types\";\n"],"mappings":";;;;;;AAsBA,MAAa,qBACZ,QACA,EACC,eACA,aAKG;CACJ,MAAM,gBAAgB,OACrB,MAMA,YACI;EACJ,MAAM,WAAW,MAAM,OACtB,0CACA;GACC,QAAQ;GACR,OAAO;GACP,CACD;AACD,MAAI,CAAC,SAAS,KACb,QAAO;AAER,MAAI;GAKH,MAAM,WAAW,MAAM,OAGpB,kCAAkC;IACpC,MAAM,EACL,UATU,MAAM,oBAAoB;KACrC,aAAa,SAAS;KACtB,oBAAoB,MAAM;KAC1B,CAAC,EAOA;IACD,GAAG,MAAM;IACT,GAAG;IACH,QAAQ;IACR,OAAO;IACP,CAAC;AACF,iBAAc,IAAI,KAAK,QAAQ,CAAC;AAChC,UAAO,OAAO,iBAAiB;AAE/B,UAAO;WACC,KAAK;AAEb,WAAQ,MAAM,yCAAyC,IAAI;AAC3D,UAAO;IACN,MAAM;IACN,OAAO;KACN,MAAM;KACN,SAAS;KACT,QAAQ;KACR,YAAY;KACZ;IACD;;;CAIH,MAAM,kBAAkB,OACvB,MAuBA,cACI;EACJ,MAAM,UAAU,MAAM,OACrB,sCACA;GACC,QAAQ;GACR,OAAO;IACN,GAAI,MAAM,2BAA2B,EACpC,yBAAyB,KAAK,yBAC9B;IACD,GAAI,MAAM,QAAQ,EACjB,MAAM,KAAK,MACX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aAAa;GACZ,qBAAqB;GACrB,yBAAyB;GACzB;EACD,eAAe,CACd;GACC,QAAQ,MAAM;AACb,WACC,SAAS,kCACT,SAAS,6BACT,SAAS,6BACT,SAAS;;GAGX,QAAQ;GACR,EACD;GACC,UAAU,SAAS,SAAS;GAC5B,QAAQ;GACR,CACD;EACD,cAAc;EACd"}
@@ -0,0 +1,16 @@
1
+ import { defineErrorCodes } from "@better-auth/core/utils/error-codes";
2
+
3
+ //#region src/error-codes.ts
4
+ const PASSKEY_ERROR_CODES = defineErrorCodes({
5
+ CHALLENGE_NOT_FOUND: "Challenge not found",
6
+ YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY: "You are not allowed to register this passkey",
7
+ FAILED_TO_VERIFY_REGISTRATION: "Failed to verify registration",
8
+ PASSKEY_NOT_FOUND: "Passkey not found",
9
+ AUTHENTICATION_FAILED: "Authentication failed",
10
+ UNABLE_TO_CREATE_SESSION: "Unable to create session",
11
+ FAILED_TO_UPDATE_PASSKEY: "Failed to update passkey"
12
+ });
13
+
14
+ //#endregion
15
+ export { PASSKEY_ERROR_CODES as t };
16
+ //# sourceMappingURL=error-codes-Dvu2mv33.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-codes-Dvu2mv33.mjs","names":[],"sources":["../src/error-codes.ts"],"sourcesContent":["import { defineErrorCodes } from \"@better-auth/core/utils/error-codes\";\n\nexport const PASSKEY_ERROR_CODES = defineErrorCodes({\n\tCHALLENGE_NOT_FOUND: \"Challenge not found\",\n\tYOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY:\n\t\t\"You are not allowed to register this passkey\",\n\tFAILED_TO_VERIFY_REGISTRATION: \"Failed to verify registration\",\n\tPASSKEY_NOT_FOUND: \"Passkey not found\",\n\tAUTHENTICATION_FAILED: \"Authentication failed\",\n\tUNABLE_TO_CREATE_SESSION: \"Unable to create session\",\n\tFAILED_TO_UPDATE_PASSKEY: \"Failed to update passkey\",\n});\n"],"mappings":";;;AAEA,MAAa,sBAAsB,iBAAiB;CACnD,qBAAqB;CACrB,8CACC;CACD,+BAA+B;CAC/B,mBAAmB;CACnB,uBAAuB;CACvB,0BAA0B;CAC1B,0BAA0B;CAC1B,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { n as PasskeyOptions, t as Passkey } from "./types-DYj_eloX.mjs";
2
+
3
+ //#region src/index.d.ts
4
+ declare module "@better-auth/core" {
5
+ interface BetterAuthPluginRegistry<AuthOptions, Options> {
6
+ passkey: {
7
+ creator: typeof passkey;
8
+ };
9
+ }
10
+ }
11
+ declare const passkey: (options?: PasskeyOptions | undefined) => BetterAuthPlugin;
12
+ //#endregion
13
+ export { type Passkey, type PasskeyOptions, passkey };
14
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs ADDED
@@ -0,0 +1,626 @@
1
+ import { t as PASSKEY_ERROR_CODES } from "./error-codes-Dvu2mv33.mjs";
2
+ import { mergeSchema } from "better-auth/db";
3
+ import { createAuthEndpoint } from "@better-auth/core/api";
4
+ import { APIError } from "@better-auth/core/error";
5
+ import { base64 } from "@better-auth/utils/base64";
6
+ import { generateAuthenticationOptions, generateRegistrationOptions, verifyAuthenticationResponse, verifyRegistrationResponse } from "@simplewebauthn/server";
7
+ import { freshSessionMiddleware, getSessionFromCtx, sessionMiddleware } from "better-auth/api";
8
+ import { setSessionCookie } from "better-auth/cookies";
9
+ import { generateRandomString } from "better-auth/crypto";
10
+ import * as z from "zod";
11
+
12
+ //#region src/utils.ts
13
+ function getRpID(options, baseURL) {
14
+ return options.rpID || (baseURL ? new URL(baseURL).hostname : "localhost");
15
+ }
16
+
17
+ //#endregion
18
+ //#region src/routes.ts
19
+ const generatePasskeyQuerySchema = z.object({
20
+ authenticatorAttachment: z.enum(["platform", "cross-platform"]).optional(),
21
+ name: z.string().optional()
22
+ }).optional();
23
+ const generatePasskeyRegistrationOptions = (opts, { maxAgeInSeconds, expirationTime }) => createAuthEndpoint("/passkey/generate-register-options", {
24
+ method: "GET",
25
+ use: [freshSessionMiddleware],
26
+ query: generatePasskeyQuerySchema,
27
+ metadata: { openapi: {
28
+ operationId: "generatePasskeyRegistrationOptions",
29
+ description: "Generate registration options for a new passkey",
30
+ responses: { 200: {
31
+ description: "Success",
32
+ parameters: { query: {
33
+ authenticatorAttachment: {
34
+ description: `Type of authenticator to use for registration.
35
+ "platform" for device-specific authenticators,
36
+ "cross-platform" for authenticators that can be used across devices.`,
37
+ required: false
38
+ },
39
+ name: {
40
+ description: `Optional custom name for the passkey.
41
+ This can help identify the passkey when managing multiple credentials.`,
42
+ required: false
43
+ }
44
+ } },
45
+ content: { "application/json": { schema: {
46
+ type: "object",
47
+ properties: {
48
+ challenge: { type: "string" },
49
+ rp: {
50
+ type: "object",
51
+ properties: {
52
+ name: { type: "string" },
53
+ id: { type: "string" }
54
+ }
55
+ },
56
+ user: {
57
+ type: "object",
58
+ properties: {
59
+ id: { type: "string" },
60
+ name: { type: "string" },
61
+ displayName: { type: "string" }
62
+ }
63
+ },
64
+ pubKeyCredParams: {
65
+ type: "array",
66
+ items: {
67
+ type: "object",
68
+ properties: {
69
+ type: { type: "string" },
70
+ alg: { type: "number" }
71
+ }
72
+ }
73
+ },
74
+ timeout: { type: "number" },
75
+ excludeCredentials: {
76
+ type: "array",
77
+ items: {
78
+ type: "object",
79
+ properties: {
80
+ id: { type: "string" },
81
+ type: { type: "string" },
82
+ transports: {
83
+ type: "array",
84
+ items: { type: "string" }
85
+ }
86
+ }
87
+ }
88
+ },
89
+ authenticatorSelection: {
90
+ type: "object",
91
+ properties: {
92
+ authenticatorAttachment: { type: "string" },
93
+ requireResidentKey: { type: "boolean" },
94
+ userVerification: { type: "string" }
95
+ }
96
+ },
97
+ attestation: { type: "string" },
98
+ extensions: { type: "object" }
99
+ }
100
+ } } }
101
+ } }
102
+ } }
103
+ }, async (ctx) => {
104
+ const { session } = ctx.context;
105
+ const userPasskeys = await ctx.context.adapter.findMany({
106
+ model: "passkey",
107
+ where: [{
108
+ field: "userId",
109
+ value: session.user.id
110
+ }]
111
+ });
112
+ const userID = new TextEncoder().encode(generateRandomString(32, "a-z", "0-9"));
113
+ const options = await generateRegistrationOptions({
114
+ rpName: opts.rpName || ctx.context.appName,
115
+ rpID: getRpID(opts, ctx.context.options.baseURL),
116
+ userID,
117
+ userName: ctx.query?.name || session.user.email || session.user.id,
118
+ userDisplayName: session.user.email || session.user.id,
119
+ attestationType: "none",
120
+ excludeCredentials: userPasskeys.map((passkey) => ({
121
+ id: passkey.credentialID,
122
+ transports: passkey.transports?.split(",")
123
+ })),
124
+ authenticatorSelection: {
125
+ residentKey: "preferred",
126
+ userVerification: "preferred",
127
+ ...opts.authenticatorSelection || {},
128
+ ...ctx.query?.authenticatorAttachment ? { authenticatorAttachment: ctx.query.authenticatorAttachment } : {}
129
+ }
130
+ });
131
+ const verificationToken = generateRandomString(32);
132
+ const webAuthnCookie = ctx.context.createAuthCookie(opts.advanced.webAuthnChallengeCookie);
133
+ await ctx.setSignedCookie(webAuthnCookie.name, verificationToken, ctx.context.secret, {
134
+ ...webAuthnCookie.attributes,
135
+ maxAge: maxAgeInSeconds
136
+ });
137
+ await ctx.context.internalAdapter.createVerificationValue({
138
+ identifier: verificationToken,
139
+ value: JSON.stringify({
140
+ expectedChallenge: options.challenge,
141
+ userData: { id: session.user.id }
142
+ }),
143
+ expiresAt: expirationTime
144
+ });
145
+ return ctx.json(options, { status: 200 });
146
+ });
147
+ const generatePasskeyAuthenticationOptions = (opts, { maxAgeInSeconds, expirationTime }) => createAuthEndpoint("/passkey/generate-authenticate-options", {
148
+ method: "GET",
149
+ metadata: { openapi: {
150
+ operationId: "passkeyGenerateAuthenticateOptions",
151
+ description: "Generate authentication options for a passkey",
152
+ responses: { 200: {
153
+ description: "Success",
154
+ content: { "application/json": { schema: {
155
+ type: "object",
156
+ properties: {
157
+ challenge: { type: "string" },
158
+ rp: {
159
+ type: "object",
160
+ properties: {
161
+ name: { type: "string" },
162
+ id: { type: "string" }
163
+ }
164
+ },
165
+ user: {
166
+ type: "object",
167
+ properties: {
168
+ id: { type: "string" },
169
+ name: { type: "string" },
170
+ displayName: { type: "string" }
171
+ }
172
+ },
173
+ timeout: { type: "number" },
174
+ allowCredentials: {
175
+ type: "array",
176
+ items: {
177
+ type: "object",
178
+ properties: {
179
+ id: { type: "string" },
180
+ type: { type: "string" },
181
+ transports: {
182
+ type: "array",
183
+ items: { type: "string" }
184
+ }
185
+ }
186
+ }
187
+ },
188
+ userVerification: { type: "string" },
189
+ authenticatorSelection: {
190
+ type: "object",
191
+ properties: {
192
+ authenticatorAttachment: { type: "string" },
193
+ requireResidentKey: { type: "boolean" },
194
+ userVerification: { type: "string" }
195
+ }
196
+ },
197
+ extensions: { type: "object" }
198
+ }
199
+ } } }
200
+ } }
201
+ } }
202
+ }, async (ctx) => {
203
+ const session = await getSessionFromCtx(ctx);
204
+ let userPasskeys = [];
205
+ if (session) userPasskeys = await ctx.context.adapter.findMany({
206
+ model: "passkey",
207
+ where: [{
208
+ field: "userId",
209
+ value: session.user.id
210
+ }]
211
+ });
212
+ const options = await generateAuthenticationOptions({
213
+ rpID: getRpID(opts, ctx.context.options.baseURL),
214
+ userVerification: "preferred",
215
+ ...userPasskeys.length ? { allowCredentials: userPasskeys.map((passkey) => ({
216
+ id: passkey.credentialID,
217
+ transports: passkey.transports?.split(",")
218
+ })) } : {}
219
+ });
220
+ const data = {
221
+ expectedChallenge: options.challenge,
222
+ userData: { id: session?.user.id || "" }
223
+ };
224
+ const verificationToken = generateRandomString(32);
225
+ const webAuthnCookie = ctx.context.createAuthCookie(opts.advanced.webAuthnChallengeCookie);
226
+ await ctx.setSignedCookie(webAuthnCookie.name, verificationToken, ctx.context.secret, {
227
+ ...webAuthnCookie.attributes,
228
+ maxAge: maxAgeInSeconds
229
+ });
230
+ await ctx.context.internalAdapter.createVerificationValue({
231
+ identifier: verificationToken,
232
+ value: JSON.stringify(data),
233
+ expiresAt: expirationTime
234
+ });
235
+ return ctx.json(options, { status: 200 });
236
+ });
237
+ const verifyPasskeyRegistrationBodySchema = z.object({
238
+ response: z.any(),
239
+ name: z.string().meta({ description: "Name of the passkey" }).optional()
240
+ });
241
+ const verifyPasskeyRegistration = (options) => createAuthEndpoint("/passkey/verify-registration", {
242
+ method: "POST",
243
+ body: verifyPasskeyRegistrationBodySchema,
244
+ use: [freshSessionMiddleware],
245
+ metadata: { openapi: {
246
+ operationId: "passkeyVerifyRegistration",
247
+ description: "Verify registration of a new passkey",
248
+ responses: {
249
+ 200: {
250
+ description: "Success",
251
+ content: { "application/json": { schema: { $ref: "#/components/schemas/Passkey" } } }
252
+ },
253
+ 400: { description: "Bad request" }
254
+ }
255
+ } }
256
+ }, async (ctx) => {
257
+ const origin = options?.origin || ctx.headers?.get("origin") || "";
258
+ if (!origin) return ctx.json(null, { status: 400 });
259
+ const resp = ctx.body.response;
260
+ const webAuthnCookie = ctx.context.createAuthCookie(options.advanced.webAuthnChallengeCookie);
261
+ const verificationToken = await ctx.getSignedCookie(webAuthnCookie.name, ctx.context.secret);
262
+ if (!verificationToken) throw APIError.from("BAD_REQUEST", PASSKEY_ERROR_CODES.CHALLENGE_NOT_FOUND);
263
+ const data = await ctx.context.internalAdapter.findVerificationValue(verificationToken);
264
+ if (!data) return ctx.json(null, { status: 400 });
265
+ const { expectedChallenge, userData } = JSON.parse(data.value);
266
+ if (userData.id !== ctx.context.session.user.id) throw APIError.from("UNAUTHORIZED", PASSKEY_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY);
267
+ try {
268
+ const { verified, registrationInfo } = await verifyRegistrationResponse({
269
+ response: resp,
270
+ expectedChallenge,
271
+ expectedOrigin: origin,
272
+ expectedRPID: getRpID(options, ctx.context.options.baseURL),
273
+ requireUserVerification: false
274
+ });
275
+ if (!verified || !registrationInfo) return ctx.json(null, { status: 400 });
276
+ const { aaguid, credentialDeviceType, credentialBackedUp, credential } = registrationInfo;
277
+ const pubKey = base64.encode(credential.publicKey);
278
+ const newPasskey = {
279
+ name: ctx.body.name,
280
+ userId: userData.id,
281
+ credentialID: credential.id,
282
+ publicKey: pubKey,
283
+ counter: credential.counter,
284
+ deviceType: credentialDeviceType,
285
+ transports: resp.response.transports.join(","),
286
+ backedUp: credentialBackedUp,
287
+ createdAt: /* @__PURE__ */ new Date(),
288
+ aaguid
289
+ };
290
+ const newPasskeyRes = await ctx.context.adapter.create({
291
+ model: "passkey",
292
+ data: newPasskey
293
+ });
294
+ await ctx.context.internalAdapter.deleteVerificationValue(data.id);
295
+ return ctx.json(newPasskeyRes, { status: 200 });
296
+ } catch (e) {
297
+ ctx.context.logger.error("Failed to verify registration", e);
298
+ throw APIError.from("INTERNAL_SERVER_ERROR", PASSKEY_ERROR_CODES.FAILED_TO_VERIFY_REGISTRATION);
299
+ }
300
+ });
301
+ const verifyPasskeyAuthenticationBodySchema = z.object({ response: z.record(z.any(), z.any()) });
302
+ const verifyPasskeyAuthentication = (options) => createAuthEndpoint("/passkey/verify-authentication", {
303
+ method: "POST",
304
+ body: verifyPasskeyAuthenticationBodySchema,
305
+ metadata: {
306
+ openapi: {
307
+ operationId: "passkeyVerifyAuthentication",
308
+ description: "Verify authentication of a passkey",
309
+ responses: { 200: {
310
+ description: "Success",
311
+ content: { "application/json": { schema: {
312
+ type: "object",
313
+ properties: {
314
+ session: { $ref: "#/components/schemas/Session" },
315
+ user: { $ref: "#/components/schemas/User" }
316
+ }
317
+ } } }
318
+ } }
319
+ },
320
+ $Infer: { body: {} }
321
+ }
322
+ }, async (ctx) => {
323
+ const origin = options?.origin || ctx.headers?.get("origin") || "";
324
+ if (!origin) throw new APIError("BAD_REQUEST", { message: "origin missing" });
325
+ const resp = ctx.body.response;
326
+ const webAuthnCookie = ctx.context.createAuthCookie(options.advanced.webAuthnChallengeCookie);
327
+ const verificationToken = await ctx.getSignedCookie(webAuthnCookie.name, ctx.context.secret);
328
+ if (!verificationToken) throw APIError.from("BAD_REQUEST", PASSKEY_ERROR_CODES.CHALLENGE_NOT_FOUND);
329
+ const data = await ctx.context.internalAdapter.findVerificationValue(verificationToken);
330
+ if (!data) throw APIError.from("BAD_REQUEST", PASSKEY_ERROR_CODES.CHALLENGE_NOT_FOUND);
331
+ const { expectedChallenge } = JSON.parse(data.value);
332
+ const passkey = await ctx.context.adapter.findOne({
333
+ model: "passkey",
334
+ where: [{
335
+ field: "credentialID",
336
+ value: resp.id
337
+ }]
338
+ });
339
+ if (!passkey) throw APIError.from("UNAUTHORIZED", PASSKEY_ERROR_CODES.PASSKEY_NOT_FOUND);
340
+ try {
341
+ const verification = await verifyAuthenticationResponse({
342
+ response: resp,
343
+ expectedChallenge,
344
+ expectedOrigin: origin,
345
+ expectedRPID: getRpID(options, ctx.context.options.baseURL),
346
+ credential: {
347
+ id: passkey.credentialID,
348
+ publicKey: base64.decode(passkey.publicKey),
349
+ counter: passkey.counter,
350
+ transports: passkey.transports?.split(",")
351
+ },
352
+ requireUserVerification: false
353
+ });
354
+ const { verified } = verification;
355
+ if (!verified) throw APIError.from("UNAUTHORIZED", PASSKEY_ERROR_CODES.AUTHENTICATION_FAILED);
356
+ await ctx.context.adapter.update({
357
+ model: "passkey",
358
+ where: [{
359
+ field: "id",
360
+ value: passkey.id
361
+ }],
362
+ update: { counter: verification.authenticationInfo.newCounter }
363
+ });
364
+ const s = await ctx.context.internalAdapter.createSession(passkey.userId);
365
+ if (!s) throw APIError.from("INTERNAL_SERVER_ERROR", PASSKEY_ERROR_CODES.UNABLE_TO_CREATE_SESSION);
366
+ const user = await ctx.context.internalAdapter.findUserById(passkey.userId);
367
+ if (!user) throw new APIError("INTERNAL_SERVER_ERROR", { message: "User not found" });
368
+ await setSessionCookie(ctx, {
369
+ session: s,
370
+ user
371
+ });
372
+ await ctx.context.internalAdapter.deleteVerificationValue(data.id);
373
+ return ctx.json({ session: s }, { status: 200 });
374
+ } catch (e) {
375
+ ctx.context.logger.error("Failed to verify authentication", e);
376
+ throw APIError.from("BAD_REQUEST", PASSKEY_ERROR_CODES.AUTHENTICATION_FAILED);
377
+ }
378
+ });
379
+ /**
380
+ * ### Endpoint
381
+ *
382
+ * GET `/passkey/list-user-passkeys`
383
+ *
384
+ * ### API Methods
385
+ *
386
+ * **server:**
387
+ * `auth.api.listPasskeys`
388
+ *
389
+ * **client:**
390
+ * `authClient.passkey.listUserPasskeys`
391
+ *
392
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/passkey#api-method-passkey-list-user-passkeys)
393
+ */
394
+ const listPasskeys = createAuthEndpoint("/passkey/list-user-passkeys", {
395
+ method: "GET",
396
+ use: [sessionMiddleware],
397
+ metadata: { openapi: {
398
+ description: "List all passkeys for the authenticated user",
399
+ responses: { "200": {
400
+ description: "Passkeys retrieved successfully",
401
+ content: { "application/json": { schema: {
402
+ type: "array",
403
+ items: {
404
+ $ref: "#/components/schemas/Passkey",
405
+ required: [
406
+ "id",
407
+ "userId",
408
+ "publicKey",
409
+ "createdAt",
410
+ "updatedAt"
411
+ ]
412
+ },
413
+ description: "Array of passkey objects associated with the user"
414
+ } } }
415
+ } }
416
+ } }
417
+ }, async (ctx) => {
418
+ const passkeys = await ctx.context.adapter.findMany({
419
+ model: "passkey",
420
+ where: [{
421
+ field: "userId",
422
+ value: ctx.context.session.user.id
423
+ }]
424
+ });
425
+ return ctx.json(passkeys, { status: 200 });
426
+ });
427
+ const deletePasskeyBodySchema = z.object({ id: z.string().meta({ description: "The ID of the passkey to delete. Eg: \"some-passkey-id\"" }) });
428
+ /**
429
+ * ### Endpoint
430
+ *
431
+ * POST `/passkey/delete-passkey`
432
+ *
433
+ * ### API Methods
434
+ *
435
+ * **server:**
436
+ * `auth.api.deletePasskey`
437
+ *
438
+ * **client:**
439
+ * `authClient.passkey.deletePasskey`
440
+ *
441
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/passkey#api-method-passkey-delete-passkey)
442
+ */
443
+ const deletePasskey = createAuthEndpoint("/passkey/delete-passkey", {
444
+ method: "POST",
445
+ body: deletePasskeyBodySchema,
446
+ use: [sessionMiddleware],
447
+ metadata: { openapi: {
448
+ description: "Delete a specific passkey",
449
+ responses: { "200": {
450
+ description: "Passkey deleted successfully",
451
+ content: { "application/json": { schema: {
452
+ type: "object",
453
+ properties: { status: {
454
+ type: "boolean",
455
+ description: "Indicates whether the deletion was successful"
456
+ } },
457
+ required: ["status"]
458
+ } } }
459
+ } }
460
+ } }
461
+ }, async (ctx) => {
462
+ const passkey = await ctx.context.adapter.findOne({
463
+ model: "passkey",
464
+ where: [{
465
+ field: "id",
466
+ value: ctx.body.id
467
+ }]
468
+ });
469
+ if (!passkey) throw APIError.from("NOT_FOUND", PASSKEY_ERROR_CODES.PASSKEY_NOT_FOUND);
470
+ if (passkey.userId !== ctx.context.session.user.id) throw new APIError("UNAUTHORIZED");
471
+ await ctx.context.adapter.delete({
472
+ model: "passkey",
473
+ where: [{
474
+ field: "id",
475
+ value: passkey.id
476
+ }]
477
+ });
478
+ return ctx.json({ status: true });
479
+ });
480
+ const updatePassKeyBodySchema = z.object({
481
+ id: z.string().meta({ description: `The ID of the passkey which will be updated. Eg: \"passkey-id\"` }),
482
+ name: z.string().meta({ description: `The new name which the passkey will be updated to. Eg: \"my-new-passkey-name\"` })
483
+ });
484
+ /**
485
+ * ### Endpoint
486
+ *
487
+ * POST `/passkey/update-passkey`
488
+ *
489
+ * ### API Methods
490
+ *
491
+ * **server:**
492
+ * `auth.api.updatePasskey`
493
+ *
494
+ * **client:**
495
+ * `authClient.passkey.updatePasskey`
496
+ *
497
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/passkey#api-method-passkey-update-passkey)
498
+ */
499
+ const updatePasskey = createAuthEndpoint("/passkey/update-passkey", {
500
+ method: "POST",
501
+ body: updatePassKeyBodySchema,
502
+ use: [sessionMiddleware],
503
+ metadata: { openapi: {
504
+ description: "Update a specific passkey's name",
505
+ responses: { "200": {
506
+ description: "Passkey updated successfully",
507
+ content: { "application/json": { schema: {
508
+ type: "object",
509
+ properties: { passkey: { $ref: "#/components/schemas/Passkey" } },
510
+ required: ["passkey"]
511
+ } } }
512
+ } }
513
+ } }
514
+ }, async (ctx) => {
515
+ const passkey = await ctx.context.adapter.findOne({
516
+ model: "passkey",
517
+ where: [{
518
+ field: "id",
519
+ value: ctx.body.id
520
+ }]
521
+ });
522
+ if (!passkey) throw APIError.from("NOT_FOUND", PASSKEY_ERROR_CODES.PASSKEY_NOT_FOUND);
523
+ if (passkey.userId !== ctx.context.session.user.id) throw APIError.from("UNAUTHORIZED", PASSKEY_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY);
524
+ const updatedPasskey = await ctx.context.adapter.update({
525
+ model: "passkey",
526
+ where: [{
527
+ field: "id",
528
+ value: ctx.body.id
529
+ }],
530
+ update: { name: ctx.body.name }
531
+ });
532
+ if (!updatedPasskey) throw APIError.from("INTERNAL_SERVER_ERROR", PASSKEY_ERROR_CODES.FAILED_TO_UPDATE_PASSKEY);
533
+ return ctx.json({ passkey: updatedPasskey }, { status: 200 });
534
+ });
535
+
536
+ //#endregion
537
+ //#region src/schema.ts
538
+ const schema = { passkey: { fields: {
539
+ name: {
540
+ type: "string",
541
+ required: false
542
+ },
543
+ publicKey: {
544
+ type: "string",
545
+ required: true
546
+ },
547
+ userId: {
548
+ type: "string",
549
+ references: {
550
+ model: "user",
551
+ field: "id"
552
+ },
553
+ required: true,
554
+ index: true
555
+ },
556
+ credentialID: {
557
+ type: "string",
558
+ required: true,
559
+ index: true
560
+ },
561
+ counter: {
562
+ type: "number",
563
+ required: true
564
+ },
565
+ deviceType: {
566
+ type: "string",
567
+ required: true
568
+ },
569
+ backedUp: {
570
+ type: "boolean",
571
+ required: true
572
+ },
573
+ transports: {
574
+ type: "string",
575
+ required: false
576
+ },
577
+ createdAt: {
578
+ type: "date",
579
+ required: false
580
+ },
581
+ aaguid: {
582
+ type: "string",
583
+ required: false
584
+ }
585
+ } } };
586
+
587
+ //#endregion
588
+ //#region src/index.ts
589
+ const passkey = (options) => {
590
+ const opts = {
591
+ origin: null,
592
+ ...options,
593
+ advanced: {
594
+ webAuthnChallengeCookie: "better-auth-passkey",
595
+ ...options?.advanced
596
+ }
597
+ };
598
+ const expirationTime = new Date(Date.now() + 1e3 * 60 * 5);
599
+ const currentTime = /* @__PURE__ */ new Date();
600
+ const maxAgeInSeconds = Math.floor((expirationTime.getTime() - currentTime.getTime()) / 1e3);
601
+ return {
602
+ id: "passkey",
603
+ endpoints: {
604
+ generatePasskeyRegistrationOptions: generatePasskeyRegistrationOptions(opts, {
605
+ maxAgeInSeconds,
606
+ expirationTime
607
+ }),
608
+ generatePasskeyAuthenticationOptions: generatePasskeyAuthenticationOptions(opts, {
609
+ maxAgeInSeconds,
610
+ expirationTime
611
+ }),
612
+ verifyPasskeyRegistration: verifyPasskeyRegistration(opts),
613
+ verifyPasskeyAuthentication: verifyPasskeyAuthentication(opts),
614
+ listPasskeys,
615
+ deletePasskey,
616
+ updatePasskey
617
+ },
618
+ schema: mergeSchema(schema, options?.schema),
619
+ $ERROR_CODES: PASSKEY_ERROR_CODES,
620
+ options
621
+ };
622
+ };
623
+
624
+ //#endregion
625
+ export { passkey };
626
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/utils.ts","../src/routes.ts","../src/schema.ts","../src/index.ts"],"sourcesContent":["import type { PasskeyOptions } from \"./types\";\n\nexport function getRpID(options: PasskeyOptions, baseURL?: string | undefined) {\n\treturn (\n\t\toptions.rpID || (baseURL ? new URL(baseURL).hostname : \"localhost\") // default rpID\n\t);\n}\n","import { createAuthEndpoint } from \"@better-auth/core/api\";\nimport { APIError } from \"@better-auth/core/error\";\nimport { base64 } from \"@better-auth/utils/base64\";\nimport type {\n\tAuthenticationResponseJSON,\n\tAuthenticatorTransportFuture,\n} from \"@simplewebauthn/server\";\nimport {\n\tgenerateAuthenticationOptions,\n\tgenerateRegistrationOptions,\n\tverifyAuthenticationResponse,\n\tverifyRegistrationResponse,\n} from \"@simplewebauthn/server\";\nimport {\n\tfreshSessionMiddleware,\n\tgetSessionFromCtx,\n\tsessionMiddleware,\n} from \"better-auth/api\";\nimport { setSessionCookie } from \"better-auth/cookies\";\nimport { generateRandomString } from \"better-auth/crypto\";\nimport * as z from \"zod\";\nimport { PASSKEY_ERROR_CODES } from \"./error-codes\";\nimport type { Passkey, PasskeyOptions, WebAuthnChallengeValue } from \"./types\";\nimport { getRpID } from \"./utils\";\n\ntype WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };\n\ntype RequiredPassKeyOptions = WithRequired<PasskeyOptions, \"advanced\"> & {\n\tadvanced: Required<PasskeyOptions[\"advanced\"]>;\n};\n\nconst generatePasskeyQuerySchema = z\n\t.object({\n\t\tauthenticatorAttachment: z.enum([\"platform\", \"cross-platform\"]).optional(),\n\t\tname: z.string().optional(),\n\t})\n\t.optional();\n\nexport const generatePasskeyRegistrationOptions = (\n\topts: RequiredPassKeyOptions,\n\t{\n\t\tmaxAgeInSeconds,\n\t\texpirationTime,\n\t}: { maxAgeInSeconds: number; expirationTime: Date },\n) =>\n\tcreateAuthEndpoint(\n\t\t\"/passkey/generate-register-options\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tuse: [freshSessionMiddleware],\n\t\t\tquery: generatePasskeyQuerySchema,\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\toperationId: \"generatePasskeyRegistrationOptions\",\n\t\t\t\t\tdescription: \"Generate registration options for a new passkey\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t200: {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tparameters: {\n\t\t\t\t\t\t\t\tquery: {\n\t\t\t\t\t\t\t\t\tauthenticatorAttachment: {\n\t\t\t\t\t\t\t\t\t\tdescription: `Type of authenticator to use for registration.\n \"platform\" for device-specific authenticators,\n \"cross-platform\" for authenticators that can be used across devices.`,\n\t\t\t\t\t\t\t\t\t\trequired: false,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\tname: {\n\t\t\t\t\t\t\t\t\t\tdescription: `Optional custom name for the passkey.\n This can help identify the passkey when managing multiple credentials.`,\n\t\t\t\t\t\t\t\t\t\trequired: false,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\tchallenge: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\trp: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\tname: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tid: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\tid: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tname: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tdisplayName: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tpubKeyCredParams: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"array\",\n\t\t\t\t\t\t\t\t\t\t\t\titems: {\n\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\talg: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"number\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\ttimeout: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"number\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\texcludeCredentials: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"array\",\n\t\t\t\t\t\t\t\t\t\t\t\titems: {\n\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tid: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttransports: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"array\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\titems: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tauthenticatorSelection: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\tauthenticatorAttachment: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\trequireResidentKey: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"boolean\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tuserVerification: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tattestation: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\n\t\t\t\t\t\t\t\t\t\t\textensions: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst { session } = ctx.context;\n\t\t\tconst userPasskeys = await ctx.context.adapter.findMany<Passkey>({\n\t\t\t\tmodel: \"passkey\",\n\t\t\t\twhere: [\n\t\t\t\t\t{\n\t\t\t\t\t\tfield: \"userId\",\n\t\t\t\t\t\tvalue: session.user.id,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t});\n\t\t\tconst userID = new TextEncoder().encode(\n\t\t\t\tgenerateRandomString(32, \"a-z\", \"0-9\"),\n\t\t\t);\n\t\t\tconst options = await generateRegistrationOptions({\n\t\t\t\trpName: opts.rpName || ctx.context.appName,\n\t\t\t\trpID: getRpID(opts, ctx.context.options.baseURL),\n\t\t\t\tuserID,\n\t\t\t\tuserName: ctx.query?.name || session.user.email || session.user.id,\n\t\t\t\tuserDisplayName: session.user.email || session.user.id,\n\t\t\t\tattestationType: \"none\",\n\t\t\t\texcludeCredentials: userPasskeys.map((passkey) => ({\n\t\t\t\t\tid: passkey.credentialID,\n\t\t\t\t\ttransports: passkey.transports?.split(\n\t\t\t\t\t\t\",\",\n\t\t\t\t\t) as AuthenticatorTransportFuture[],\n\t\t\t\t})),\n\t\t\t\tauthenticatorSelection: {\n\t\t\t\t\tresidentKey: \"preferred\",\n\t\t\t\t\tuserVerification: \"preferred\",\n\t\t\t\t\t...(opts.authenticatorSelection || {}),\n\t\t\t\t\t...(ctx.query?.authenticatorAttachment\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tauthenticatorAttachment: ctx.query.authenticatorAttachment,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {}),\n\t\t\t\t},\n\t\t\t});\n\t\t\tconst verificationToken = generateRandomString(32);\n\t\t\tconst webAuthnCookie = ctx.context.createAuthCookie(\n\t\t\t\topts.advanced.webAuthnChallengeCookie,\n\t\t\t);\n\t\t\tawait ctx.setSignedCookie(\n\t\t\t\twebAuthnCookie.name,\n\t\t\t\tverificationToken,\n\t\t\t\tctx.context.secret,\n\t\t\t\t{\n\t\t\t\t\t...webAuthnCookie.attributes,\n\t\t\t\t\tmaxAge: maxAgeInSeconds,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait ctx.context.internalAdapter.createVerificationValue({\n\t\t\t\tidentifier: verificationToken,\n\t\t\t\tvalue: JSON.stringify({\n\t\t\t\t\texpectedChallenge: options.challenge,\n\t\t\t\t\tuserData: {\n\t\t\t\t\t\tid: session.user.id,\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t\texpiresAt: expirationTime,\n\t\t\t});\n\t\t\treturn ctx.json(options, {\n\t\t\t\tstatus: 200,\n\t\t\t});\n\t\t},\n\t);\n\nexport const generatePasskeyAuthenticationOptions = (\n\topts: RequiredPassKeyOptions,\n\t{\n\t\tmaxAgeInSeconds,\n\t\texpirationTime,\n\t}: { maxAgeInSeconds: number; expirationTime: Date },\n) =>\n\tcreateAuthEndpoint(\n\t\t\"/passkey/generate-authenticate-options\",\n\t\t{\n\t\t\tmethod: \"GET\",\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\toperationId: \"passkeyGenerateAuthenticateOptions\",\n\t\t\t\t\tdescription: \"Generate authentication options for a passkey\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t200: {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\tchallenge: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\trp: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\tname: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tid: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\tid: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tname: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tdisplayName: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\ttimeout: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"number\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tallowCredentials: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"array\",\n\t\t\t\t\t\t\t\t\t\t\t\titems: {\n\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tid: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttransports: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"array\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\titems: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tuserVerification: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tauthenticatorSelection: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\t\t\tauthenticatorAttachment: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\trequireResidentKey: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"boolean\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\tuserVerification: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\textensions: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst session = await getSessionFromCtx(ctx);\n\t\t\tlet userPasskeys: Passkey[] = [];\n\t\t\tif (session) {\n\t\t\t\tuserPasskeys = await ctx.context.adapter.findMany<Passkey>({\n\t\t\t\t\tmodel: \"passkey\",\n\t\t\t\t\twhere: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfield: \"userId\",\n\t\t\t\t\t\t\tvalue: session.user.id,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst options = await generateAuthenticationOptions({\n\t\t\t\trpID: getRpID(opts, ctx.context.options.baseURL),\n\t\t\t\tuserVerification: \"preferred\",\n\t\t\t\t...(userPasskeys.length\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tallowCredentials: userPasskeys.map((passkey) => ({\n\t\t\t\t\t\t\t\tid: passkey.credentialID,\n\t\t\t\t\t\t\t\ttransports: passkey.transports?.split(\n\t\t\t\t\t\t\t\t\t\",\",\n\t\t\t\t\t\t\t\t) as AuthenticatorTransportFuture[],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t}\n\t\t\t\t\t: {}),\n\t\t\t});\n\t\t\tconst data = {\n\t\t\t\texpectedChallenge: options.challenge,\n\t\t\t\tuserData: {\n\t\t\t\t\tid: session?.user.id || \"\",\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst verificationToken = generateRandomString(32);\n\t\t\tconst webAuthnCookie = ctx.context.createAuthCookie(\n\t\t\t\topts.advanced.webAuthnChallengeCookie,\n\t\t\t);\n\t\t\tawait ctx.setSignedCookie(\n\t\t\t\twebAuthnCookie.name,\n\t\t\t\tverificationToken,\n\t\t\t\tctx.context.secret,\n\t\t\t\t{\n\t\t\t\t\t...webAuthnCookie.attributes,\n\t\t\t\t\tmaxAge: maxAgeInSeconds,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait ctx.context.internalAdapter.createVerificationValue({\n\t\t\t\tidentifier: verificationToken,\n\t\t\t\tvalue: JSON.stringify(data),\n\t\t\t\texpiresAt: expirationTime,\n\t\t\t});\n\t\t\treturn ctx.json(options, {\n\t\t\t\tstatus: 200,\n\t\t\t});\n\t\t},\n\t);\n\nconst verifyPasskeyRegistrationBodySchema = z.object({\n\tresponse: z.any(),\n\tname: z\n\t\t.string()\n\t\t.meta({\n\t\t\tdescription: \"Name of the passkey\",\n\t\t})\n\t\t.optional(),\n});\n\nexport const verifyPasskeyRegistration = (options: RequiredPassKeyOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/passkey/verify-registration\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tbody: verifyPasskeyRegistrationBodySchema,\n\t\t\tuse: [freshSessionMiddleware],\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\toperationId: \"passkeyVerifyRegistration\",\n\t\t\t\t\tdescription: \"Verify registration of a new passkey\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t200: {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Passkey\",\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t400: {\n\t\t\t\t\t\t\tdescription: \"Bad request\",\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst origin = options?.origin || ctx.headers?.get(\"origin\") || \"\";\n\t\t\tif (!origin) {\n\t\t\t\treturn ctx.json(null, {\n\t\t\t\t\tstatus: 400,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst resp = ctx.body.response;\n\t\t\tconst webAuthnCookie = ctx.context.createAuthCookie(\n\t\t\t\toptions.advanced.webAuthnChallengeCookie,\n\t\t\t);\n\t\t\tconst verificationToken = await ctx.getSignedCookie(\n\t\t\t\twebAuthnCookie.name,\n\t\t\t\tctx.context.secret,\n\t\t\t);\n\t\t\tif (!verificationToken) {\n\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\"BAD_REQUEST\",\n\t\t\t\t\tPASSKEY_ERROR_CODES.CHALLENGE_NOT_FOUND,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst data =\n\t\t\t\tawait ctx.context.internalAdapter.findVerificationValue(\n\t\t\t\t\tverificationToken,\n\t\t\t\t);\n\t\t\tif (!data) {\n\t\t\t\treturn ctx.json(null, {\n\t\t\t\t\tstatus: 400,\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst { expectedChallenge, userData } = JSON.parse(\n\t\t\t\tdata.value,\n\t\t\t) as WebAuthnChallengeValue;\n\n\t\t\tif (userData.id !== ctx.context.session.user.id) {\n\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\"UNAUTHORIZED\",\n\t\t\t\t\tPASSKEY_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst verification = await verifyRegistrationResponse({\n\t\t\t\t\tresponse: resp,\n\t\t\t\t\texpectedChallenge,\n\t\t\t\t\texpectedOrigin: origin,\n\t\t\t\t\texpectedRPID: getRpID(options, ctx.context.options.baseURL),\n\t\t\t\t\trequireUserVerification: false,\n\t\t\t\t});\n\t\t\t\tconst { verified, registrationInfo } = verification;\n\t\t\t\tif (!verified || !registrationInfo) {\n\t\t\t\t\treturn ctx.json(null, {\n\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tconst { aaguid, credentialDeviceType, credentialBackedUp, credential } =\n\t\t\t\t\tregistrationInfo;\n\t\t\t\tconst pubKey = base64.encode(credential.publicKey);\n\t\t\t\tconst newPasskey: Omit<Passkey, \"id\"> = {\n\t\t\t\t\tname: ctx.body.name,\n\t\t\t\t\tuserId: userData.id,\n\t\t\t\t\tcredentialID: credential.id,\n\t\t\t\t\tpublicKey: pubKey,\n\t\t\t\t\tcounter: credential.counter,\n\t\t\t\t\tdeviceType: credentialDeviceType,\n\t\t\t\t\ttransports: resp.response.transports.join(\",\"),\n\t\t\t\t\tbackedUp: credentialBackedUp,\n\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\taaguid: aaguid,\n\t\t\t\t};\n\t\t\t\tconst newPasskeyRes = await ctx.context.adapter.create<\n\t\t\t\t\tOmit<Passkey, \"id\">,\n\t\t\t\t\tPasskey\n\t\t\t\t>({\n\t\t\t\t\tmodel: \"passkey\",\n\t\t\t\t\tdata: newPasskey,\n\t\t\t\t});\n\t\t\t\tawait ctx.context.internalAdapter.deleteVerificationValue(data.id);\n\t\t\t\treturn ctx.json(newPasskeyRes, {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t});\n\t\t\t} catch (e) {\n\t\t\t\tctx.context.logger.error(\"Failed to verify registration\", e);\n\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\"INTERNAL_SERVER_ERROR\",\n\t\t\t\t\tPASSKEY_ERROR_CODES.FAILED_TO_VERIFY_REGISTRATION,\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t);\n\nconst verifyPasskeyAuthenticationBodySchema = z.object({\n\tresponse: z.record(z.any(), z.any()),\n});\n\nexport const verifyPasskeyAuthentication = (options: RequiredPassKeyOptions) =>\n\tcreateAuthEndpoint(\n\t\t\"/passkey/verify-authentication\",\n\t\t{\n\t\t\tmethod: \"POST\",\n\t\t\tbody: verifyPasskeyAuthenticationBodySchema,\n\t\t\tmetadata: {\n\t\t\t\topenapi: {\n\t\t\t\t\toperationId: \"passkeyVerifyAuthentication\",\n\t\t\t\t\tdescription: \"Verify authentication of a passkey\",\n\t\t\t\t\tresponses: {\n\t\t\t\t\t\t200: {\n\t\t\t\t\t\t\tdescription: \"Success\",\n\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\t\tsession: {\n\t\t\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Session\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/User\",\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t$Infer: {\n\t\t\t\t\tbody: {} as {\n\t\t\t\t\t\tresponse: AuthenticationResponseJSON;\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tasync (ctx) => {\n\t\t\tconst origin = options?.origin || ctx.headers?.get(\"origin\") || \"\";\n\t\t\tif (!origin) {\n\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\tmessage: \"origin missing\",\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst resp = ctx.body.response;\n\t\t\tconst webAuthnCookie = ctx.context.createAuthCookie(\n\t\t\t\toptions.advanced.webAuthnChallengeCookie,\n\t\t\t);\n\t\t\tconst verificationToken = await ctx.getSignedCookie(\n\t\t\t\twebAuthnCookie.name,\n\t\t\t\tctx.context.secret,\n\t\t\t);\n\t\t\tif (!verificationToken) {\n\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\"BAD_REQUEST\",\n\t\t\t\t\tPASSKEY_ERROR_CODES.CHALLENGE_NOT_FOUND,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst data =\n\t\t\t\tawait ctx.context.internalAdapter.findVerificationValue(\n\t\t\t\t\tverificationToken,\n\t\t\t\t);\n\t\t\tif (!data) {\n\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\"BAD_REQUEST\",\n\t\t\t\t\tPASSKEY_ERROR_CODES.CHALLENGE_NOT_FOUND,\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst { expectedChallenge } = JSON.parse(\n\t\t\t\tdata.value,\n\t\t\t) as WebAuthnChallengeValue;\n\t\t\tconst passkey = await ctx.context.adapter.findOne<Passkey>({\n\t\t\t\tmodel: \"passkey\",\n\t\t\t\twhere: [\n\t\t\t\t\t{\n\t\t\t\t\t\tfield: \"credentialID\",\n\t\t\t\t\t\tvalue: resp.id,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t});\n\t\t\tif (!passkey) {\n\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\"UNAUTHORIZED\",\n\t\t\t\t\tPASSKEY_ERROR_CODES.PASSKEY_NOT_FOUND,\n\t\t\t\t);\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst verification = await verifyAuthenticationResponse({\n\t\t\t\t\tresponse: resp as AuthenticationResponseJSON,\n\t\t\t\t\texpectedChallenge,\n\t\t\t\t\texpectedOrigin: origin,\n\t\t\t\t\texpectedRPID: getRpID(options, ctx.context.options.baseURL),\n\t\t\t\t\tcredential: {\n\t\t\t\t\t\tid: passkey.credentialID,\n\t\t\t\t\t\tpublicKey: base64.decode(passkey.publicKey),\n\t\t\t\t\t\tcounter: passkey.counter,\n\t\t\t\t\t\ttransports: passkey.transports?.split(\n\t\t\t\t\t\t\t\",\",\n\t\t\t\t\t\t) as AuthenticatorTransportFuture[],\n\t\t\t\t\t},\n\t\t\t\t\trequireUserVerification: false,\n\t\t\t\t});\n\t\t\t\tconst { verified } = verification;\n\t\t\t\tif (!verified)\n\t\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\t\"UNAUTHORIZED\",\n\t\t\t\t\t\tPASSKEY_ERROR_CODES.AUTHENTICATION_FAILED,\n\t\t\t\t\t);\n\n\t\t\t\tawait ctx.context.adapter.update<Passkey>({\n\t\t\t\t\tmodel: \"passkey\",\n\t\t\t\t\twhere: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfield: \"id\",\n\t\t\t\t\t\t\tvalue: passkey.id,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tupdate: {\n\t\t\t\t\t\tcounter: verification.authenticationInfo.newCounter,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t\tconst s = await ctx.context.internalAdapter.createSession(\n\t\t\t\t\tpasskey.userId,\n\t\t\t\t);\n\t\t\t\tif (!s) {\n\t\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\t\"INTERNAL_SERVER_ERROR\",\n\t\t\t\t\t\tPASSKEY_ERROR_CODES.UNABLE_TO_CREATE_SESSION,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tconst user = await ctx.context.internalAdapter.findUserById(\n\t\t\t\t\tpasskey.userId,\n\t\t\t\t);\n\t\t\t\tif (!user) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tawait setSessionCookie(ctx, {\n\t\t\t\t\tsession: s,\n\t\t\t\t\tuser,\n\t\t\t\t});\n\t\t\t\tawait ctx.context.internalAdapter.deleteVerificationValue(data.id);\n\n\t\t\t\treturn ctx.json(\n\t\t\t\t\t{\n\t\t\t\t\t\tsession: s,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tstatus: 200,\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t} catch (e) {\n\t\t\t\tctx.context.logger.error(\"Failed to verify authentication\", e);\n\t\t\t\tthrow APIError.from(\n\t\t\t\t\t\"BAD_REQUEST\",\n\t\t\t\t\tPASSKEY_ERROR_CODES.AUTHENTICATION_FAILED,\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t);\n\n/**\n * ### Endpoint\n *\n * GET `/passkey/list-user-passkeys`\n *\n * ### API Methods\n *\n * **server:**\n * `auth.api.listPasskeys`\n *\n * **client:**\n * `authClient.passkey.listUserPasskeys`\n *\n * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/passkey#api-method-passkey-list-user-passkeys)\n */\nexport const listPasskeys = createAuthEndpoint(\n\t\"/passkey/list-user-passkeys\",\n\t{\n\t\tmethod: \"GET\",\n\t\tuse: [sessionMiddleware],\n\t\tmetadata: {\n\t\t\topenapi: {\n\t\t\t\tdescription: \"List all passkeys for the authenticated user\",\n\t\t\t\tresponses: {\n\t\t\t\t\t\"200\": {\n\t\t\t\t\t\tdescription: \"Passkeys retrieved successfully\",\n\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\ttype: \"array\",\n\t\t\t\t\t\t\t\t\titems: {\n\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Passkey\",\n\t\t\t\t\t\t\t\t\t\trequired: [\n\t\t\t\t\t\t\t\t\t\t\t\"id\",\n\t\t\t\t\t\t\t\t\t\t\t\"userId\",\n\t\t\t\t\t\t\t\t\t\t\t\"publicKey\",\n\t\t\t\t\t\t\t\t\t\t\t\"createdAt\",\n\t\t\t\t\t\t\t\t\t\t\t\"updatedAt\",\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\tdescription:\n\t\t\t\t\t\t\t\t\t\t\"Array of passkey objects associated with the user\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t},\n\tasync (ctx) => {\n\t\tconst passkeys = await ctx.context.adapter.findMany<Passkey>({\n\t\t\tmodel: \"passkey\",\n\t\t\twhere: [{ field: \"userId\", value: ctx.context.session.user.id }],\n\t\t});\n\t\treturn ctx.json(passkeys, {\n\t\t\tstatus: 200,\n\t\t});\n\t},\n);\n\nconst deletePasskeyBodySchema = z.object({\n\tid: z.string().meta({\n\t\tdescription: 'The ID of the passkey to delete. Eg: \"some-passkey-id\"',\n\t}),\n});\n\n/**\n * ### Endpoint\n *\n * POST `/passkey/delete-passkey`\n *\n * ### API Methods\n *\n * **server:**\n * `auth.api.deletePasskey`\n *\n * **client:**\n * `authClient.passkey.deletePasskey`\n *\n * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/passkey#api-method-passkey-delete-passkey)\n */\nexport const deletePasskey = createAuthEndpoint(\n\t\"/passkey/delete-passkey\",\n\t{\n\t\tmethod: \"POST\",\n\t\tbody: deletePasskeyBodySchema,\n\t\tuse: [sessionMiddleware],\n\t\tmetadata: {\n\t\t\topenapi: {\n\t\t\t\tdescription: \"Delete a specific passkey\",\n\t\t\t\tresponses: {\n\t\t\t\t\t\"200\": {\n\t\t\t\t\t\tdescription: \"Passkey deleted successfully\",\n\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\tstatus: {\n\t\t\t\t\t\t\t\t\t\t\ttype: \"boolean\",\n\t\t\t\t\t\t\t\t\t\t\tdescription:\n\t\t\t\t\t\t\t\t\t\t\t\t\"Indicates whether the deletion was successful\",\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\trequired: [\"status\"],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t},\n\tasync (ctx) => {\n\t\tconst passkey = await ctx.context.adapter.findOne<Passkey>({\n\t\t\tmodel: \"passkey\",\n\t\t\twhere: [\n\t\t\t\t{\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t\tvalue: ctx.body.id,\n\t\t\t\t},\n\t\t\t],\n\t\t});\n\t\tif (!passkey) {\n\t\t\tthrow APIError.from(\"NOT_FOUND\", PASSKEY_ERROR_CODES.PASSKEY_NOT_FOUND);\n\t\t}\n\t\tif (passkey.userId !== ctx.context.session.user.id) {\n\t\t\tthrow new APIError(\"UNAUTHORIZED\");\n\t\t}\n\t\tawait ctx.context.adapter.delete({\n\t\t\tmodel: \"passkey\",\n\t\t\twhere: [{ field: \"id\", value: passkey.id }],\n\t\t});\n\t\treturn ctx.json({\n\t\t\tstatus: true,\n\t\t});\n\t},\n);\n\nconst updatePassKeyBodySchema = z.object({\n\tid: z.string().meta({\n\t\tdescription: `The ID of the passkey which will be updated. Eg: \\\"passkey-id\\\"`,\n\t}),\n\tname: z.string().meta({\n\t\tdescription: `The new name which the passkey will be updated to. Eg: \\\"my-new-passkey-name\\\"`,\n\t}),\n});\n\n/**\n * ### Endpoint\n *\n * POST `/passkey/update-passkey`\n *\n * ### API Methods\n *\n * **server:**\n * `auth.api.updatePasskey`\n *\n * **client:**\n * `authClient.passkey.updatePasskey`\n *\n * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/passkey#api-method-passkey-update-passkey)\n */\nexport const updatePasskey = createAuthEndpoint(\n\t\"/passkey/update-passkey\",\n\t{\n\t\tmethod: \"POST\",\n\t\tbody: updatePassKeyBodySchema,\n\t\tuse: [sessionMiddleware],\n\t\tmetadata: {\n\t\t\topenapi: {\n\t\t\t\tdescription: \"Update a specific passkey's name\",\n\t\t\t\tresponses: {\n\t\t\t\t\t\"200\": {\n\t\t\t\t\t\tdescription: \"Passkey updated successfully\",\n\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\tpasskey: {\n\t\t\t\t\t\t\t\t\t\t\t$ref: \"#/components/schemas/Passkey\",\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\trequired: [\"passkey\"],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t},\n\tasync (ctx) => {\n\t\tconst passkey = await ctx.context.adapter.findOne<Passkey>({\n\t\t\tmodel: \"passkey\",\n\t\t\twhere: [\n\t\t\t\t{\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t\tvalue: ctx.body.id,\n\t\t\t\t},\n\t\t\t],\n\t\t});\n\n\t\tif (!passkey) {\n\t\t\tthrow APIError.from(\"NOT_FOUND\", PASSKEY_ERROR_CODES.PASSKEY_NOT_FOUND);\n\t\t}\n\n\t\tif (passkey.userId !== ctx.context.session.user.id) {\n\t\t\tthrow APIError.from(\n\t\t\t\t\"UNAUTHORIZED\",\n\t\t\t\tPASSKEY_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY,\n\t\t\t);\n\t\t}\n\n\t\tconst updatedPasskey = await ctx.context.adapter.update<Passkey>({\n\t\t\tmodel: \"passkey\",\n\t\t\twhere: [\n\t\t\t\t{\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t\tvalue: ctx.body.id,\n\t\t\t\t},\n\t\t\t],\n\t\t\tupdate: {\n\t\t\t\tname: ctx.body.name,\n\t\t\t},\n\t\t});\n\n\t\tif (!updatedPasskey) {\n\t\t\tthrow APIError.from(\n\t\t\t\t\"INTERNAL_SERVER_ERROR\",\n\t\t\t\tPASSKEY_ERROR_CODES.FAILED_TO_UPDATE_PASSKEY,\n\t\t\t);\n\t\t}\n\t\treturn ctx.json(\n\t\t\t{\n\t\t\t\tpasskey: updatedPasskey,\n\t\t\t},\n\t\t\t{\n\t\t\t\tstatus: 200,\n\t\t\t},\n\t\t);\n\t},\n);\n","import type { BetterAuthPluginDBSchema } from \"@better-auth/core/db\";\n\nexport const schema = {\n\tpasskey: {\n\t\tfields: {\n\t\t\tname: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: false,\n\t\t\t},\n\t\t\tpublicKey: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tuserId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\treferences: {\n\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t},\n\t\t\t\trequired: true,\n\t\t\t\tindex: true,\n\t\t\t},\n\t\t\tcredentialID: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t\tindex: true,\n\t\t\t},\n\t\t\tcounter: {\n\t\t\t\ttype: \"number\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tdeviceType: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tbackedUp: {\n\t\t\t\ttype: \"boolean\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\ttransports: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: false,\n\t\t\t},\n\t\t\tcreatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t\trequired: false,\n\t\t\t},\n\t\t\taaguid: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: false,\n\t\t\t},\n\t\t},\n\t},\n} satisfies BetterAuthPluginDBSchema;\n","import type { BetterAuthPlugin } from \"@better-auth/core\";\nimport { mergeSchema } from \"better-auth/db\";\nimport { PASSKEY_ERROR_CODES } from \"./error-codes\";\nimport {\n\tdeletePasskey,\n\tgeneratePasskeyAuthenticationOptions,\n\tgeneratePasskeyRegistrationOptions,\n\tlistPasskeys,\n\tupdatePasskey,\n\tverifyPasskeyAuthentication,\n\tverifyPasskeyRegistration,\n} from \"./routes\";\nimport { schema } from \"./schema\";\nimport type { Passkey, PasskeyOptions } from \"./types\";\n\ndeclare module \"@better-auth/core\" {\n\tinterface BetterAuthPluginRegistry<AuthOptions, Options> {\n\t\tpasskey: {\n\t\t\tcreator: typeof passkey;\n\t\t};\n\t}\n}\n\nexport const passkey = (options?: PasskeyOptions | undefined) => {\n\tconst opts = {\n\t\torigin: null,\n\t\t...options,\n\t\tadvanced: {\n\t\t\twebAuthnChallengeCookie: \"better-auth-passkey\",\n\t\t\t...options?.advanced,\n\t\t},\n\t};\n\tconst expirationTime = new Date(Date.now() + 1000 * 60 * 5);\n\tconst currentTime = new Date();\n\tconst maxAgeInSeconds = Math.floor(\n\t\t(expirationTime.getTime() - currentTime.getTime()) / 1000,\n\t);\n\n\treturn {\n\t\tid: \"passkey\",\n\t\tendpoints: {\n\t\t\tgeneratePasskeyRegistrationOptions: generatePasskeyRegistrationOptions(\n\t\t\t\topts,\n\t\t\t\t{ maxAgeInSeconds, expirationTime },\n\t\t\t),\n\t\t\tgeneratePasskeyAuthenticationOptions:\n\t\t\t\tgeneratePasskeyAuthenticationOptions(opts, {\n\t\t\t\t\tmaxAgeInSeconds,\n\t\t\t\t\texpirationTime,\n\t\t\t\t}),\n\t\t\tverifyPasskeyRegistration: verifyPasskeyRegistration(opts),\n\t\t\tverifyPasskeyAuthentication: verifyPasskeyAuthentication(opts),\n\t\t\tlistPasskeys,\n\t\t\tdeletePasskey,\n\t\t\tupdatePasskey,\n\t\t},\n\t\tschema: mergeSchema(schema, options?.schema),\n\t\t$ERROR_CODES: PASSKEY_ERROR_CODES,\n\t\toptions,\n\t} satisfies BetterAuthPlugin;\n};\n\nexport type { Passkey, PasskeyOptions };\n"],"mappings":";;;;;;;;;;;;AAEA,SAAgB,QAAQ,SAAyB,SAA8B;AAC9E,QACC,QAAQ,SAAS,UAAU,IAAI,IAAI,QAAQ,CAAC,WAAW;;;;;AC2BzD,MAAM,6BAA6B,EACjC,OAAO;CACP,yBAAyB,EAAE,KAAK,CAAC,YAAY,iBAAiB,CAAC,CAAC,UAAU;CAC1E,MAAM,EAAE,QAAQ,CAAC,UAAU;CAC3B,CAAC,CACD,UAAU;AAEZ,MAAa,sCACZ,MACA,EACC,iBACA,qBAGD,mBACC,sCACA;CACC,QAAQ;CACR,KAAK,CAAC,uBAAuB;CAC7B,OAAO;CACP,UAAU,EACT,SAAS;EACR,aAAa;EACb,aAAa;EACb,WAAW,EACV,KAAK;GACJ,aAAa;GACb,YAAY,EACX,OAAO;IACN,yBAAyB;KACxB,aAAa;;;KAGb,UAAU;KACV;IACD,MAAM;KACL,aAAa;;KAEb,UAAU;KACV;IACD,EACD;GACD,SAAS,EACR,oBAAoB,EACnB,QAAQ;IACP,MAAM;IACN,YAAY;KACX,WAAW,EACV,MAAM,UACN;KACD,IAAI;MACH,MAAM;MACN,YAAY;OACX,MAAM,EACL,MAAM,UACN;OACD,IAAI,EACH,MAAM,UACN;OACD;MACD;KACD,MAAM;MACL,MAAM;MACN,YAAY;OACX,IAAI,EACH,MAAM,UACN;OACD,MAAM,EACL,MAAM,UACN;OACD,aAAa,EACZ,MAAM,UACN;OACD;MACD;KACD,kBAAkB;MACjB,MAAM;MACN,OAAO;OACN,MAAM;OACN,YAAY;QACX,MAAM,EACL,MAAM,UACN;QACD,KAAK,EACJ,MAAM,UACN;QACD;OACD;MACD;KACD,SAAS,EACR,MAAM,UACN;KACD,oBAAoB;MACnB,MAAM;MACN,OAAO;OACN,MAAM;OACN,YAAY;QACX,IAAI,EACH,MAAM,UACN;QACD,MAAM,EACL,MAAM,UACN;QACD,YAAY;SACX,MAAM;SACN,OAAO,EACN,M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M,IAAI,QAAQ,gBAAgB,wBAAwB;EACzD,YAAY;EACZ,OAAO,KAAK,UAAU;GACrB,mBAAmB,QAAQ;GAC3B,UAAU,EACT,IAAI,QAAQ,KAAK,IACjB;GACD,CAAC;EACF,WAAW;EACX,CAAC;AACF,QAAO,IAAI,KAAK,SAAS,EACxB,QAAQ,KACR,CAAC;EAEH;AAEF,MAAa,wCACZ,MACA,EACC,iBACA,qBAGD,mBACC,0CACA;CACC,QAAQ;CACR,UAAU,EACT,SAAS;EACR,aAAa;EACb,aAAa;EACb,WAAW,EACV,KAAK;GACJ,aAAa;GACb,SAAS,EACR,oBAAoB,EACnB,QAAQ;IACP,MAAM;IACN,YAAY;KACX,WAAW,EACV,MAAM,UACN;KACD,IAAI;MACH,MAAM;MACN,YAAY;OACX,MAAM,EACL,MAAM,UACN;OACD,IAAI,EACH,MAAM,UACN;OACD;MACD;KACD,MAAM;MACL,MAAM;MACN,YAAY;OACX,IAAI,EACH,MAAM,UACN;OACD,MAAM,EACL,MAAM,UACN;OACD,aAAa,EACZ,MAAM,UACN;OACD;MACD;KACD,SAAS,EACR,MAAM,UACN;KACD,kBAAkB;MACjB,MAAM;MACN,OAAO;OACN,MAAM;OACN,YAAY;QACX,IAAI,EACH,MAAM,UACN;QACD,MAAM,EACL,MAAM,UACN;QACD,YAAY;SACX,MAAM;SACN,OAAO,EACN,MAAM,UACN;SACD;QACD;OACD;MACD;KACD,kBAAkB,EACjB,MAAM,UACN;KACD,wBAAwB;MACvB,MAAM;MACN,YAAY;OACX,yBAAyB,EACxB,MAAM,UACN;OACD,oBAAoB,EACnB,MAAM,WACN;OACD,kBAAkB,EACjB,MAAM,UACN;OACD;MACD;KACD,YAAY,EACX,MAAM,UACN;KACD;IACD,EACD,EACD;GACD,EACD;EACD,EACD;CACD,EACD,OAAO,QAAQ;CACd,MAAM,UAAU,MAAM,kBAAkB,IAAI;CAC5C,IAAI,eAA0B,EAAE;AAChC,KAAI,QACH,gBAAe,MAAM,IAAI,QA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1,76 @@
1
+ import { CredentialDeviceType } from "@simplewebauthn/server";
2
+ import { InferOptionSchema } from "better-auth/types";
3
+
4
+ //#region src/schema.d.ts
5
+ declare const schema: BetterAuthPluginDBSchema;
6
+ //#endregion
7
+ //#region src/types.d.ts
8
+ /**
9
+ * @internal
10
+ */
11
+ interface WebAuthnChallengeValue {
12
+ expectedChallenge: string;
13
+ userData: {
14
+ id: string;
15
+ };
16
+ }
17
+ interface PasskeyOptions {
18
+ /**
19
+ * A unique identifier for your website. 'localhost' is okay for
20
+ * local dev
21
+ *
22
+ * @default "localhost"
23
+ */
24
+ rpID?: string | undefined;
25
+ /**
26
+ * Human-readable title for your website
27
+ *
28
+ * @default "Better Auth"
29
+ */
30
+ rpName?: string | undefined;
31
+ /**
32
+ * The URL at which registrations and authentications should occur.
33
+ * `http://localhost` and `http://localhost:PORT` are also valid.
34
+ * Do NOT include any trailing /
35
+ *
36
+ * if this isn't provided. The client itself will
37
+ * pass this value.
38
+ */
39
+ origin?: (string | string[] | null) | undefined;
40
+ /**
41
+ * Allow customization of the authenticatorSelection options
42
+ * during passkey registration.
43
+ */
44
+ authenticatorSelection?: AuthenticatorSelectionCriteria | undefined;
45
+ /**
46
+ * Advanced options
47
+ */
48
+ advanced?: {
49
+ /**
50
+ * Cookie name for storing WebAuthn challenge ID during authentication flow
51
+ *
52
+ * @default "better-auth-passkey"
53
+ */
54
+ webAuthnChallengeCookie?: string;
55
+ } | undefined;
56
+ /**
57
+ * Schema for the passkey model
58
+ */
59
+ schema?: InferOptionSchema<typeof schema> | undefined;
60
+ }
61
+ type Passkey = {
62
+ id: string;
63
+ name?: string | undefined;
64
+ publicKey: string;
65
+ userId: string;
66
+ credentialID: string;
67
+ counter: number;
68
+ deviceType: CredentialDeviceType;
69
+ backedUp: boolean;
70
+ transports?: string | undefined;
71
+ createdAt: Date;
72
+ aaguid?: string | undefined;
73
+ };
74
+ //#endregion
75
+ export { PasskeyOptions as n, WebAuthnChallengeValue as r, Passkey as t };
76
+ //# sourceMappingURL=types-DYj_eloX.d.mts.map
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@hammadj/better-auth-passkey",
3
+ "version": "1.5.0-beta.9",
4
+ "type": "module",
5
+ "description": "Passkey plugin for Better Auth",
6
+ "main": "dist/index.mjs",
7
+ "module": "dist/index.mjs",
8
+ "types": "dist/index.d.mts",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "dev-source": "./src/index.ts",
15
+ "types": "./dist/index.d.mts",
16
+ "default": "./dist/index.mjs"
17
+ },
18
+ "./client": {
19
+ "dev-source": "./src/client.ts",
20
+ "types": "./dist/client.d.mts",
21
+ "default": "./dist/client.mjs"
22
+ }
23
+ },
24
+ "typesVersions": {
25
+ "*": {
26
+ "*": [
27
+ "./dist/index.d.mts"
28
+ ],
29
+ "client": [
30
+ "./dist/client.d.mts"
31
+ ]
32
+ }
33
+ },
34
+ "devDependencies": {
35
+ "tsdown": "^0.20.1",
36
+ "@hammadj/better-auth": "1.5.0-beta.9",
37
+ "@hammadj/better-auth-core": "1.5.0-beta.9"
38
+ },
39
+ "dependencies": {
40
+ "@simplewebauthn/browser": "^13.2.2",
41
+ "@simplewebauthn/server": "^13.2.2",
42
+ "zod": "^4.3.6"
43
+ },
44
+ "peerDependencies": {
45
+ "@better-auth/utils": "0.3.1",
46
+ "@better-fetch/fetch": "1.1.21",
47
+ "better-call": "1.2.0",
48
+ "nanostores": "^1.0.1",
49
+ "@hammadj/better-auth-core": "1.5.0-beta.9",
50
+ "@hammadj/better-auth": "1.5.0-beta.9"
51
+ },
52
+ "files": [
53
+ "dist"
54
+ ],
55
+ "repository": {
56
+ "type": "git",
57
+ "url": "git+https://github.com/META-DREAMER/better-auth.git",
58
+ "directory": "packages/passkey"
59
+ },
60
+ "homepage": "https://www.better-auth.com/docs/plugins/passkey",
61
+ "keywords": [
62
+ "auth",
63
+ "passkey",
64
+ "typescript",
65
+ "better-auth"
66
+ ],
67
+ "license": "MIT",
68
+ "scripts": {
69
+ "test": "vitest",
70
+ "coverage": "vitest run --coverage --coverage.provider=istanbul",
71
+ "lint:package": "publint run --strict",
72
+ "lint:types": "attw --profile esm-only --pack .",
73
+ "build": "tsdown",
74
+ "dev": "tsdown --watch",
75
+ "typecheck": "tsc --project tsconfig.json"
76
+ }
77
+ }