forked from inventures/hatchjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessToken.js
96 lines (85 loc) · 3.39 KB
/
AccessToken.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
//
// Hatch.js is a CMS and social website building framework built in Node.js
// Copyright (C) 2013 Inventures Software Ltd
//
// This file is part of Hatch.js
//
// Hatch.js is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation, version 3
//
// Hatch.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Affero General Public License for more details. You should have received a copy of the GNU
// General Public License along with Hatch.js. If not, see <http://www.gnu.org/licenses/>.
//
// Authors: Marcus Greenwood, Anatoliy Chakkaev and others
//
'use strict';
module.exports = function (compound, AccessToken) {
var User = compound.models.User;
/**
* Load the auth token from the current request object and then load the
* User attached to this token.
*
* @param {String} token - string representing access token.
* @param {Function} callback - called with (err, user).
*/
AccessToken.loadUser = function (token, callback) {
// if there is no token present, just continue
if (!token) {
callback(new Error('No access token given'));
}
AccessToken.findOne({where: {token: token }}, function (err, accessToken) {
if (err) {
callback(err);
} else if (accessToken !== null && accessToken.userId !== undefined) {
User.find(accessToken.userId, function (err, user) {
if (err) {
return callback(err);
}
// validate the token
if (!user) {
console.log('User in access token ' + accessToken.id + ' not found');
return callback();
}
if (!accessToken.isTokenValid()) {
console.log('Access token ' + accessToken.id + ' was invalid');
return callback();
}
callback(null, user);
});
}
else {
callback();
}
});
};
/**
* Returns whether this token is still valid for use.
*
* @return {Boolean}
*/
AccessToken.prototype.isTokenValid = function () {
return !this.expiryDate || this.expiryDate > new Date();
};
/**
* Generate a new token for the specified user/client and save to the
* database and return via callback.
*
* @param {Number} userId - Id of the user
* @param {Number} clientId - Id of the client application
* @param {Object} scope - scope of access
* @param {Date} expiryDate - expiry date for this token (optional)
* @param {Function} callback - callback function
*/
AccessToken.generateToken = function (userId, clientId, scope, expiryDate, callback) {
var token = new AccessToken();
token.userId = userId;
token.clientId = clientId;
token.scope = JSON.parse(scope || '{}');
token.expiryDate = expiryDate;
token.token = compound.hatch.crypto.generateRandomString(256);
token.save(callback);
};
};