forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_adapter.js
65 lines (48 loc) · 1.21 KB
/
memory_adapter.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
'use strict';
const LRU = require('lru-cache');
const epochTime = require('../helpers/epoch_time');
const storage = new LRU({});
function grantKeyFor(id) {
return `grant:${id}`;
}
class MemoryAdapter {
constructor(name) {
this.name = name;
}
key(id) {
return `${this.name}:${id}`;
}
destroy(id) {
const key = this.key(id);
const grantId = storage.get(key) && storage.get(key).grantId;
storage.del(key);
if (grantId) {
const grantKey = grantKeyFor(grantId);
storage.get(grantKey).forEach(token => storage.del(token));
}
return Promise.resolve();
}
consume(id) {
storage.get(this.key(id)).consumed = epochTime();
return Promise.resolve();
}
find(id) {
return Promise.resolve(storage.get(this.key(id)));
}
upsert(id, payload, expiresIn) {
const key = this.key(id);
const grantId = payload.grantId;
if (grantId) {
const grantKey = grantKeyFor(grantId);
const grant = storage.get(grantKey);
if (!grant) {
storage.set(grantKey, [key]);
} else {
grant.push(key);
}
}
storage.set(key, payload, expiresIn * 1000);
return Promise.resolve();
}
}
module.exports = MemoryAdapter;