diff options
Diffstat (limited to 'server/models/oauth-token.js')
-rw-r--r-- | server/models/oauth-token.js | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/server/models/oauth-token.js b/server/models/oauth-token.js index 5da5da417..23c698732 100644 --- a/server/models/oauth-token.js +++ b/server/models/oauth-token.js | |||
@@ -1,13 +1,15 @@ | |||
1 | const mongoose = require('mongoose') | 1 | const mongoose = require('mongoose') |
2 | 2 | ||
3 | const logger = require('../helpers/logger') | ||
4 | |||
3 | // --------------------------------------------------------------------------- | 5 | // --------------------------------------------------------------------------- |
4 | 6 | ||
5 | const OAuthTokenSchema = mongoose.Schema({ | 7 | const OAuthTokenSchema = mongoose.Schema({ |
6 | accessToken: String, | 8 | accessToken: String, |
7 | accessTokenExpiresOn: Date, | 9 | accessTokenExpiresAt: Date, |
8 | client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' }, | 10 | client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' }, |
9 | refreshToken: String, | 11 | refreshToken: String, |
10 | refreshTokenExpiresOn: Date, | 12 | refreshTokenExpiresAt: Date, |
11 | user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } | 13 | user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } |
12 | }) | 14 | }) |
13 | 15 | ||
@@ -16,19 +18,38 @@ OAuthTokenSchema.path('client').required(true) | |||
16 | OAuthTokenSchema.path('user').required(true) | 18 | OAuthTokenSchema.path('user').required(true) |
17 | 19 | ||
18 | OAuthTokenSchema.statics = { | 20 | OAuthTokenSchema.statics = { |
19 | loadByRefreshToken: loadByRefreshToken, | 21 | getByRefreshTokenAndPopulateClient: getByRefreshTokenAndPopulateClient, |
20 | loadByTokenAndPopulateUser: loadByTokenAndPopulateUser | 22 | getByTokenAndPopulateUser: getByTokenAndPopulateUser, |
23 | getByRefreshToken: getByRefreshToken | ||
21 | } | 24 | } |
22 | 25 | ||
23 | mongoose.model('OAuthToken', OAuthTokenSchema) | 26 | mongoose.model('OAuthToken', OAuthTokenSchema) |
24 | 27 | ||
25 | // --------------------------------------------------------------------------- | 28 | // --------------------------------------------------------------------------- |
26 | 29 | ||
27 | function loadByRefreshToken (refreshToken, callback) { | 30 | function getByRefreshTokenAndPopulateClient (refreshToken) { |
28 | return this.findOne({ refreshToken: refreshToken }, callback) | 31 | return this.findOne({ refreshToken: refreshToken }).populate('client').then(function (token) { |
32 | if (!token) return token | ||
33 | |||
34 | const tokenInfos = { | ||
35 | refreshToken: token.refreshToken, | ||
36 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | ||
37 | client: { | ||
38 | id: token.client._id.toString() | ||
39 | }, | ||
40 | user: token.user | ||
41 | } | ||
42 | |||
43 | return tokenInfos | ||
44 | }).catch(function (err) { | ||
45 | logger.info('getRefreshToken error.', { error: err }) | ||
46 | }) | ||
29 | } | 47 | } |
30 | 48 | ||
31 | function loadByTokenAndPopulateUser (bearerToken, callback) { | 49 | function getByTokenAndPopulateUser (bearerToken) { |
32 | // FIXME: allow to use callback | ||
33 | return this.findOne({ accessToken: bearerToken }).populate('user') | 50 | return this.findOne({ accessToken: bearerToken }).populate('user') |
34 | } | 51 | } |
52 | |||
53 | function getByRefreshToken (refreshToken) { | ||
54 | return this.findOne({ refreshToken: refreshToken }) | ||
55 | } | ||