@holeauth/passkey-drizzle 0.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/mysql/index.cjs +72 -0
- package/dist/mysql/index.cjs.map +1 -0
- package/dist/mysql/index.d.cts +173 -0
- package/dist/mysql/index.d.ts +173 -0
- package/dist/mysql/index.js +69 -0
- package/dist/mysql/index.js.map +1 -0
- package/dist/pg/index.cjs +70 -0
- package/dist/pg/index.cjs.map +1 -0
- package/dist/pg/index.d.cts +189 -0
- package/dist/pg/index.d.ts +189 -0
- package/dist/pg/index.js +67 -0
- package/dist/pg/index.js.map +1 -0
- package/dist/sqlite/index.cjs +70 -0
- package/dist/sqlite/index.cjs.map +1 -0
- package/dist/sqlite/index.d.cts +173 -0
- package/dist/sqlite/index.d.ts +173 -0
- package/dist/sqlite/index.js +67 -0
- package/dist/sqlite/index.js.map +1 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robert Kratz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @holeauth/passkey-drizzle
|
|
2
|
+
|
|
3
|
+
Drizzle adapter for `@holeauth/plugin-passkey`. Subpaths: `/pg`, `/mysql`, `/sqlite`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createPasskeyTables, createPasskeyAdapter } from '@holeauth/passkey-drizzle/pg';
|
|
7
|
+
import { users } from '@/db/schema';
|
|
8
|
+
|
|
9
|
+
export const pk = createPasskeyTables({ usersTable: users });
|
|
10
|
+
const passkeyAdapter = createPasskeyAdapter({ db, tables: pk.tables });
|
|
11
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var mysqlCore = require('drizzle-orm/mysql-core');
|
|
4
|
+
var drizzleOrm = require('drizzle-orm');
|
|
5
|
+
|
|
6
|
+
// src/mysql/index.ts
|
|
7
|
+
function createPasskeyTables(opts) {
|
|
8
|
+
const { usersTable, prefix = "holeauth_passkey_" } = opts;
|
|
9
|
+
const p = (s) => `${prefix}${s}`;
|
|
10
|
+
const passkeys = mysqlCore.mysqlTable(
|
|
11
|
+
p("credential"),
|
|
12
|
+
{
|
|
13
|
+
id: mysqlCore.varchar("id", { length: 191 }).primaryKey(),
|
|
14
|
+
userId: mysqlCore.varchar("user_id", { length: 191 }).notNull().references(() => usersTable.id, { onDelete: "cascade" }),
|
|
15
|
+
credentialId: mysqlCore.varchar("credential_id", { length: 512 }).notNull(),
|
|
16
|
+
publicKey: mysqlCore.text("public_key").notNull(),
|
|
17
|
+
counter: mysqlCore.int("counter").notNull().default(0),
|
|
18
|
+
transports: mysqlCore.json("transports").$type(),
|
|
19
|
+
deviceName: mysqlCore.varchar("device_name", { length: 191 }),
|
|
20
|
+
createdAt: mysqlCore.timestamp("created_at", { fsp: 3 }).notNull().defaultNow()
|
|
21
|
+
},
|
|
22
|
+
(t) => ({
|
|
23
|
+
credIdx: mysqlCore.uniqueIndex(`${p("credential")}_cred_idx`).on(t.credentialId),
|
|
24
|
+
userIdx: mysqlCore.index(`${p("credential")}_user_idx`).on(t.userId)
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
const passkeysRelations = drizzleOrm.relations(passkeys, ({ one }) => ({
|
|
28
|
+
user: one(usersTable, { fields: [passkeys.userId], references: [usersTable.id] })
|
|
29
|
+
}));
|
|
30
|
+
return { tables: { passkeys }, relations: { passkeysRelations } };
|
|
31
|
+
}
|
|
32
|
+
var rowToRecord = (r) => ({
|
|
33
|
+
id: String(r.id),
|
|
34
|
+
userId: String(r.userId),
|
|
35
|
+
credentialId: String(r.credentialId),
|
|
36
|
+
publicKey: String(r.publicKey),
|
|
37
|
+
counter: Number(r.counter),
|
|
38
|
+
transports: r.transports ?? null,
|
|
39
|
+
deviceName: r.deviceName ?? null,
|
|
40
|
+
createdAt: r.createdAt
|
|
41
|
+
});
|
|
42
|
+
function createPasskeyAdapter(opts) {
|
|
43
|
+
const { db, tables, generateId = () => crypto.randomUUID() } = opts;
|
|
44
|
+
const { passkeys } = tables;
|
|
45
|
+
return {
|
|
46
|
+
async list(userId) {
|
|
47
|
+
const rows = await db.select().from(passkeys).where(drizzleOrm.eq(passkeys.userId, userId));
|
|
48
|
+
return rows.map(rowToRecord);
|
|
49
|
+
},
|
|
50
|
+
async getByCredentialId(credentialId) {
|
|
51
|
+
const rows = await db.select().from(passkeys).where(drizzleOrm.eq(passkeys.credentialId, credentialId)).limit(1);
|
|
52
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
53
|
+
},
|
|
54
|
+
async create(data) {
|
|
55
|
+
const id = generateId();
|
|
56
|
+
await db.insert(passkeys).values({ id, ...data });
|
|
57
|
+
const rows = await db.select().from(passkeys).where(drizzleOrm.eq(passkeys.id, id)).limit(1);
|
|
58
|
+
return rowToRecord(rows[0]);
|
|
59
|
+
},
|
|
60
|
+
async updateCounter(credentialId, counter) {
|
|
61
|
+
await db.update(passkeys).set({ counter }).where(drizzleOrm.eq(passkeys.credentialId, credentialId));
|
|
62
|
+
},
|
|
63
|
+
async delete(id) {
|
|
64
|
+
await db.delete(passkeys).where(drizzleOrm.eq(passkeys.id, id));
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
exports.createPasskeyAdapter = createPasskeyAdapter;
|
|
70
|
+
exports.createPasskeyTables = createPasskeyTables;
|
|
71
|
+
//# sourceMappingURL=index.cjs.map
|
|
72
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/mysql/index.ts"],"names":["mysqlTable","varchar","text","int","json","timestamp","uniqueIndex","index","relations","eq"],"mappings":";;;;;;AAsBO,SAAS,oBAA+C,IAAA,EAAqC;AAClG,EAAA,MAAM,EAAE,UAAA,EAAY,MAAA,GAAS,mBAAA,EAAoB,GAAI,IAAA;AACrD,EAA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esContent":["import {\n mysqlTable,\n varchar,\n int,\n text,\n timestamp,\n json,\n uniqueIndex,\n index,\n type MySqlTableWithColumns,\n} from 'drizzle-orm/mysql-core';\nimport { relations, eq } from 'drizzle-orm';\nimport type { PasskeyAdapter, PasskeyRecord } from '@holeauth/plugin-passkey';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type MysqlUsersTable = MySqlTableWithColumns<any> & { id: any };\n\nexport interface CreatePasskeyTablesOptions<U extends MysqlUsersTable> {\n usersTable: U;\n prefix?: string;\n}\n\nexport function createPasskeyTables<U extends MysqlUsersTable>(opts: CreatePasskeyTablesOptions<U>) {\n const { usersTable, prefix = 'holeauth_passkey_' } = opts;\n const p = (s: string) => `${prefix}${s}`;\n const passkeys = mysqlTable(\n p('credential'),\n {\n id: varchar('id', { length: 191 }).primaryKey(),\n userId: varchar('user_id', { length: 191 })\n .notNull()\n .references(() => usersTable.id, { onDelete: 'cascade' }),\n credentialId: varchar('credential_id', { length: 512 }).notNull(),\n publicKey: text('public_key').notNull(),\n counter: int('counter').notNull().default(0),\n transports: json('transports').$type<string[]>(),\n deviceName: varchar('device_name', { length: 191 }),\n createdAt: timestamp('created_at', { fsp: 3 }).notNull().defaultNow(),\n },\n (t) => ({\n credIdx: uniqueIndex(`${p('credential')}_cred_idx`).on(t.credentialId),\n userIdx: index(`${p('credential')}_user_idx`).on(t.userId),\n }),\n );\n const passkeysRelations = relations(passkeys, ({ one }) => ({\n user: one(usersTable, { fields: [passkeys.userId], references: [usersTable.id] }),\n }));\n return { tables: { passkeys }, relations: { passkeysRelations } };\n}\n\ntype Tables = ReturnType<typeof createPasskeyTables>['tables'];\n\nexport interface CreatePasskeyAdapterOptions {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n db: any;\n tables: Tables;\n generateId?: () => string;\n}\n\nconst rowToRecord = (r: Record<string, unknown>): PasskeyRecord => ({\n id: String(r.id),\n userId: String(r.userId),\n credentialId: String(r.credentialId),\n publicKey: String(r.publicKey),\n counter: Number(r.counter),\n transports: (r.transports as string[] | null | undefined) ?? null,\n deviceName: (r.deviceName as string | null | undefined) ?? null,\n createdAt: r.createdAt as Date | undefined,\n});\n\nexport function createPasskeyAdapter(opts: CreatePasskeyAdapterOptions): PasskeyAdapter {\n const { db, tables, generateId = () => crypto.randomUUID() } = opts;\n const { passkeys } = tables;\n return {\n async list(userId) {\n const rows = await db.select().from(passkeys).where(eq(passkeys.userId, userId));\n return (rows as Record<string, unknown>[]).map(rowToRecord);\n },\n async getByCredentialId(credentialId) {\n const rows = await db\n .select()\n .from(passkeys)\n .where(eq(passkeys.credentialId, credentialId))\n .limit(1);\n return rows[0] ? rowToRecord(rows[0] as Record<string, unknown>) : null;\n },\n async create(data) {\n const id = generateId();\n await db.insert(passkeys).values({ id, ...data });\n const rows = await db.select().from(passkeys).where(eq(passkeys.id, id)).limit(1);\n return rowToRecord(rows[0] as Record<string, unknown>);\n },\n async updateCounter(credentialId, counter) {\n await db.update(passkeys).set({ counter }).where(eq(passkeys.credentialId, credentialId));\n },\n async delete(id) {\n await db.delete(passkeys).where(eq(passkeys.id, id));\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import * as drizzle_orm from 'drizzle-orm';
|
|
2
|
+
import * as drizzle_orm_mysql_core from 'drizzle-orm/mysql-core';
|
|
3
|
+
import { MySqlTableWithColumns } from 'drizzle-orm/mysql-core';
|
|
4
|
+
import { PasskeyAdapter } from '@holeauth/plugin-passkey';
|
|
5
|
+
|
|
6
|
+
type MysqlUsersTable = MySqlTableWithColumns<any> & {
|
|
7
|
+
id: any;
|
|
8
|
+
};
|
|
9
|
+
interface CreatePasskeyTablesOptions<U extends MysqlUsersTable> {
|
|
10
|
+
usersTable: U;
|
|
11
|
+
prefix?: string;
|
|
12
|
+
}
|
|
13
|
+
declare function createPasskeyTables<U extends MysqlUsersTable>(opts: CreatePasskeyTablesOptions<U>): {
|
|
14
|
+
tables: {
|
|
15
|
+
passkeys: MySqlTableWithColumns<{
|
|
16
|
+
name: string;
|
|
17
|
+
schema: undefined;
|
|
18
|
+
columns: {
|
|
19
|
+
id: drizzle_orm_mysql_core.MySqlColumn<{
|
|
20
|
+
name: "id";
|
|
21
|
+
tableName: string;
|
|
22
|
+
dataType: "string";
|
|
23
|
+
columnType: "MySqlVarChar";
|
|
24
|
+
data: string;
|
|
25
|
+
driverParam: string | number;
|
|
26
|
+
notNull: true;
|
|
27
|
+
hasDefault: false;
|
|
28
|
+
isPrimaryKey: true;
|
|
29
|
+
isAutoincrement: false;
|
|
30
|
+
hasRuntimeDefault: false;
|
|
31
|
+
enumValues: [string, ...string[]];
|
|
32
|
+
baseColumn: never;
|
|
33
|
+
identity: undefined;
|
|
34
|
+
generated: undefined;
|
|
35
|
+
}, object>;
|
|
36
|
+
userId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
37
|
+
name: "user_id";
|
|
38
|
+
tableName: string;
|
|
39
|
+
dataType: "string";
|
|
40
|
+
columnType: "MySqlVarChar";
|
|
41
|
+
data: string;
|
|
42
|
+
driverParam: string | number;
|
|
43
|
+
notNull: true;
|
|
44
|
+
hasDefault: false;
|
|
45
|
+
isPrimaryKey: false;
|
|
46
|
+
isAutoincrement: false;
|
|
47
|
+
hasRuntimeDefault: false;
|
|
48
|
+
enumValues: [string, ...string[]];
|
|
49
|
+
baseColumn: never;
|
|
50
|
+
identity: undefined;
|
|
51
|
+
generated: undefined;
|
|
52
|
+
}, object>;
|
|
53
|
+
credentialId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
54
|
+
name: "credential_id";
|
|
55
|
+
tableName: string;
|
|
56
|
+
dataType: "string";
|
|
57
|
+
columnType: "MySqlVarChar";
|
|
58
|
+
data: string;
|
|
59
|
+
driverParam: string | number;
|
|
60
|
+
notNull: true;
|
|
61
|
+
hasDefault: false;
|
|
62
|
+
isPrimaryKey: false;
|
|
63
|
+
isAutoincrement: false;
|
|
64
|
+
hasRuntimeDefault: false;
|
|
65
|
+
enumValues: [string, ...string[]];
|
|
66
|
+
baseColumn: never;
|
|
67
|
+
identity: undefined;
|
|
68
|
+
generated: undefined;
|
|
69
|
+
}, object>;
|
|
70
|
+
publicKey: drizzle_orm_mysql_core.MySqlColumn<{
|
|
71
|
+
name: "public_key";
|
|
72
|
+
tableName: string;
|
|
73
|
+
dataType: "string";
|
|
74
|
+
columnType: "MySqlText";
|
|
75
|
+
data: string;
|
|
76
|
+
driverParam: string;
|
|
77
|
+
notNull: true;
|
|
78
|
+
hasDefault: false;
|
|
79
|
+
isPrimaryKey: false;
|
|
80
|
+
isAutoincrement: false;
|
|
81
|
+
hasRuntimeDefault: false;
|
|
82
|
+
enumValues: [string, ...string[]];
|
|
83
|
+
baseColumn: never;
|
|
84
|
+
identity: undefined;
|
|
85
|
+
generated: undefined;
|
|
86
|
+
}, object>;
|
|
87
|
+
counter: drizzle_orm_mysql_core.MySqlColumn<{
|
|
88
|
+
name: "counter";
|
|
89
|
+
tableName: string;
|
|
90
|
+
dataType: "number";
|
|
91
|
+
columnType: "MySqlInt";
|
|
92
|
+
data: number;
|
|
93
|
+
driverParam: string | number;
|
|
94
|
+
notNull: true;
|
|
95
|
+
hasDefault: true;
|
|
96
|
+
isPrimaryKey: false;
|
|
97
|
+
isAutoincrement: false;
|
|
98
|
+
hasRuntimeDefault: false;
|
|
99
|
+
enumValues: undefined;
|
|
100
|
+
baseColumn: never;
|
|
101
|
+
identity: undefined;
|
|
102
|
+
generated: undefined;
|
|
103
|
+
}, object>;
|
|
104
|
+
transports: drizzle_orm_mysql_core.MySqlColumn<{
|
|
105
|
+
name: "transports";
|
|
106
|
+
tableName: string;
|
|
107
|
+
dataType: "json";
|
|
108
|
+
columnType: "MySqlJson";
|
|
109
|
+
data: string[];
|
|
110
|
+
driverParam: string;
|
|
111
|
+
notNull: false;
|
|
112
|
+
hasDefault: false;
|
|
113
|
+
isPrimaryKey: false;
|
|
114
|
+
isAutoincrement: false;
|
|
115
|
+
hasRuntimeDefault: false;
|
|
116
|
+
enumValues: undefined;
|
|
117
|
+
baseColumn: never;
|
|
118
|
+
identity: undefined;
|
|
119
|
+
generated: undefined;
|
|
120
|
+
}, object>;
|
|
121
|
+
deviceName: drizzle_orm_mysql_core.MySqlColumn<{
|
|
122
|
+
name: "device_name";
|
|
123
|
+
tableName: string;
|
|
124
|
+
dataType: "string";
|
|
125
|
+
columnType: "MySqlVarChar";
|
|
126
|
+
data: string;
|
|
127
|
+
driverParam: string | number;
|
|
128
|
+
notNull: false;
|
|
129
|
+
hasDefault: false;
|
|
130
|
+
isPrimaryKey: false;
|
|
131
|
+
isAutoincrement: false;
|
|
132
|
+
hasRuntimeDefault: false;
|
|
133
|
+
enumValues: [string, ...string[]];
|
|
134
|
+
baseColumn: never;
|
|
135
|
+
identity: undefined;
|
|
136
|
+
generated: undefined;
|
|
137
|
+
}, object>;
|
|
138
|
+
createdAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
139
|
+
name: "created_at";
|
|
140
|
+
tableName: string;
|
|
141
|
+
dataType: "date";
|
|
142
|
+
columnType: "MySqlTimestamp";
|
|
143
|
+
data: Date;
|
|
144
|
+
driverParam: string | number;
|
|
145
|
+
notNull: true;
|
|
146
|
+
hasDefault: true;
|
|
147
|
+
isPrimaryKey: false;
|
|
148
|
+
isAutoincrement: false;
|
|
149
|
+
hasRuntimeDefault: false;
|
|
150
|
+
enumValues: undefined;
|
|
151
|
+
baseColumn: never;
|
|
152
|
+
identity: undefined;
|
|
153
|
+
generated: undefined;
|
|
154
|
+
}, object>;
|
|
155
|
+
};
|
|
156
|
+
dialect: "mysql";
|
|
157
|
+
}>;
|
|
158
|
+
};
|
|
159
|
+
relations: {
|
|
160
|
+
passkeysRelations: drizzle_orm.Relations<string, {
|
|
161
|
+
user: drizzle_orm.One<U["_"]["name"], true>;
|
|
162
|
+
}>;
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
type Tables = ReturnType<typeof createPasskeyTables>['tables'];
|
|
166
|
+
interface CreatePasskeyAdapterOptions {
|
|
167
|
+
db: any;
|
|
168
|
+
tables: Tables;
|
|
169
|
+
generateId?: () => string;
|
|
170
|
+
}
|
|
171
|
+
declare function createPasskeyAdapter(opts: CreatePasskeyAdapterOptions): PasskeyAdapter;
|
|
172
|
+
|
|
173
|
+
export { type CreatePasskeyAdapterOptions, type CreatePasskeyTablesOptions, type MysqlUsersTable, createPasskeyAdapter, createPasskeyTables };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import * as drizzle_orm from 'drizzle-orm';
|
|
2
|
+
import * as drizzle_orm_mysql_core from 'drizzle-orm/mysql-core';
|
|
3
|
+
import { MySqlTableWithColumns } from 'drizzle-orm/mysql-core';
|
|
4
|
+
import { PasskeyAdapter } from '@holeauth/plugin-passkey';
|
|
5
|
+
|
|
6
|
+
type MysqlUsersTable = MySqlTableWithColumns<any> & {
|
|
7
|
+
id: any;
|
|
8
|
+
};
|
|
9
|
+
interface CreatePasskeyTablesOptions<U extends MysqlUsersTable> {
|
|
10
|
+
usersTable: U;
|
|
11
|
+
prefix?: string;
|
|
12
|
+
}
|
|
13
|
+
declare function createPasskeyTables<U extends MysqlUsersTable>(opts: CreatePasskeyTablesOptions<U>): {
|
|
14
|
+
tables: {
|
|
15
|
+
passkeys: MySqlTableWithColumns<{
|
|
16
|
+
name: string;
|
|
17
|
+
schema: undefined;
|
|
18
|
+
columns: {
|
|
19
|
+
id: drizzle_orm_mysql_core.MySqlColumn<{
|
|
20
|
+
name: "id";
|
|
21
|
+
tableName: string;
|
|
22
|
+
dataType: "string";
|
|
23
|
+
columnType: "MySqlVarChar";
|
|
24
|
+
data: string;
|
|
25
|
+
driverParam: string | number;
|
|
26
|
+
notNull: true;
|
|
27
|
+
hasDefault: false;
|
|
28
|
+
isPrimaryKey: true;
|
|
29
|
+
isAutoincrement: false;
|
|
30
|
+
hasRuntimeDefault: false;
|
|
31
|
+
enumValues: [string, ...string[]];
|
|
32
|
+
baseColumn: never;
|
|
33
|
+
identity: undefined;
|
|
34
|
+
generated: undefined;
|
|
35
|
+
}, object>;
|
|
36
|
+
userId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
37
|
+
name: "user_id";
|
|
38
|
+
tableName: string;
|
|
39
|
+
dataType: "string";
|
|
40
|
+
columnType: "MySqlVarChar";
|
|
41
|
+
data: string;
|
|
42
|
+
driverParam: string | number;
|
|
43
|
+
notNull: true;
|
|
44
|
+
hasDefault: false;
|
|
45
|
+
isPrimaryKey: false;
|
|
46
|
+
isAutoincrement: false;
|
|
47
|
+
hasRuntimeDefault: false;
|
|
48
|
+
enumValues: [string, ...string[]];
|
|
49
|
+
baseColumn: never;
|
|
50
|
+
identity: undefined;
|
|
51
|
+
generated: undefined;
|
|
52
|
+
}, object>;
|
|
53
|
+
credentialId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
54
|
+
name: "credential_id";
|
|
55
|
+
tableName: string;
|
|
56
|
+
dataType: "string";
|
|
57
|
+
columnType: "MySqlVarChar";
|
|
58
|
+
data: string;
|
|
59
|
+
driverParam: string | number;
|
|
60
|
+
notNull: true;
|
|
61
|
+
hasDefault: false;
|
|
62
|
+
isPrimaryKey: false;
|
|
63
|
+
isAutoincrement: false;
|
|
64
|
+
hasRuntimeDefault: false;
|
|
65
|
+
enumValues: [string, ...string[]];
|
|
66
|
+
baseColumn: never;
|
|
67
|
+
identity: undefined;
|
|
68
|
+
generated: undefined;
|
|
69
|
+
}, object>;
|
|
70
|
+
publicKey: drizzle_orm_mysql_core.MySqlColumn<{
|
|
71
|
+
name: "public_key";
|
|
72
|
+
tableName: string;
|
|
73
|
+
dataType: "string";
|
|
74
|
+
columnType: "MySqlText";
|
|
75
|
+
data: string;
|
|
76
|
+
driverParam: string;
|
|
77
|
+
notNull: true;
|
|
78
|
+
hasDefault: false;
|
|
79
|
+
isPrimaryKey: false;
|
|
80
|
+
isAutoincrement: false;
|
|
81
|
+
hasRuntimeDefault: false;
|
|
82
|
+
enumValues: [string, ...string[]];
|
|
83
|
+
baseColumn: never;
|
|
84
|
+
identity: undefined;
|
|
85
|
+
generated: undefined;
|
|
86
|
+
}, object>;
|
|
87
|
+
counter: drizzle_orm_mysql_core.MySqlColumn<{
|
|
88
|
+
name: "counter";
|
|
89
|
+
tableName: string;
|
|
90
|
+
dataType: "number";
|
|
91
|
+
columnType: "MySqlInt";
|
|
92
|
+
data: number;
|
|
93
|
+
driverParam: string | number;
|
|
94
|
+
notNull: true;
|
|
95
|
+
hasDefault: true;
|
|
96
|
+
isPrimaryKey: false;
|
|
97
|
+
isAutoincrement: false;
|
|
98
|
+
hasRuntimeDefault: false;
|
|
99
|
+
enumValues: undefined;
|
|
100
|
+
baseColumn: never;
|
|
101
|
+
identity: undefined;
|
|
102
|
+
generated: undefined;
|
|
103
|
+
}, object>;
|
|
104
|
+
transports: drizzle_orm_mysql_core.MySqlColumn<{
|
|
105
|
+
name: "transports";
|
|
106
|
+
tableName: string;
|
|
107
|
+
dataType: "json";
|
|
108
|
+
columnType: "MySqlJson";
|
|
109
|
+
data: string[];
|
|
110
|
+
driverParam: string;
|
|
111
|
+
notNull: false;
|
|
112
|
+
hasDefault: false;
|
|
113
|
+
isPrimaryKey: false;
|
|
114
|
+
isAutoincrement: false;
|
|
115
|
+
hasRuntimeDefault: false;
|
|
116
|
+
enumValues: undefined;
|
|
117
|
+
baseColumn: never;
|
|
118
|
+
identity: undefined;
|
|
119
|
+
generated: undefined;
|
|
120
|
+
}, object>;
|
|
121
|
+
deviceName: drizzle_orm_mysql_core.MySqlColumn<{
|
|
122
|
+
name: "device_name";
|
|
123
|
+
tableName: string;
|
|
124
|
+
dataType: "string";
|
|
125
|
+
columnType: "MySqlVarChar";
|
|
126
|
+
data: string;
|
|
127
|
+
driverParam: string | number;
|
|
128
|
+
notNull: false;
|
|
129
|
+
hasDefault: false;
|
|
130
|
+
isPrimaryKey: false;
|
|
131
|
+
isAutoincrement: false;
|
|
132
|
+
hasRuntimeDefault: false;
|
|
133
|
+
enumValues: [string, ...string[]];
|
|
134
|
+
baseColumn: never;
|
|
135
|
+
identity: undefined;
|
|
136
|
+
generated: undefined;
|
|
137
|
+
}, object>;
|
|
138
|
+
createdAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
139
|
+
name: "created_at";
|
|
140
|
+
tableName: string;
|
|
141
|
+
dataType: "date";
|
|
142
|
+
columnType: "MySqlTimestamp";
|
|
143
|
+
data: Date;
|
|
144
|
+
driverParam: string | number;
|
|
145
|
+
notNull: true;
|
|
146
|
+
hasDefault: true;
|
|
147
|
+
isPrimaryKey: false;
|
|
148
|
+
isAutoincrement: false;
|
|
149
|
+
hasRuntimeDefault: false;
|
|
150
|
+
enumValues: undefined;
|
|
151
|
+
baseColumn: never;
|
|
152
|
+
identity: undefined;
|
|
153
|
+
generated: undefined;
|
|
154
|
+
}, object>;
|
|
155
|
+
};
|
|
156
|
+
dialect: "mysql";
|
|
157
|
+
}>;
|
|
158
|
+
};
|
|
159
|
+
relations: {
|
|
160
|
+
passkeysRelations: drizzle_orm.Relations<string, {
|
|
161
|
+
user: drizzle_orm.One<U["_"]["name"], true>;
|
|
162
|
+
}>;
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
type Tables = ReturnType<typeof createPasskeyTables>['tables'];
|
|
166
|
+
interface CreatePasskeyAdapterOptions {
|
|
167
|
+
db: any;
|
|
168
|
+
tables: Tables;
|
|
169
|
+
generateId?: () => string;
|
|
170
|
+
}
|
|
171
|
+
declare function createPasskeyAdapter(opts: CreatePasskeyAdapterOptions): PasskeyAdapter;
|
|
172
|
+
|
|
173
|
+
export { type CreatePasskeyAdapterOptions, type CreatePasskeyTablesOptions, type MysqlUsersTable, createPasskeyAdapter, createPasskeyTables };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { mysqlTable, timestamp, varchar, json, int, text, index, uniqueIndex } from 'drizzle-orm/mysql-core';
|
|
2
|
+
import { relations, eq } from 'drizzle-orm';
|
|
3
|
+
|
|
4
|
+
// src/mysql/index.ts
|
|
5
|
+
function createPasskeyTables(opts) {
|
|
6
|
+
const { usersTable, prefix = "holeauth_passkey_" } = opts;
|
|
7
|
+
const p = (s) => `${prefix}${s}`;
|
|
8
|
+
const passkeys = mysqlTable(
|
|
9
|
+
p("credential"),
|
|
10
|
+
{
|
|
11
|
+
id: varchar("id", { length: 191 }).primaryKey(),
|
|
12
|
+
userId: varchar("user_id", { length: 191 }).notNull().references(() => usersTable.id, { onDelete: "cascade" }),
|
|
13
|
+
credentialId: varchar("credential_id", { length: 512 }).notNull(),
|
|
14
|
+
publicKey: text("public_key").notNull(),
|
|
15
|
+
counter: int("counter").notNull().default(0),
|
|
16
|
+
transports: json("transports").$type(),
|
|
17
|
+
deviceName: varchar("device_name", { length: 191 }),
|
|
18
|
+
createdAt: timestamp("created_at", { fsp: 3 }).notNull().defaultNow()
|
|
19
|
+
},
|
|
20
|
+
(t) => ({
|
|
21
|
+
credIdx: uniqueIndex(`${p("credential")}_cred_idx`).on(t.credentialId),
|
|
22
|
+
userIdx: index(`${p("credential")}_user_idx`).on(t.userId)
|
|
23
|
+
})
|
|
24
|
+
);
|
|
25
|
+
const passkeysRelations = relations(passkeys, ({ one }) => ({
|
|
26
|
+
user: one(usersTable, { fields: [passkeys.userId], references: [usersTable.id] })
|
|
27
|
+
}));
|
|
28
|
+
return { tables: { passkeys }, relations: { passkeysRelations } };
|
|
29
|
+
}
|
|
30
|
+
var rowToRecord = (r) => ({
|
|
31
|
+
id: String(r.id),
|
|
32
|
+
userId: String(r.userId),
|
|
33
|
+
credentialId: String(r.credentialId),
|
|
34
|
+
publicKey: String(r.publicKey),
|
|
35
|
+
counter: Number(r.counter),
|
|
36
|
+
transports: r.transports ?? null,
|
|
37
|
+
deviceName: r.deviceName ?? null,
|
|
38
|
+
createdAt: r.createdAt
|
|
39
|
+
});
|
|
40
|
+
function createPasskeyAdapter(opts) {
|
|
41
|
+
const { db, tables, generateId = () => crypto.randomUUID() } = opts;
|
|
42
|
+
const { passkeys } = tables;
|
|
43
|
+
return {
|
|
44
|
+
async list(userId) {
|
|
45
|
+
const rows = await db.select().from(passkeys).where(eq(passkeys.userId, userId));
|
|
46
|
+
return rows.map(rowToRecord);
|
|
47
|
+
},
|
|
48
|
+
async getByCredentialId(credentialId) {
|
|
49
|
+
const rows = await db.select().from(passkeys).where(eq(passkeys.credentialId, credentialId)).limit(1);
|
|
50
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
51
|
+
},
|
|
52
|
+
async create(data) {
|
|
53
|
+
const id = generateId();
|
|
54
|
+
await db.insert(passkeys).values({ id, ...data });
|
|
55
|
+
const rows = await db.select().from(passkeys).where(eq(passkeys.id, id)).limit(1);
|
|
56
|
+
return rowToRecord(rows[0]);
|
|
57
|
+
},
|
|
58
|
+
async updateCounter(credentialId, counter) {
|
|
59
|
+
await db.update(passkeys).set({ counter }).where(eq(passkeys.credentialId, credentialId));
|
|
60
|
+
},
|
|
61
|
+
async delete(id) {
|
|
62
|
+
await db.delete(passkeys).where(eq(passkeys.id, id));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { createPasskeyAdapter, createPasskeyTables };
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/mysql/index.ts"],"names":[],"mappings":";;;;AAsBO,SAAS,oBAA+C,IAAA,EAAqC;AAClG,EAAA,MAAM,EAAE,UAAA,EAAY,MAAA,GAAS,mBAAA,EAAoB,GAAI,IAAA;AACrD,EAAA,MAAM,IAAI,CAAC,CAAA,KAAc,CAAA,EAAG,MAAM,GAAG,CAAC,CAAA,CAAA;AACtC,EAAA,MAAM,QAAA,GAAW,UAAA;AAAA,IACf,EAAE,YAAY,CAAA;AAAA,IACd;AAAA,MACE,EAAA,EAAI,QAAQ,IAAA,EAAM,EAAE,QAAQ,GAAA,EAAK,EAAE,UAAA,EAAW;AAAA,MAC9C,QAAQ,O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,OAAO,KAAK,CAAC,CAAA,GAAI,YAAY,IAAA,CAAK,CAAC,CAA4B,CAAA,GAAI,IAAA;AAAA,IACrE,CAAA;AAAA,IACA,MAAM,OAAO,IAAA,EAAM;AACjB,MAAA,MAAM,KAAK,UAAA,EAAW;AACtB,MAAA,MAAM,EAAA,CAAG,OAAO,QAAQ,CAAA,CAAE,OAAO,EAAE,EAAA,EAAI,GAAG,IAAA,EAAM,CAAA;AAChD,MAAA,MAAM,OAAO,MAAM,EAAA,CAAG,MAAA,EAAO,CAAE,KAAK,QAAQ,CAAA,CAAE,KAAA,CAAM,EAAA,CAAG,SAAS,EAAA,EAAI,EAAE,CAAC,CAAA,CAAE,MAAM,CAAC,CAAA;AAChF,MAAA,OAAO,WAAA,CAAY,IAAA,CAAK,CAAC,CAA4B,CAAA;AAAA,IACvD,CAAA;AAAA,IACA,MAAM,aAAA,CAAc,YAAA,EAAc,OAAA,EAAS;AACzC,MAAA,MAAM,EAAA,CAAG,MAAA,CAAO,QAAQ,CAAA,CAAE,IAAI,EAAE,OAAA,EAAS,CAAA,CAAE,KAAA,CAAM,EAAA,CAAG,QAAA,CAAS,YAAA,EAAc,YAAY,CAAC,CAAA;AAAA,IAC1F,CAAA;AAAA,IACA,MAAM,OAAO,EAAA,EAAI;AACf,MAAA,MAAM,EAAA,CAAG,OAAO,QAAQ,CAAA,CAAE,MAAM,EAAA,CAAG,QAAA,CAAS,EAAA,EAAI,EAAE,CAAC,CAAA;AAAA,IACrD;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import {\n mysqlTable,\n varchar,\n int,\n text,\n timestamp,\n json,\n uniqueIndex,\n index,\n type MySqlTableWithColumns,\n} from 'drizzle-orm/mysql-core';\nimport { relations, eq } from 'drizzle-orm';\nimport type { PasskeyAdapter, PasskeyRecord } from '@holeauth/plugin-passkey';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type MysqlUsersTable = MySqlTableWithColumns<any> & { id: any };\n\nexport interface CreatePasskeyTablesOptions<U extends MysqlUsersTable> {\n usersTable: U;\n prefix?: string;\n}\n\nexport function createPasskeyTables<U extends MysqlUsersTable>(opts: CreatePasskeyTablesOptions<U>) {\n const { usersTable, prefix = 'holeauth_passkey_' } = opts;\n const p = (s: string) => `${prefix}${s}`;\n const passkeys = mysqlTable(\n p('credential'),\n {\n id: varchar('id', { length: 191 }).primaryKey(),\n userId: varchar('user_id', { length: 191 })\n .notNull()\n .references(() => usersTable.id, { onDelete: 'cascade' }),\n credentialId: varchar('credential_id', { length: 512 }).notNull(),\n publicKey: text('public_key').notNull(),\n counter: int('counter').notNull().default(0),\n transports: json('transports').$type<string[]>(),\n deviceName: varchar('device_name', { length: 191 }),\n createdAt: timestamp('created_at', { fsp: 3 }).notNull().defaultNow(),\n },\n (t) => ({\n credIdx: uniqueIndex(`${p('credential')}_cred_idx`).on(t.credentialId),\n userIdx: index(`${p('credential')}_user_idx`).on(t.userId),\n }),\n );\n const passkeysRelations = relations(passkeys, ({ one }) => ({\n user: one(usersTable, { fields: [passkeys.userId], references: [usersTable.id] }),\n }));\n return { tables: { passkeys }, relations: { passkeysRelations } };\n}\n\ntype Tables = ReturnType<typeof createPasskeyTables>['tables'];\n\nexport interface CreatePasskeyAdapterOptions {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n db: any;\n tables: Tables;\n generateId?: () => string;\n}\n\nconst rowToRecord = (r: Record<string, unknown>): PasskeyRecord => ({\n id: String(r.id),\n userId: String(r.userId),\n credentialId: String(r.credentialId),\n publicKey: String(r.publicKey),\n counter: Number(r.counter),\n transports: (r.transports as string[] | null | undefined) ?? null,\n deviceName: (r.deviceName as string | null | undefined) ?? null,\n createdAt: r.createdAt as Date | undefined,\n});\n\nexport function createPasskeyAdapter(opts: CreatePasskeyAdapterOptions): PasskeyAdapter {\n const { db, tables, generateId = () => crypto.randomUUID() } = opts;\n const { passkeys } = tables;\n return {\n async list(userId) {\n const rows = await db.select().from(passkeys).where(eq(passkeys.userId, userId));\n return (rows as Record<string, unknown>[]).map(rowToRecord);\n },\n async getByCredentialId(credentialId) {\n const rows = await db\n .select()\n .from(passkeys)\n .where(eq(passkeys.credentialId, credentialId))\n .limit(1);\n return rows[0] ? rowToRecord(rows[0] as Record<string, unknown>) : null;\n },\n async create(data) {\n const id = generateId();\n await db.insert(passkeys).values({ id, ...data });\n const rows = await db.select().from(passkeys).where(eq(passkeys.id, id)).limit(1);\n return rowToRecord(rows[0] as Record<string, unknown>);\n },\n async updateCounter(credentialId, counter) {\n await db.update(passkeys).set({ counter }).where(eq(passkeys.credentialId, credentialId));\n },\n async delete(id) {\n await db.delete(passkeys).where(eq(passkeys.id, id));\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var pgCore = require('drizzle-orm/pg-core');
|
|
4
|
+
var drizzleOrm = require('drizzle-orm');
|
|
5
|
+
|
|
6
|
+
// src/pg/index.ts
|
|
7
|
+
function createPasskeyTables(opts) {
|
|
8
|
+
const { usersTable, prefix = "holeauth_passkey_" } = opts;
|
|
9
|
+
const p = (s) => `${prefix}${s}`;
|
|
10
|
+
const passkeys = pgCore.pgTable(
|
|
11
|
+
p("credential"),
|
|
12
|
+
{
|
|
13
|
+
id: pgCore.text("id").primaryKey(),
|
|
14
|
+
userId: pgCore.text("user_id").notNull().references(() => usersTable.id, { onDelete: "cascade" }),
|
|
15
|
+
credentialId: pgCore.text("credential_id").notNull(),
|
|
16
|
+
publicKey: pgCore.text("public_key").notNull(),
|
|
17
|
+
counter: pgCore.integer("counter").notNull().default(0),
|
|
18
|
+
transports: pgCore.text("transports").array(),
|
|
19
|
+
deviceName: pgCore.text("device_name"),
|
|
20
|
+
createdAt: pgCore.timestamp("created_at", { withTimezone: true, mode: "date" }).notNull().defaultNow()
|
|
21
|
+
},
|
|
22
|
+
(t) => ({
|
|
23
|
+
credIdx: pgCore.uniqueIndex().on(t.credentialId),
|
|
24
|
+
userIdx: pgCore.index().on(t.userId)
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
const passkeysRelations = drizzleOrm.relations(passkeys, ({ one }) => ({
|
|
28
|
+
user: one(usersTable, { fields: [passkeys.userId], references: [usersTable.id] })
|
|
29
|
+
}));
|
|
30
|
+
return { tables: { passkeys }, relations: { passkeysRelations } };
|
|
31
|
+
}
|
|
32
|
+
var rowToRecord = (r) => ({
|
|
33
|
+
id: String(r.id),
|
|
34
|
+
userId: String(r.userId),
|
|
35
|
+
credentialId: String(r.credentialId),
|
|
36
|
+
publicKey: String(r.publicKey),
|
|
37
|
+
counter: Number(r.counter),
|
|
38
|
+
transports: r.transports ?? null,
|
|
39
|
+
deviceName: r.deviceName ?? null,
|
|
40
|
+
createdAt: r.createdAt
|
|
41
|
+
});
|
|
42
|
+
function createPasskeyAdapter(opts) {
|
|
43
|
+
const { db, tables, generateId = () => crypto.randomUUID() } = opts;
|
|
44
|
+
const { passkeys } = tables;
|
|
45
|
+
return {
|
|
46
|
+
async list(userId) {
|
|
47
|
+
const rows = await db.select().from(passkeys).where(drizzleOrm.eq(passkeys.userId, userId));
|
|
48
|
+
return rows.map(rowToRecord);
|
|
49
|
+
},
|
|
50
|
+
async getByCredentialId(credentialId) {
|
|
51
|
+
const rows = await db.select().from(passkeys).where(drizzleOrm.eq(passkeys.credentialId, credentialId)).limit(1);
|
|
52
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
53
|
+
},
|
|
54
|
+
async create(data) {
|
|
55
|
+
const [row] = await db.insert(passkeys).values({ id: generateId(), ...data }).returning();
|
|
56
|
+
return rowToRecord(row);
|
|
57
|
+
},
|
|
58
|
+
async updateCounter(credentialId, counter) {
|
|
59
|
+
await db.update(passkeys).set({ counter }).where(drizzleOrm.eq(passkeys.credentialId, credentialId));
|
|
60
|
+
},
|
|
61
|
+
async delete(id) {
|
|
62
|
+
await db.delete(passkeys).where(drizzleOrm.eq(passkeys.id, id));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
exports.createPasskeyAdapter = createPasskeyAdapter;
|
|
68
|
+
exports.createPasskeyTables = createPasskeyTables;
|
|
69
|
+
//# sourceMappingURL=index.cjs.map
|
|
70
|
+
//# sourceMappingURL=index.cjs.map
|