-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.controller.js
234 lines (214 loc) · 6.64 KB
/
auth.controller.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import passport from 'passport';
import oauth2orize from 'oauth2orize';
import { BasicStrategy } from 'passport-http';
import BearerStrategy from 'passport-http-bearer';
let clientService = {};
let authService = {};
let userService = {};
const server = oauth2orize.createServer();
const TOKEN_LENGTH = 256;
/**
* Instructions for passport to use a basic authentication.
* Requires the user to supply their username and password.
*/
passport.use('basic', new BasicStrategy('basic', (username, password, next) => {
userService.getCredentialsByUsername(username, password)
.then(user => next(null, user))
.catch((err) => {
if (err.status === 404) {
return next(null, false);
}
return next(err);
})
}));
/**
* Instructions for passport to use a basic authentication. Requires the client
* of a user to supply their clientId (not id, this is like a username) and their
* secret to authenticate the client.
*/
passport.use('client-basic', new BasicStrategy('client-basic', (username, password, next) => {
clientService.findOneClientByClientId(username)
.then((client) => {
if (!client || client.secret !== password) {
return next(null, false);
}
return next(null, client);
})
.catch((err) => {
if (err.status === 404) {
return next(null, false);
}
return next(err);
});
}));
/**
* Instructions for passport to use a bearer token authentication. Requires the
* user/client to supply their token in a header for access.
*/
passport.use(new BearerStrategy('bearer', (accessToken, next) => {
authService.findOneTokenByValue(accessToken)
.then((foundToken) => {
userService.getUserByIdOrUsername(foundToken.userId)
.then((user) => {
if (!user) {
return next(null, false);
}
return next(null, user, { scope: '*' });
})
.catch(err => next(err));
})
.catch(err => next(err))
}));
/**
* Registers the function to serialize the client for sessions on the auth server's side
*/
server.serializeClient((client, next) => next(null, client.id), null);
/**
* Registers the the function to deserialize the client for sessions on the auth server's side
*/
server.deserializeClient((id, next) => {
clientService.findOneClientById(id)
.then(client => next(null, client))
.catch(err => next(err));
});
/**
* The server's function to generate new access codes for a client.
*/
server.grant(oauth2orize.grant.code((client, redirectUri, user, ares, next) => {
authService.createCode(client.id, redirectUri, user.id)
.then(code => next(null, code.value))
.catch(err => next(err));
}));
// Exchange authorization codes
/**
* The server's function to exchange an access code for a token. If the access code exists,
* and it's clientId is the same as the client asking, and the redirectURI is the same as the
* one stored, then the access code is deleted and a new token is generated and stored.
*/
server.exchange(oauth2orize.exchange.code((client, code, redirectUri, next) => {
authService.findOneCodeByValue(code)
.then((authCode) => {
if (redirectUri !== authCode.redirectURI) {
return next(null, false);
}
const { userId } = authCode;
authService.deleteCode(code)
.then(() => {
authService.createToken(userId, TOKEN_LENGTH)
.then(newToken => next(null, newToken))
.catch(err => next(err));
})
.catch(err => next(err));
})
.catch(err => next(err));
}));
/**
* Server's password for token exchange. This method will be used if the grant_type is "password".
* The username must first be converted into a userId for the function to operate.
*/
server.exchange(oauth2orize.exchange.password((client, username, password, scope, next) => {
userService.getCredentialsByUsername(username, password)
.then((user) => {
authService.createToken(user.id)
.then(newToken => next(null, newToken))
.catch(err => next(err));
})
.catch(err => next(err));
}));
/**
* Given a username, and password create a bare-bones user and a token for the new user.
*
* @param req the request
* @param res the response
* @param next the next middleware
* @returns {Promise.<void>}
*/
export async function register(req, res, next) {
const user = {};
if (req.body.username) {
user.username = req.body.username;
}
if (req.body.email) {
user.email = req.body.email;
}
try {
const newUser = await userService.createUser(user, req.body.password, false);
const newToken = await authService.createToken(newUser.id);
res.send({ user: newUser, token: newToken });
} catch (err) {
next(err);
}
}
/**
* If the request gets this far, then return success because the token is valid.
*
* @param req the request
* @param res the response
* @returns {Promise.<void>}
*/
export async function checkToken(req, res) {
res.status(200).send();
}
/**
* A simple function used to return the access code to the client
*
* @param req the request
* @param res the response
*/
export function success(req, res) {
res.send({ code: req.query.code });
}
/**
* Returns the server's authorization function
*
* @returns {[null,null]}
*/
export function authorization() {
return [
server.authorization((clientId, redirectUri, next) => {
clientService.findOneClientByClientId(clientId)
.then(client => next(null, client, redirectUri))
.catch((err) => { next(err); })
}),
(req, res) => {
res.send({
transactionID: req.oauth2.transactionID,
user: req.user,
client: req.oauth2.client
});
}
]
}
/**
* The auth server's decision function
* @returns {[null]}
*/
export function decision() {
return [server.decision()];
}
/**
* The auth server's token function
* @returns {[null]}
*/
export function token() {
return [
server.token()
]
}
/**
* Sets the services to be used by the controller
* @param newUserService the UserService
* @param newClientService the ClientService
* @param newAuthService the AuthService
*/
export function setDependencies(newUserService, newClientService, newAuthService) {
userService = newUserService;
clientService = newClientService;
authService = newAuthService;
}
/** Registering the authentication strategies with Passport */
exports.isBasicAuthenticated = passport.authenticate('basic', { session: false });
exports.isClientAuthenticated = passport.authenticate('client-basic', { session: false });
exports.isBearerAuthenticated = passport.authenticate('bearer', { session: false });
exports.isAuthenticated = passport.authenticate(['bearer', 'basic'], { session: false });
/** Done Registering */