aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth-token.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-20 16:23:58 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-20 16:23:58 +0200
commit2f372a865487427ff97ad17edd0e6adfbb478c80 (patch)
tree84e8b2ba2240e83dcdeeb057579327b0a663974b /server/models/oauth-token.js
parent66698b833f77806045b36fdeba612acda43dd7f7 (diff)
downloadPeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.tar.gz
PeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.tar.zst
PeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.zip
Server: implement refresh token
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}