-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadapter.ts
158 lines (136 loc) · 2.86 KB
/
adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { PrismaClient, OidcModel, Prisma } from "@prisma/client";
import { Adapter, AdapterPayload } from "oidc-provider";
const prisma = new PrismaClient();
const types = [
"Session",
"AccessToken",
"AuthorizationCode",
"RefreshToken",
"DeviceCode",
"ClientCredentials",
"Client",
"InitialAccessToken",
"RegistrationAccessToken",
"Interaction",
"ReplayDetection",
"PushedAuthorizationRequest",
"Grant",
"BackchannelAuthenticationRequest",
].reduce(
(map, name, i) => ({ ...map, [name]: i + 1 }),
{} as Record<string, number>
);
const prepare = (doc: OidcModel) => {
const isPayloadJson =
doc.payload &&
typeof doc.payload === "object" &&
!Array.isArray(doc.payload);
const payload = isPayloadJson ? (doc.payload as Prisma.JsonObject) : {};
return {
...payload,
...(doc.consumedAt ? { consumed: true } : undefined),
};
};
const expiresAt = (expiresIn?: number) =>
expiresIn ? new Date(Date.now() + expiresIn * 1000) : null;
export class PrismaAdapter implements Adapter {
type: number;
constructor(name: string) {
this.type = types[name];
}
async upsert(
id: string,
payload: AdapterPayload,
expiresIn?: number
): Promise<void> {
const data = {
type: this.type,
payload: payload as Prisma.JsonObject,
grantId: payload.grantId,
userCode: payload.userCode,
uid: payload.uid,
expiresAt: expiresAt(expiresIn),
};
await prisma.oidcModel.upsert({
where: {
id_type: {
id,
type: this.type,
},
},
update: {
...data,
},
create: {
id,
...data,
},
});
}
async find(id: string): Promise<AdapterPayload | undefined> {
const doc = await prisma.oidcModel.findUnique({
where: {
id_type: {
id,
type: this.type,
},
},
});
if (!doc || (doc.expiresAt && doc.expiresAt < new Date())) {
return undefined;
}
return prepare(doc);
}
async findByUserCode(userCode: string): Promise<AdapterPayload | undefined> {
const doc = await prisma.oidcModel.findFirst({
where: {
userCode,
},
});
if (!doc || (doc.expiresAt && doc.expiresAt < new Date())) {
return undefined;
}
return prepare(doc);
}
async findByUid(uid: string): Promise<AdapterPayload | undefined> {
const doc = await prisma.oidcModel.findUnique({
where: {
uid,
},
});
if (!doc || (doc.expiresAt && doc.expiresAt < new Date())) {
return undefined;
}
return prepare(doc);
}
async consume(id: string): Promise<void> {
await prisma.oidcModel.update({
where: {
id_type: {
id,
type: this.type,
},
},
data: {
consumedAt: new Date(),
},
});
}
async destroy(id: string): Promise<void> {
await prisma.oidcModel.delete({
where: {
id_type: {
id,
type: this.type,
},
},
});
}
async revokeByGrantId(grantId: string): Promise<void> {
await prisma.oidcModel.deleteMany({
where: {
grantId,
},
});
}
}