@gauts/auth 0.5.1 → 0.6.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/README.md +140 -98
- package/SECURITY.md +1 -1
- package/dist/adapters/hono/index.d.ts +15 -16
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +1 -3
- package/dist/adapters/hono/index.js.map +1 -1
- package/dist/adapters/prisma/config.d.ts +7 -0
- package/dist/adapters/prisma/config.d.ts.map +1 -0
- package/dist/adapters/prisma/config.js +151 -0
- package/dist/adapters/prisma/config.js.map +1 -0
- package/dist/adapters/prisma/index.d.ts +3 -48
- package/dist/adapters/prisma/index.d.ts.map +1 -1
- package/dist/adapters/prisma/index.js +12 -221
- package/dist/adapters/prisma/index.js.map +1 -1
- package/dist/adapters/prisma/model.d.ts +33 -0
- package/dist/adapters/prisma/model.d.ts.map +1 -0
- package/dist/adapters/prisma/model.js +110 -0
- package/dist/adapters/prisma/model.js.map +1 -0
- package/dist/adapters/prisma/types.d.ts +125 -0
- package/dist/adapters/prisma/types.d.ts.map +1 -0
- package/dist/adapters/prisma/types.js +2 -0
- package/dist/adapters/prisma/types.js.map +1 -0
- package/dist/auth.d.ts +6 -6
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +1 -1
- package/dist/auth.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/session/cache.d.ts +7 -7
- package/dist/session/cache.d.ts.map +1 -1
- package/dist/session/cache.js +4 -33
- package/dist/session/cache.js.map +1 -1
- package/dist/session/guards.d.ts +2 -2
- package/dist/session/guards.d.ts.map +1 -1
- package/dist/session/guards.js +12 -13
- package/dist/session/guards.js.map +1 -1
- package/dist/session/service.d.ts +4 -4
- package/dist/session/service.d.ts.map +1 -1
- package/dist/session/service.js +0 -4
- package/dist/session/service.js.map +1 -1
- package/dist/session/types.d.ts +20 -25
- package/dist/session/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { createError } from "../../errors.js";
|
|
2
|
+
import { isRecord } from "../../session/guards.js";
|
|
3
|
+
const PRISMA_DEFAULTS = {
|
|
4
|
+
account: {
|
|
5
|
+
name: "account",
|
|
6
|
+
select: ["id", "email"],
|
|
7
|
+
},
|
|
8
|
+
relation: {
|
|
9
|
+
select: ["id"],
|
|
10
|
+
},
|
|
11
|
+
sessions: {
|
|
12
|
+
name: "sessions",
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
export const isPrismaDelegate = (value) => {
|
|
16
|
+
return (isRecord(value) &&
|
|
17
|
+
typeof value.create === "function" &&
|
|
18
|
+
typeof value.findFirst === "function" &&
|
|
19
|
+
typeof value.findMany === "function" &&
|
|
20
|
+
typeof value.findUnique === "function" &&
|
|
21
|
+
typeof value.update === "function" &&
|
|
22
|
+
typeof value.updateMany === "function");
|
|
23
|
+
};
|
|
24
|
+
const isAuthScalar = (value) => {
|
|
25
|
+
return (value === null ||
|
|
26
|
+
typeof value === "boolean" ||
|
|
27
|
+
typeof value === "string" ||
|
|
28
|
+
(typeof value === "number" && Number.isFinite(value)));
|
|
29
|
+
};
|
|
30
|
+
const resolveName = ({ input, path }) => {
|
|
31
|
+
if (typeof input !== "string" || !input.trim()) {
|
|
32
|
+
throw createError({
|
|
33
|
+
code: "AUTH_CONFIG_INVALID",
|
|
34
|
+
message: `${path} must be a non-empty string.`,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return input;
|
|
38
|
+
};
|
|
39
|
+
const resolveSelect = ({ defaultSelect, input, path, }) => {
|
|
40
|
+
if (input === undefined) {
|
|
41
|
+
return defaultSelect;
|
|
42
|
+
}
|
|
43
|
+
if (!Array.isArray(input)) {
|
|
44
|
+
throw createError({
|
|
45
|
+
code: "AUTH_CONFIG_INVALID",
|
|
46
|
+
message: `${path}.select must contain unique non-empty field names.`,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
const fields = input;
|
|
50
|
+
if (fields.some((field) => typeof field !== "string" || !field.trim()) ||
|
|
51
|
+
new Set(fields).size !== fields.length) {
|
|
52
|
+
throw createError({
|
|
53
|
+
code: "AUTH_CONFIG_INVALID",
|
|
54
|
+
message: `${path}.select must contain unique non-empty field names.`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const names = fields;
|
|
58
|
+
return names.includes("id") ? names : ["id", ...names];
|
|
59
|
+
};
|
|
60
|
+
const isAccessValue = (value) => {
|
|
61
|
+
if (Array.isArray(value)) {
|
|
62
|
+
return value.length > 0 && value.every(isAuthScalar);
|
|
63
|
+
}
|
|
64
|
+
return isAuthScalar(value);
|
|
65
|
+
};
|
|
66
|
+
const resolveAccess = ({ input, path }) => {
|
|
67
|
+
if (input === undefined) {
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
if (!isRecord(input) || Object.values(input).some((value) => !isAccessValue(value))) {
|
|
71
|
+
throw createError({
|
|
72
|
+
code: "AUTH_CONFIG_INVALID",
|
|
73
|
+
message: `${path}.access contains an invalid condition.`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return input;
|
|
77
|
+
};
|
|
78
|
+
const resolveModel = ({ client, defaultName, defaultSelect, input, path, relation, }) => {
|
|
79
|
+
if (input !== undefined && !isRecord(input)) {
|
|
80
|
+
throw createError({
|
|
81
|
+
code: "AUTH_CONFIG_INVALID",
|
|
82
|
+
message: `${path} model configuration is invalid.`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const config = isRecord(input) ? input : {};
|
|
86
|
+
const name = config.name === undefined
|
|
87
|
+
? defaultName
|
|
88
|
+
: resolveName({ input: config.name, path: `${path}.name` });
|
|
89
|
+
const delegate = isRecord(client) ? client[name] : undefined;
|
|
90
|
+
if (!isPrismaDelegate(delegate)) {
|
|
91
|
+
throw createError({
|
|
92
|
+
code: "AUTH_CONFIG_INVALID",
|
|
93
|
+
message: `Prisma model ${name} is invalid.`,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (config.relations !== undefined && !isRecord(config.relations)) {
|
|
97
|
+
throw createError({
|
|
98
|
+
code: "AUTH_CONFIG_INVALID",
|
|
99
|
+
message: `${path}.relations is invalid.`,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const relations = Object.fromEntries(Object.entries(isRecord(config.relations) ? config.relations : {}).map(([relationName, relationConfig]) => [
|
|
103
|
+
relationName,
|
|
104
|
+
resolveModel({
|
|
105
|
+
client,
|
|
106
|
+
defaultName: relationName,
|
|
107
|
+
defaultSelect: PRISMA_DEFAULTS.relation.select,
|
|
108
|
+
input: relationConfig,
|
|
109
|
+
path: `${path}.relations.${relationName}`,
|
|
110
|
+
relation: relationName,
|
|
111
|
+
}),
|
|
112
|
+
]));
|
|
113
|
+
return {
|
|
114
|
+
access: resolveAccess({ input: config.access, path }),
|
|
115
|
+
name,
|
|
116
|
+
relation,
|
|
117
|
+
relations,
|
|
118
|
+
select: resolveSelect({ defaultSelect, input: config.select, path }),
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
export const resolvePrismaModels = ({ client, input, }) => {
|
|
122
|
+
if (input !== undefined && !isRecord(input)) {
|
|
123
|
+
throw createError({
|
|
124
|
+
code: "AUTH_CONFIG_INVALID",
|
|
125
|
+
message: "Prisma models configuration is invalid.",
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const models = isRecord(input) ? input : {};
|
|
129
|
+
if (models.sessions !== undefined && !isRecord(models.sessions)) {
|
|
130
|
+
throw createError({
|
|
131
|
+
code: "AUTH_CONFIG_INVALID",
|
|
132
|
+
message: "Prisma sessions model configuration is invalid.",
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const sessions = isRecord(models.sessions) ? models.sessions : {};
|
|
136
|
+
const sessionName = sessions.name === undefined
|
|
137
|
+
? PRISMA_DEFAULTS.sessions.name
|
|
138
|
+
: resolveName({ input: sessions.name, path: "models.sessions.name" });
|
|
139
|
+
return {
|
|
140
|
+
account: resolveModel({
|
|
141
|
+
client,
|
|
142
|
+
defaultName: PRISMA_DEFAULTS.account.name,
|
|
143
|
+
defaultSelect: PRISMA_DEFAULTS.account.select,
|
|
144
|
+
input: models.account,
|
|
145
|
+
path: "models.account",
|
|
146
|
+
relation: "account",
|
|
147
|
+
}),
|
|
148
|
+
sessions: sessionName,
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/adapters/prisma/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AASnD,MAAM,eAAe,GAAG;IACpB,OAAO,EAAE;QACL,IAAI,EAAE,SAAS;QACf,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC;KAC1B;IACD,QAAQ,EAAE;QACN,MAAM,EAAE,CAAC,IAAI,CAAC;KACjB;IACD,QAAQ,EAAE;QACN,IAAI,EAAE,UAAU;KACnB;CACK,CAAC;AAWX,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAc,EAA2B,EAAE;IACxE,OAAO,CACH,QAAQ,CAAC,KAAK,CAAC;QACf,OAAO,KAAK,CAAC,MAAM,KAAK,UAAU;QAClC,OAAO,KAAK,CAAC,SAAS,KAAK,UAAU;QACrC,OAAO,KAAK,CAAC,QAAQ,KAAK,UAAU;QACpC,OAAO,KAAK,CAAC,UAAU,KAAK,UAAU;QACtC,OAAO,KAAK,CAAC,MAAM,KAAK,UAAU;QAClC,OAAO,KAAK,CAAC,UAAU,KAAK,UAAU,CACzC,CAAC;AACN,CAAC,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,KAAc,EAAuB,EAAE;IACzD,OAAO,CACH,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAA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AC,IAAI,EAAE,IAAI,EAAE,sBAAsB,EAAE,CAAC,CAAC;IAE9E,OAAO;QACH,OAAO,EAAE,YAAY,CAAC;YAClB,MAAM;YACN,WAAW,EAAE,eAAe,CAAC,OAAO,CAAC,IAAI;YACzC,aAAa,EAAE,eAAe,CAAC,OAAO,CAAC,MAAM;YAC7C,KAAK,EAAE,MAAM,CAAC,OAAO;YACrB,IAAI,EAAE,gBAAgB;YACtB,QAAQ,EAAE,SAAS;SACtB,CAAC;QACF,QAAQ,EAAE,WAAW;KACxB,CAAC;AACN,CAAC,CAAC"}
|
|
@@ -1,49 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type DelegateRow<T> = DelegateMeta<T> extends {
|
|
5
|
-
types: {
|
|
6
|
-
payload: {
|
|
7
|
-
scalars: infer Result;
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
} ? Result : never;
|
|
11
|
-
type IsSessionTable<T> = [DelegateRow<T>] extends [never] ? false : DelegateRow<T> extends SessionRecord ? true : false;
|
|
12
|
-
export type PrismaSessionTable<Client> = {
|
|
13
|
-
[Key in keyof Client]: IsSessionTable<Client[Key]> extends true ? Key : never;
|
|
14
|
-
}[keyof Client] & string;
|
|
15
|
-
export type PrismaAccessRules = {
|
|
16
|
-
allowedRoles?: readonly string[];
|
|
17
|
-
allowedStatuses: readonly string[];
|
|
18
|
-
};
|
|
19
|
-
export type PrismaRelationsConfig = {
|
|
20
|
-
account?: string;
|
|
21
|
-
user?: string;
|
|
22
|
-
};
|
|
23
|
-
export type PrismaSessionConfig<Client> = {
|
|
24
|
-
relations?: PrismaRelationsConfig;
|
|
25
|
-
table?: PrismaSessionTable<Client>;
|
|
26
|
-
};
|
|
27
|
-
export type PrismaAdapterConfig<Client> = {
|
|
28
|
-
access: {
|
|
29
|
-
account: PrismaAccessRules;
|
|
30
|
-
user: PrismaAccessRules;
|
|
31
|
-
};
|
|
32
|
-
session?: PrismaSessionConfig<Client>;
|
|
33
|
-
};
|
|
34
|
-
type DefaultPrismaAdapterInput<Client extends object> = {
|
|
35
|
-
client: Client;
|
|
36
|
-
config: PrismaAdapterConfig<Client>;
|
|
37
|
-
};
|
|
38
|
-
type CustomPrismaAdapterInput<Client extends object> = {
|
|
39
|
-
client: Client;
|
|
40
|
-
config: PrismaAdapterConfig<Client> & {
|
|
41
|
-
session: PrismaSessionConfig<Client> & {
|
|
42
|
-
table: PrismaSessionTable<Client>;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
export type PrismaAdapterInput<Client extends object> = typeof DEFAULT_TABLE extends PrismaSessionTable<Client> ? DefaultPrismaAdapterInput<Client> : CustomPrismaAdapterInput<Client>;
|
|
47
|
-
export declare const createPrismaAdapter: <Client extends object>(input: PrismaAdapterInput<Client>) => DbAdapter;
|
|
48
|
-
export {};
|
|
1
|
+
import type { PrismaAdapterInput, PrismaDb, PrismaModelsConfig } from "./types.js";
|
|
2
|
+
export type { PrismaAccount, PrismaAccountConfig, PrismaAccountModel, PrismaAdapterInput, PrismaModelsConfig, PrismaRelations, PrismaSessionConfig, PrismaSessionModel, } from "./types.js";
|
|
3
|
+
export declare const createPrismaAdapter: <Client extends object, const Models extends PrismaModelsConfig<Client> | undefined = undefined>(input: PrismaAdapterInput<Client, Models>) => PrismaDb<Client, Models>;
|
|
49
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAiB,kBAAkB,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAElG,YAAY,EACR,aAAa,EACb,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,kBAAkB,GACrB,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,mBAAmB,GAC5B,MAAM,SAAS,MAAM,EACrB,KAAK,CAAC,MAAM,SAAS,kBAAkB,CAAC,MAAM,CAAC,GAAG,SAAS,GAAG,SAAS,EAEvE,OAAO,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,KAC1C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAqFzB,CAAC"}
|
|
@@ -1,224 +1,16 @@
|
|
|
1
1
|
import { createError } from "../../errors.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
account_id: true,
|
|
6
|
-
agent: true,
|
|
7
|
-
created_at: true,
|
|
8
|
-
expires_at: true,
|
|
9
|
-
id: true,
|
|
10
|
-
ip: true,
|
|
11
|
-
platform: true,
|
|
12
|
-
revoked_at: true,
|
|
13
|
-
token_hash: true,
|
|
14
|
-
updated_at: true,
|
|
15
|
-
};
|
|
16
|
-
const isNullableDate = (value) => {
|
|
17
|
-
return value instanceof Date || value === null;
|
|
18
|
-
};
|
|
19
|
-
const isStringArray = (value) => {
|
|
20
|
-
return (Array.isArray(value) &&
|
|
21
|
-
value.every((item) => typeof item === "string" && item.length > 0));
|
|
22
|
-
};
|
|
23
|
-
const isDelegate = (value) => {
|
|
24
|
-
return (isRecord(value) &&
|
|
25
|
-
typeof value.create === "function" &&
|
|
26
|
-
typeof value.findFirst === "function" &&
|
|
27
|
-
typeof value.findMany === "function" &&
|
|
28
|
-
typeof value.findUnique === "function" &&
|
|
29
|
-
typeof value.update === "function" &&
|
|
30
|
-
typeof value.updateMany === "function");
|
|
31
|
-
};
|
|
32
|
-
const isSessionRecord = (value) => {
|
|
33
|
-
return (isRecord(value) &&
|
|
34
|
-
typeof value.account_id === "string" &&
|
|
35
|
-
isNullableString(value.agent) &&
|
|
36
|
-
value.created_at instanceof Date &&
|
|
37
|
-
value.expires_at instanceof Date &&
|
|
38
|
-
typeof value.id === "string" &&
|
|
39
|
-
isNullableString(value.ip) &&
|
|
40
|
-
isNullableString(value.platform) &&
|
|
41
|
-
isNullableDate(value.revoked_at) &&
|
|
42
|
-
typeof value.token_hash === "string" &&
|
|
43
|
-
isNullableDate(value.updated_at));
|
|
44
|
-
};
|
|
45
|
-
const resolveValues = ({ input, name }) => {
|
|
46
|
-
if (!isStringArray(input) || input.length === 0 || new Set(input).size !== input.length) {
|
|
47
|
-
throw createError({
|
|
48
|
-
code: "AUTH_CONFIG_INVALID",
|
|
49
|
-
message: `${name} must contain unique non-empty strings.`,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
return input;
|
|
53
|
-
};
|
|
54
|
-
const resolveAccess = (config) => {
|
|
55
|
-
if (!isRecord(config) ||
|
|
56
|
-
!isRecord(config.access) ||
|
|
57
|
-
!isRecord(config.access.account) ||
|
|
58
|
-
!isRecord(config.access.user)) {
|
|
59
|
-
throw createError({
|
|
60
|
-
code: "AUTH_CONFIG_INVALID",
|
|
61
|
-
message: "Prisma account and user access rules are required.",
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
return {
|
|
65
|
-
account: {
|
|
66
|
-
roles: config.access.account.allowedRoles === undefined
|
|
67
|
-
? null
|
|
68
|
-
: resolveValues({
|
|
69
|
-
input: config.access.account.allowedRoles,
|
|
70
|
-
name: "access.account.allowedRoles",
|
|
71
|
-
}),
|
|
72
|
-
statuses: resolveValues({
|
|
73
|
-
input: config.access.account.allowedStatuses,
|
|
74
|
-
name: "access.account.allowedStatuses",
|
|
75
|
-
}),
|
|
76
|
-
},
|
|
77
|
-
user: {
|
|
78
|
-
roles: config.access.user.allowedRoles === undefined
|
|
79
|
-
? null
|
|
80
|
-
: resolveValues({
|
|
81
|
-
input: config.access.user.allowedRoles,
|
|
82
|
-
name: "access.user.allowedRoles",
|
|
83
|
-
}),
|
|
84
|
-
statuses: resolveValues({
|
|
85
|
-
input: config.access.user.allowedStatuses,
|
|
86
|
-
name: "access.user.allowedStatuses",
|
|
87
|
-
}),
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
const resolveName = ({ input, name }) => {
|
|
92
|
-
if (typeof input !== "string" || !input) {
|
|
93
|
-
throw createError({
|
|
94
|
-
code: "AUTH_CONFIG_INVALID",
|
|
95
|
-
message: `${name} must be a non-empty string.`,
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
return input;
|
|
99
|
-
};
|
|
100
|
-
const resolveConfig = (config) => {
|
|
101
|
-
if (!isRecord(config)) {
|
|
102
|
-
throw createError({
|
|
103
|
-
code: "AUTH_CONFIG_INVALID",
|
|
104
|
-
message: "Prisma configuration is required.",
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
if (config.session !== undefined && !isRecord(config.session)) {
|
|
108
|
-
throw createError({
|
|
109
|
-
code: "AUTH_CONFIG_INVALID",
|
|
110
|
-
message: "Prisma session configuration is invalid.",
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
const session = isRecord(config.session) ? config.session : {};
|
|
114
|
-
if (session.relations !== undefined && !isRecord(session.relations)) {
|
|
115
|
-
throw createError({
|
|
116
|
-
code: "AUTH_CONFIG_INVALID",
|
|
117
|
-
message: "Prisma session relations are invalid.",
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
const relations = isRecord(session.relations) ? session.relations : {};
|
|
121
|
-
return {
|
|
122
|
-
access: resolveAccess(config),
|
|
123
|
-
relations: {
|
|
124
|
-
account: relations.account === undefined
|
|
125
|
-
? "account"
|
|
126
|
-
: resolveName({
|
|
127
|
-
input: relations.account,
|
|
128
|
-
name: "session.relations.account",
|
|
129
|
-
}),
|
|
130
|
-
user: relations.user === undefined
|
|
131
|
-
? "user"
|
|
132
|
-
: resolveName({ input: relations.user, name: "session.relations.user" }),
|
|
133
|
-
},
|
|
134
|
-
table: session.table === undefined
|
|
135
|
-
? DEFAULT_TABLE
|
|
136
|
-
: resolveName({ input: session.table, name: "session.table" }),
|
|
137
|
-
};
|
|
138
|
-
};
|
|
139
|
-
const matchesAccess = (value, rule) => {
|
|
140
|
-
return rule.statuses.includes(value.status) && (!rule.roles || rule.roles.includes(value.role));
|
|
141
|
-
};
|
|
142
|
-
const createAuthSelect = (relations) => ({
|
|
143
|
-
...select,
|
|
144
|
-
[relations.account]: {
|
|
145
|
-
select: {
|
|
146
|
-
email: true,
|
|
147
|
-
id: true,
|
|
148
|
-
name: true,
|
|
149
|
-
role: true,
|
|
150
|
-
status: true,
|
|
151
|
-
timezone: true,
|
|
152
|
-
[relations.user]: {
|
|
153
|
-
select: {
|
|
154
|
-
id: true,
|
|
155
|
-
role: true,
|
|
156
|
-
status: true,
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
},
|
|
160
|
-
},
|
|
161
|
-
});
|
|
162
|
-
const requireRow = (value) => {
|
|
163
|
-
if (!isSessionRecord(value)) {
|
|
164
|
-
throw new Error("Prisma session table returned invalid data.");
|
|
165
|
-
}
|
|
166
|
-
return {
|
|
167
|
-
account_id: value.account_id,
|
|
168
|
-
agent: value.agent,
|
|
169
|
-
created_at: value.created_at,
|
|
170
|
-
expires_at: value.expires_at,
|
|
171
|
-
id: value.id,
|
|
172
|
-
ip: value.ip,
|
|
173
|
-
platform: value.platform,
|
|
174
|
-
revoked_at: value.revoked_at,
|
|
175
|
-
token_hash: value.token_hash,
|
|
176
|
-
updated_at: value.updated_at,
|
|
177
|
-
};
|
|
178
|
-
};
|
|
179
|
-
const requireRows = (value) => {
|
|
180
|
-
if (!Array.isArray(value)) {
|
|
181
|
-
throw new Error("Prisma session table returned invalid data.");
|
|
182
|
-
}
|
|
183
|
-
return value.map(requireRow);
|
|
184
|
-
};
|
|
185
|
-
const requireAuthRow = ({ access, relations, value }) => {
|
|
186
|
-
const row = requireRow(value);
|
|
187
|
-
if (!isRecord(value)) {
|
|
188
|
-
throw new Error("Prisma account relation returned invalid data.");
|
|
189
|
-
}
|
|
190
|
-
const relation = value[relations.account];
|
|
191
|
-
if (!isRecord(relation)) {
|
|
192
|
-
throw new Error("Prisma account relation returned invalid data.");
|
|
193
|
-
}
|
|
194
|
-
const account = {
|
|
195
|
-
email: relation.email,
|
|
196
|
-
id: relation.id,
|
|
197
|
-
name: relation.name,
|
|
198
|
-
role: relation.role,
|
|
199
|
-
status: relation.status,
|
|
200
|
-
timezone: relation.timezone,
|
|
201
|
-
user: relation[relations.user],
|
|
202
|
-
};
|
|
203
|
-
if (!isAuthAccount(account)) {
|
|
204
|
-
throw new Error("Prisma account relation returned invalid data.");
|
|
205
|
-
}
|
|
206
|
-
return {
|
|
207
|
-
...row,
|
|
208
|
-
account,
|
|
209
|
-
allowed: matchesAccess(account, access.account) && matchesAccess(account.user, access.user),
|
|
210
|
-
};
|
|
211
|
-
};
|
|
2
|
+
import { isRecord } from "../../session/guards.js";
|
|
3
|
+
import { isPrismaDelegate, resolvePrismaModels } from "./config.js";
|
|
4
|
+
import { createAuthSelect, requireAuthRow, requireRow, requireRows, sessionSelect, } from "./model.js";
|
|
212
5
|
export const createPrismaAdapter = (input) => {
|
|
213
|
-
const
|
|
214
|
-
const resolved =
|
|
215
|
-
const
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
if (!isDelegate(table)) {
|
|
6
|
+
const raw = input;
|
|
7
|
+
const resolved = resolvePrismaModels({ client: raw.client, input: raw.models });
|
|
8
|
+
const table = isRecord(raw.client) ? raw.client[resolved.sessions] : undefined;
|
|
9
|
+
const authSelect = createAuthSelect(resolved.account);
|
|
10
|
+
if (!isPrismaDelegate(table)) {
|
|
219
11
|
throw createError({
|
|
220
12
|
code: "AUTH_CONFIG_INVALID",
|
|
221
|
-
message: `Prisma session
|
|
13
|
+
message: `Prisma session model ${resolved.sessions} is invalid.`,
|
|
222
14
|
});
|
|
223
15
|
}
|
|
224
16
|
return {
|
|
@@ -227,7 +19,7 @@ export const createPrismaAdapter = (input) => {
|
|
|
227
19
|
},
|
|
228
20
|
find: async ({ account_id, session_id }) => {
|
|
229
21
|
const row = await table.findFirst({
|
|
230
|
-
select,
|
|
22
|
+
select: sessionSelect,
|
|
231
23
|
where: {
|
|
232
24
|
account_id,
|
|
233
25
|
id: session_id,
|
|
@@ -238,7 +30,7 @@ export const createPrismaAdapter = (input) => {
|
|
|
238
30
|
findActive: async ({ account_id, now }) => {
|
|
239
31
|
const rows = await table.findMany({
|
|
240
32
|
orderBy: { created_at: "desc" },
|
|
241
|
-
select,
|
|
33
|
+
select: sessionSelect,
|
|
242
34
|
where: {
|
|
243
35
|
account_id,
|
|
244
36
|
expires_at: { gt: now },
|
|
@@ -255,8 +47,7 @@ export const createPrismaAdapter = (input) => {
|
|
|
255
47
|
return row === null
|
|
256
48
|
? null
|
|
257
49
|
: requireAuthRow({
|
|
258
|
-
|
|
259
|
-
relations: resolved.relations,
|
|
50
|
+
account: resolved.account,
|
|
260
51
|
value: row,
|
|
261
52
|
});
|
|
262
53
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAEnD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACpE,OAAO,EACH,gBAAgB,EAChB,cAAc,EACd,UAAU,EACV,WAAW,EACX,aAAa,GAChB,MAAM,YAAY,CAAC;AAcpB,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAI/B,KAAyC,EACjB,EAAE;IAC1B,MAAM,GAAG,GAAG,KAA6C,CAAC;IAC1D,MAAM,QAAQ,GAAG,mBAAmB,C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uthSessionRecord, SessionRecord } from "../../session/types.js";
|
|
2
|
+
import type { ResolvedPrismaModel } from "./types.js";
|
|
3
|
+
export declare const sessionSelect: {
|
|
4
|
+
readonly account_id: true;
|
|
5
|
+
readonly agent: true;
|
|
6
|
+
readonly created_at: true;
|
|
7
|
+
readonly expires_at: true;
|
|
8
|
+
readonly id: true;
|
|
9
|
+
readonly ip: true;
|
|
10
|
+
readonly platform: true;
|
|
11
|
+
readonly revoked_at: true;
|
|
12
|
+
readonly token_hash: true;
|
|
13
|
+
readonly updated_at: true;
|
|
14
|
+
};
|
|
15
|
+
export declare const createAuthSelect: (account: ResolvedPrismaModel) => {
|
|
16
|
+
account_id: true;
|
|
17
|
+
agent: true;
|
|
18
|
+
created_at: true;
|
|
19
|
+
expires_at: true;
|
|
20
|
+
id: true;
|
|
21
|
+
ip: true;
|
|
22
|
+
platform: true;
|
|
23
|
+
revoked_at: true;
|
|
24
|
+
token_hash: true;
|
|
25
|
+
updated_at: true;
|
|
26
|
+
};
|
|
27
|
+
export declare const requireRow: (value: unknown) => SessionRecord;
|
|
28
|
+
export declare const requireRows: (value: unknown) => SessionRecord[];
|
|
29
|
+
export declare const requireAuthRow: ({ account, value, }: {
|
|
30
|
+
account: ResolvedPrismaModel;
|
|
31
|
+
value: unknown;
|
|
32
|
+
}) => AuthSessionRecord;
|
|
33
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/model.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAER,iBAAiB,EAEjB,aAAa,EAChB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAqB,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAOzE,eAAO,MAAM,aAAa;;;;;;;;;;;CAWhB,CAAC;AAmCX,eAAO,MAAM,gBAAgB,GAAI,SAAS,mBAAmB;;;;;;;;;;;CAK3D,CAAC;AAuDH,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,aAiB3C,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,KAAG,aAAa,EAMzD,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,qBAG5B;IACC,OAAO,EAAE,mBAAmB,CAAC;IAC7B,KAAK,EAAE,OAAO,CAAC;CAClB,KAAG,iBAcH,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { isAuthValue, isNullableString, isRecord } from "../../session/guards.js";
|
|
2
|
+
export const sessionSelect = {
|
|
3
|
+
account_id: true,
|
|
4
|
+
agent: true,
|
|
5
|
+
created_at: true,
|
|
6
|
+
expires_at: true,
|
|
7
|
+
id: true,
|
|
8
|
+
ip: true,
|
|
9
|
+
platform: true,
|
|
10
|
+
revoked_at: true,
|
|
11
|
+
token_hash: true,
|
|
12
|
+
updated_at: true,
|
|
13
|
+
};
|
|
14
|
+
const isNullableDate = (value) => {
|
|
15
|
+
return value instanceof Date || value === null;
|
|
16
|
+
};
|
|
17
|
+
const isSessionRecord = (value) => {
|
|
18
|
+
return (isRecord(value) &&
|
|
19
|
+
typeof value.account_id === "string" &&
|
|
20
|
+
isNullableString(value.agent) &&
|
|
21
|
+
value.created_at instanceof Date &&
|
|
22
|
+
value.expires_at instanceof Date &&
|
|
23
|
+
typeof value.id === "string" &&
|
|
24
|
+
isNullableString(value.ip) &&
|
|
25
|
+
isNullableString(value.platform) &&
|
|
26
|
+
isNullableDate(value.revoked_at) &&
|
|
27
|
+
typeof value.token_hash === "string" &&
|
|
28
|
+
isNullableDate(value.updated_at));
|
|
29
|
+
};
|
|
30
|
+
const createModelSelect = (model) => {
|
|
31
|
+
const fields = new Set([...model.select, ...Object.keys(model.access)]);
|
|
32
|
+
const select = Object.fromEntries([...fields].map((field) => [field, true]));
|
|
33
|
+
for (const relation of Object.values(model.relations)) {
|
|
34
|
+
select[relation.relation] = { select: createModelSelect(relation) };
|
|
35
|
+
}
|
|
36
|
+
return select;
|
|
37
|
+
};
|
|
38
|
+
export const createAuthSelect = (account) => ({
|
|
39
|
+
...sessionSelect,
|
|
40
|
+
[account.relation]: {
|
|
41
|
+
select: createModelSelect(account),
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
const matchesAccess = ({ access, value, }) => {
|
|
45
|
+
return Object.entries(access).every(([field, expected]) => {
|
|
46
|
+
const current = value[field];
|
|
47
|
+
return Array.isArray(expected)
|
|
48
|
+
? expected.some((allowed) => Object.is(allowed, current))
|
|
49
|
+
: Object.is(expected, current);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const readModel = ({ model, value, }) => {
|
|
53
|
+
if (!isRecord(value)) {
|
|
54
|
+
throw new Error(`Prisma relation ${model.relation} returned invalid data.`);
|
|
55
|
+
}
|
|
56
|
+
const data = {};
|
|
57
|
+
for (const field of model.select) {
|
|
58
|
+
const fieldValue = value[field];
|
|
59
|
+
if (!isAuthValue(fieldValue) || isRecord(fieldValue) || Array.isArray(fieldValue)) {
|
|
60
|
+
throw new Error(`Prisma field ${model.name}.${field} returned invalid payload data.`);
|
|
61
|
+
}
|
|
62
|
+
data[field] = fieldValue;
|
|
63
|
+
}
|
|
64
|
+
if (typeof data.id !== "string") {
|
|
65
|
+
throw new Error(`Prisma model ${model.name} must return a string id.`);
|
|
66
|
+
}
|
|
67
|
+
let allowed = matchesAccess({ access: model.access, value });
|
|
68
|
+
for (const relation of Object.values(model.relations)) {
|
|
69
|
+
const nested = readModel({ model: relation, value: value[relation.relation] });
|
|
70
|
+
data[relation.relation] = nested.data;
|
|
71
|
+
allowed &&= nested.allowed;
|
|
72
|
+
}
|
|
73
|
+
return { allowed, data: data };
|
|
74
|
+
};
|
|
75
|
+
export const requireRow = (value) => {
|
|
76
|
+
if (!isSessionRecord(value)) {
|
|
77
|
+
throw new Error("Prisma session table returned invalid data.");
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
account_id: value.account_id,
|
|
81
|
+
agent: value.agent,
|
|
82
|
+
created_at: value.created_at,
|
|
83
|
+
expires_at: value.expires_at,
|
|
84
|
+
id: value.id,
|
|
85
|
+
ip: value.ip,
|
|
86
|
+
platform: value.platform,
|
|
87
|
+
revoked_at: value.revoked_at,
|
|
88
|
+
token_hash: value.token_hash,
|
|
89
|
+
updated_at: value.updated_at,
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
export const requireRows = (value) => {
|
|
93
|
+
if (!Array.isArray(value)) {
|
|
94
|
+
throw new Error("Prisma session table returned invalid data.");
|
|
95
|
+
}
|
|
96
|
+
return value.map(requireRow);
|
|
97
|
+
};
|
|
98
|
+
export const requireAuthRow = ({ account, value, }) => {
|
|
99
|
+
const row = requireRow(value);
|
|
100
|
+
if (!isRecord(value)) {
|
|
101
|
+
throw new Error("Prisma account relation returned invalid data.");
|
|
102
|
+
}
|
|
103
|
+
const resolved = readModel({ model: account, value: value[account.relation] });
|
|
104
|
+
return {
|
|
105
|
+
...row,
|
|
106
|
+
account: resolved.data,
|
|
107
|
+
allowed: resolved.allowed,
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
//# sourceMappingURL=model.js.map
|