This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (122 loc) · 3.75 KB
/
index.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
121
122
123
124
125
126
127
var AWS = require('aws-sdk');
var uniqid = require('uniqid');
var s3 = new AWS.S3();
function get(bucket, id){
return new Promise((resolve, reject) => {
s3.getObject({
Bucket: bucket,
Key: id
}
,(err, data) => {
if(err){
return reject(err);
}
else resolve(JSON.parse(data.Body));
});
})
}
function put(bucket, id, data){
return new Promise((resolve, reject) => {
s3.putObject({
Bucket: bucket,
Key: id,
Body: JSON.stringify(data)
}
,(err) => {
if(err){
return reject(err);
}
else resolve(data);
});
})
}
function remove(bucket, id){
return new Promise((resolve, reject) => {
s3.deleteObject({
Bucket: bucket,
Key: id
}
,(err) => {
if(err){
return reject(err);
}
else resolve();
});
})
}
/**
* @param prefix {String} prefix at the start of the collection.
*
*/
function tokenStore(prefix, bucket){
return{
save: function(refreshToken){
return Promise.resolve(refreshToken)
.then((refreshToken) => {
return put(bucket, `${prefix}/client-user/${refreshToken.userId}-${refreshToken.clientId}`, refreshToken);
})
.then((refreshToken) => {
return put(bucket, `${prefix}/token/${refreshToken.token}`, refreshToken);
});
},
fetchByToken: function(token){
return get(bucket, `${prefix}/token/${token}`);
},
removeByUserIdClientId: function(userId, clientId){
return get(bucket, `${prefix}/token/${userId}-${clientId}`)
.then((refreshToken)=> {
return remove(bucket, `${prefix}/client-user/${refreshToken.userId}-${refreshToken.clientId}`)
.then(()=> {
return remove(bucket, `${prefix}/token/${refreshToken.token}`)
})
});
},
removeByRefreshToken: function(token){
return get(bucket, `${prefix}/token/${token}`)
.then((refreshToken)=> {
return remove(bucket, `${prefix}/client-user/${refreshToken.userId}-${refreshToken.clientId}`)
.then(()=> {
return remove(bucket, `${prefix}/token/${refreshToken.token}`)
})
});
}
}
}
module.exports = function(options){
const bucket = options.bucket;
return {
clientStore: {
createClient: (client) => {
return put(bucket, `client/${client.id}`, client)
},
fetchById: (clientId) => {
return get(bucket, `client/${clientId}`);
}
},
codeStore: {
save: (oauth2Code) => {
return put(bucket, `code/${oauth2Code.code}`, oauth2Code);
},
fetchByCode: (code) => {
return get(bucket, `code/${code}`);
},
removeByCode: (code) => {
return remove(bucket, `code/${code}`);
}
},
refreshTokenStore: tokenStore('refresh-token', bucket),
tokenStore: tokenStore('token', bucket),
userStore: {
save: (user) => {
if(!user._id){
user._id = uniqid()
}
console.log(user._id);
return put(bucket, `user/${user._id}`, user)
},
fetchById: (userId) => {
return get(bucket, `user/${userId}`);
}
}
}
}