aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/oauth-model.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/oauth-model.js')
-rw-r--r--server/lib/oauth-model.js97
1 files changed, 0 insertions, 97 deletions
diff --git a/server/lib/oauth-model.js b/server/lib/oauth-model.js
deleted file mode 100644
index 1c12f1b14..000000000
--- a/server/lib/oauth-model.js
+++ /dev/null
@@ -1,97 +0,0 @@
1const db = require('../initializers/database')
2const logger = require('../helpers/logger')
3
4// See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications
5const OAuthModel = {
6 getAccessToken,
7 getClient,
8 getRefreshToken,
9 getUser,
10 revokeToken,
11 saveToken
12}
13
14// ---------------------------------------------------------------------------
15
16function getAccessToken (bearerToken) {
17 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
18
19 return db.OAuthToken.getByTokenAndPopulateUser(bearerToken)
20}
21
22function getClient (clientId, clientSecret) {
23 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
24
25 return db.OAuthClient.getByIdAndSecret(clientId, clientSecret)
26}
27
28function getRefreshToken (refreshToken) {
29 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
30
31 return db.OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken)
32}
33
34function getUser (username, password) {
35 logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
36
37 return db.User.getByUsername(username).then(function (user) {
38 if (!user) return null
39
40 // We need to return a promise
41 return new Promise(function (resolve, reject) {
42 return user.isPasswordMatch(password, function (err, isPasswordMatch) {
43 if (err) return reject(err)
44
45 if (isPasswordMatch === true) {
46 return resolve(user)
47 }
48
49 return resolve(null)
50 })
51 })
52 })
53}
54
55function revokeToken (token) {
56 return db.OAuthToken.getByRefreshTokenAndPopulateUser(token.refreshToken).then(function (tokenDB) {
57 if (tokenDB) tokenDB.destroy()
58
59 /*
60 * Thanks to https://github.com/manjeshpv/node-oauth2-server-implementation/blob/master/components/oauth/mongo-models.js
61 * "As per the discussion we need set older date
62 * revokeToken will expected return a boolean in future version
63 * https://github.com/oauthjs/node-oauth2-server/pull/274
64 * https://github.com/oauthjs/node-oauth2-server/issues/290"
65 */
66 const expiredToken = tokenDB
67 expiredToken.refreshTokenExpiresAt = new Date('2015-05-28T06:59:53.000Z')
68
69 return expiredToken
70 })
71}
72
73function saveToken (token, client, user) {
74 logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.')
75
76 const tokenToCreate = {
77 accessToken: token.accessToken,
78 accessTokenExpiresAt: token.accessTokenExpiresAt,
79 refreshToken: token.refreshToken,
80 refreshTokenExpiresAt: token.refreshTokenExpiresAt,
81 oAuthClientId: client.id,
82 userId: user.id
83 }
84
85 return db.OAuthToken.create(tokenToCreate).then(function (tokenCreated) {
86 tokenCreated.client = client
87 tokenCreated.user = user
88
89 return tokenCreated
90 }).catch(function (err) {
91 throw err
92 })
93}
94
95// ---------------------------------------------------------------------------
96
97module.exports = OAuthModel