-
Notifications
You must be signed in to change notification settings - Fork 48
/
userTokens.js
171 lines (158 loc) · 5.2 KB
/
userTokens.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
/**
* api/v1/controllers/userTokens.js
*/
'use strict';
const helper = require('../helpers/nouns/tokens');
const apiErrors = require('../apiErrors');
const cnstnts = require('../constants');
const u = require('../helpers/verbs/utils');
const httpStatus = cnstnts.httpStatus;
function whereClauseForUser(user) {
const whr = {};
whr[cnstnts.SEQ_OR] = [
{
'user.name': {},
},
{
'user.id': {},
},
];
whr[cnstnts.SEQ_OR][0]['user.name'][cnstnts.SEQ_LIKE] = user;
whr[cnstnts.SEQ_OR][1]['user.id'][cnstnts.SEQ_LIKE] = user;
return whr;
} // whereClauseForUser
function whereClauseForUserAndTokenName(user, tokenName) {
const whr = whereClauseForUser(user);
whr.name = {};
whr.name[cnstnts.SEQ_LIKE] = tokenName;
return whr;
} // whereClauseForUserAndTokenName
module.exports = {
/**
* DELETE /users/{key}/tokens/{tokenName}
*
* Deletes the token metadata record and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
deleteUserToken(req, res, next) {
const user = req.swagger.params.key.value;
const tokenName = req.swagger.params.tokenName.value;
const whr = whereClauseForUserAndTokenName(user, tokenName);
helper.model.findOne(whr)
.then((o) => {
if (o) {
return o.destroy();
}
const err = new apiErrors.ResourceNotFoundError();
err.resource = helper.model.name;
err.key = user + ', ' + tokenName;
throw err;
})
.then((o) => res.status(httpStatus.OK)
.json(u.responsify(o, helper, req.method)))
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* GET /users/{key}/tokens/{tokenName}
*
* Retrieves the token metadata record and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
getUserToken(req, res, next) {
const user = req.swagger.params.key.value;
const tokenName = req.swagger.params.tokenName.value;
const whr = whereClauseForUserAndTokenName(user, tokenName);
helper.model.findOne(whr)
.then((o) => {
res.status(httpStatus.OK).json(u.responsify(o, helper, req.method));
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* GET /users/{key}/tokens
*
* Gets zero or more tokens for the specified user and sends them back in the
* response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
getUserTokens(req, res, next) {
const user = req.swagger.params.key.value;
const whr = whereClauseForUser(user);
helper.model.findAll(whr)
.then((o) => {
res.set(cnstnts.COUNT_HEADER_NAME, o.length);
const retval = o.map((row) => u.responsify(row, helper, req.method));
res.status(httpStatus.OK).json(retval);
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* POST /users/{key}tokens/{tokenName}/restore
*
* Restore access for the specified token if access had previously been
* revoked.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
restoreTokenByName(req, res, next) {
const user = req.swagger.params.key.value;
const tokenName = req.swagger.params.tokenName.value;
const whr = whereClauseForUserAndTokenName(user, tokenName);
helper.model.findOne(whr)
.then((o) => {
if (o.isRevoked === '0') {
throw new apiErrors.InvalidTokenActionError();
}
return o.restore();
})
.then((o) => {
const retval = u.responsify(o, helper, req.method);
res.status(httpStatus.OK).json(retval);
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* POST /users/{key}tokens/{tokenName}/revoke
*
* Revoke access for the specified token.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
revokeTokenByName(req, res, next) {
const user = req.swagger.params.key.value;
const tokenName = req.swagger.params.tokenName.value;
const whr = whereClauseForUserAndTokenName(user, tokenName);
helper.model.findOne(whr)
.then((o) => {
if (o.isRevoked > '0') {
throw new apiErrors.InvalidTokenActionError();
}
return o.revoke();
})
.then((o) => {
const retval = u.responsify(o, helper, req.method);
res.status(httpStatus.OK).json(retval);
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
}; // exports