forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.js
More file actions
76 lines (60 loc) · 1.63 KB
/
Copy pathredis.js
File metadata and controls
76 lines (60 loc) · 1.63 KB
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
'use strict';
const Redis = require('ioredis'); // eslint-disable-line import/no-unresolved
const _ = require('lodash');
const client = new Redis(process.env.REDIS_URL, {
keyPrefix: 'oidc:',
});
class RedisAdapter {
constructor(name) {
this.name = name;
}
key(id) {
return `${this.name}:${id}`;
}
grantKey(id) {
return `grant:${id}`;
}
destroy(id) {
const key = this.key(id);
return client.hget(key, 'grantId')
.then((grantId) => client.lrange(this.grantKey(grantId), 0, -1))
.then((tokens) => Promise.all(_.map(tokens, (token) => client.del(token))))
.then(() => client.del(key));
}
consume(id) {
return client.hset(this.key(id), 'consumed', Date.now() / 1000 | 0);
}
find(id) {
return client.hgetall(this.key(id)).then((data) => {
if (_.isEmpty(data)) {
return undefined;
} else if (data.dump !== undefined) {
return JSON.parse(data.dump);
}
return data;
});
}
upsert(id, payload, expiresIn) {
const key = this.key(id);
let toStore = payload;
// Clients are not simple objects where value is always a string
// redis does only allow string values =>
// work around it to keep the adapter interface simple
if (this.name === 'Client') {
toStore = {
dump: JSON.stringify(payload),
};
}
const multi = client.multi();
multi.hmset(key, toStore);
if (expiresIn) {
multi.expire(key, expiresIn);
}
if (toStore.grantId) {
const grantKey = this.grantKey(toStore.grantId);
multi.rpush(grantKey, key);
}
return multi.exec();
}
}
module.exports = RedisAdapter;