aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth-token.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/oauth-token.js')
-rw-r--r--server/models/oauth-token.js37
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 @@
1const mongoose = require('mongoose') 1const mongoose = require('mongoose')
2 2
3const logger = require('../helpers/logger')
4
3// --------------------------------------------------------------------------- 5// ---------------------------------------------------------------------------
4 6
5const OAuthTokenSchema = mongoose.Schema({ 7const 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)
16OAuthTokenSchema.path('user').required(true) 18OAuthTokenSchema.path('user').required(true)
17 19
18OAuthTokenSchema.statics = { 20OAuthTokenSchema.statics = {
19 loadByRefreshToken: loadByRefreshToken, 21 getByRefreshTokenAndPopulateClient: getByRefreshTokenAndPopulateClient,
20 loadByTokenAndPopulateUser: loadByTokenAndPopulateUser 22 getByTokenAndPopulateUser: getByTokenAndPopulateUser,
23 getByRefreshToken: getByRefreshToken
21} 24}
22 25
23mongoose.model('OAuthToken', OAuthTokenSchema) 26mongoose.model('OAuthToken', OAuthTokenSchema)
24 27
25// --------------------------------------------------------------------------- 28// ---------------------------------------------------------------------------
26 29
27function loadByRefreshToken (refreshToken, callback) { 30function 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
31function loadByTokenAndPopulateUser (bearerToken, callback) { 49function 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
53function getByRefreshToken (refreshToken) {
54 return this.findOne({ refreshToken: refreshToken })
55}