-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
85 lines (70 loc) · 2.01 KB
/
model.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
/**
* Module dependencies.
*/
var jwt = require('jsonwebtoken');
var tokenUtil = require('oauth2-server/lib/utils/token-util');
const db = require('./database/databaseHelper')
const SCOPES = ['read', 'write'];
// TODO: Generate the scope depending on the user demand
module.exports.generateAccessToken = function (client, user, scope) {
return jwt.sign({
userId: user.id,
clientId: client.id,
scopes: SCOPES,
iss: process.env.URL,
refreshToken: tokenUtil.generateRandomToken(),
refreshTokenExpiresAt: Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 14) // 2 semaines
}, process.env.secretKey, { expiresIn: 60 * 30 }); // 30 minutes
}
/**
* Get access token.
*/
module.exports.getAccessToken = function (bearerToken) {
return db.getAccessToken(bearerToken)
};
/**
* Get client.
*/
module.exports.getClient = function (clientId, clientSecret) {
if (clientSecret == null)
return db.getClientWithId(clientId);
else
return db.getClientWithIdAndSecret(clientId, clientSecret);
};
/**
* Get refresh token.
*/
module.exports.getRefreshToken = function (refreshToken) {
return db.getRefreshToken(refreshToken);
};
/**
* Get user.
*/
module.exports.getUser = function (username, password) {
return db.getUser(username, password);
};
/**
* Get Authorization Code.
*/
module.exports.getAuthorizationCode = function (authorization_code) {
return db.getAuthorizationCode(authorization_code);
};
/**
* Save the Authorization Code
*
*/
module.exports.saveAuthorizationCode = function (authorizationCode, client, user) {
return db.saveAuthorizationCode(authorizationCode, client, user);
};
/**
* Save token.
*/
module.exports.saveToken = function (token, client, user) {
return db.saveToken(token, client, user);
};
module.exports.revokeAuthorizationCode = function (authorization_code) {
return db.revokeAuthorizationCode(authorization_code);
}
module.exports.revokeToken = function (token) {
return db.revokeToken(token);
}