This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongodb.js
120 lines (101 loc) · 2.95 KB
/
mongodb.js
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
const { MongoClient } = require('mongodb'); // eslint-disable-line import/no-unresolved
const { snakeCase } = require('lodash');
let DB;
const grantable = new Set([
'access_token',
'authorization_code',
'refresh_token',
'device_code',
]);
class CollectionSet extends Set {
add(name) {
const nu = this.has(name);
super.add(name);
if (!nu) {
DB.collection(name).createIndexes([
...(grantable.has(name)
? [{
key: { grantId: 1 },
partialFilterExpression: { grantId: { $exists: true } },
background: true,
}] : []),
...(name === 'device_code'
? [{
key: { userCode: 1 },
partialFilterExpression: { userCode: { $exists: true } },
background: true,
unique: true,
}] : []),
{
key: { expiresAt: 1 },
expireAfterSeconds: 0,
background: true,
},
]).catch(console.error); // eslint-disable-line no-console
}
}
}
const collections = new CollectionSet();
class MongoAdapter {
constructor(name) {
this.name = snakeCase(name);
collections.add(this.name);
}
// note: the payload for Session model may contain client_id as keys, make sure you do not use
// dots (".") in your client_id value charset.
async upsert(_id, payload, expiresIn) {
let expiresAt;
if (expiresIn) {
expiresAt = new Date(Date.now() + (expiresIn * 1000));
}
const unset = Object.entries(payload).reduce((acc, [key, value]) => {
if (value === undefined) {
acc[key] = '';
delete payload[key]; // eslint-disable-line no-param-reassign
}
return acc;
}, {});
await this.coll().updateOne({ _id }, {
$set: {
_id,
...payload,
...(expiresAt ? { expiresAt } : undefined),
},
...(Object.keys(unset).length ? { $unset: unset } : undefined),
}, { upsert: true });
}
async find(_id) {
return this.coll().find({ _id }).limit(1).next();
}
async findByUserCode(userCode) {
return this.coll().find({ userCode }).limit(1).next();
}
async destroy(_id) {
const found = await this.coll().findOneAndDelete({ _id });
if (found.value && found.value.grantId) {
const promises = [];
collections.forEach((name) => {
if (grantable.has(name)) {
promises.push(this.coll(name).deleteMany({ grantId: found.value.grantId }));
}
});
await Promise.all(promises);
}
}
async consume(_id) {
await this.coll().findOneAndUpdate({ _id }, { $currentDate: { consumed: true } });
}
coll(name) {
return this.constructor.coll(name || this.name);
}
static coll(name) {
return DB.collection(name);
}
static async connect() {
const connection = await MongoClient.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
});
DB = connection.db(connection.s.options.dbName);
}
}
module.exports = MongoAdapter;