-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhooks.js
65 lines (53 loc) · 2.48 KB
/
hooks.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";
var _ = require("underscore");
var crypto = require("crypto");
var database = {
clients: {
officialApiClient: { secret: "C0FFEE" },
unofficialClient: { secret: "DECAF" }
},
users: {
AzureDiamond: { password: "hunter2" },
Cthon98: { password: "*********" }
},
tokensToUsernames: {}
};
function generateToken(data) {
var random = Math.floor(Math.random() * 100001);
var timestamp = (new Date()).getTime();
var sha256 = crypto.createHmac("sha256", random + "WOO" + timestamp);
return sha256.update(data).digest("base64");
}
exports.validateClient = function (credentials, req, cb) {
// Call back with `true` to signal that the client is valid, and `false` otherwise.
// Call back with an error if you encounter an internal server error situation while trying to validate.
var isValid = _.has(database.clients, credentials.clientId) &&
database.clients[credentials.clientId].secret === credentials.clientSecret;
cb(null, isValid);
};
exports.grantUserToken = function (credentials, req, cb) {
var isValid = _.has(database.users, credentials.username) &&
database.users[credentials.username].password === credentials.password;
if (isValid) {
// If the user authenticates, generate a token for them and store it so `exports.authenticateToken` below
// can look it up later.
var token = generateToken(credentials.username + ":" + credentials.password);
database.tokensToUsernames[token] = credentials.username;
// Call back with the token so Restify-OAuth2 can pass it on to the client.
return cb(null, token);
}
// Call back with `false` to signal the username/password combination did not authenticate.
// Calling back with an error would be reserved for internal server error situations.
cb(null, false);
};
exports.authenticateToken = function (token, req, cb) {
if (_.has(database.tokensToUsernames, token)) {
// If the token authenticates, set the corresponding property on the request, and call back with `true`.
// The routes can now use these properties to check if the request is authorized and authenticated.
req.username = database.tokensToUsernames[token];
return cb(null, true);
}
// If the token does not authenticate, call back with `false` to signal that.
// Calling back with an error would be reserved for internal server error situations.
cb(null, false);
};