aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
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
parent66698b833f77806045b36fdeba612acda43dd7f7 (diff)
downloadPeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.tar.gz
PeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.tar.zst
PeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.zip
Server: implement refresh token
Diffstat (limited to 'server/models')
-rw-r--r--server/models/oauth-client.js4
-rw-r--r--server/models/oauth-token.js37
-rw-r--r--server/models/user.js8
3 files changed, 35 insertions, 14 deletions
diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js
index 048e5af48..830f68857 100644
--- a/server/models/oauth-client.js
+++ b/server/models/oauth-client.js
@@ -11,8 +11,8 @@ const OAuthClientSchema = mongoose.Schema({
11OAuthClientSchema.path('clientSecret').required(true) 11OAuthClientSchema.path('clientSecret').required(true)
12 12
13OAuthClientSchema.statics = { 13OAuthClientSchema.statics = {
14 getByIdAndSecret: getByIdAndSecret,
14 list: list, 15 list: list,
15 loadByIdAndSecret: loadByIdAndSecret,
16 loadFirstClient: loadFirstClient 16 loadFirstClient: loadFirstClient
17} 17}
18 18
@@ -28,6 +28,6 @@ function loadFirstClient (callback) {
28 return this.findOne({}, callback) 28 return this.findOne({}, callback)
29} 29}
30 30
31function loadByIdAndSecret (id, clientSecret) { 31function getByIdAndSecret (id, clientSecret) {
32 return this.findOne({ _id: id, clientSecret: clientSecret }) 32 return this.findOne({ _id: id, clientSecret: clientSecret })
33} 33}
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}
diff --git a/server/models/user.js b/server/models/user.js
index 130b49b55..14ffecbff 100644
--- a/server/models/user.js
+++ b/server/models/user.js
@@ -11,8 +11,8 @@ UserSchema.path('password').required(true)
11UserSchema.path('username').required(true) 11UserSchema.path('username').required(true)
12 12
13UserSchema.statics = { 13UserSchema.statics = {
14 list: list, 14 getByUsernameAndPassword: getByUsernameAndPassword,
15 loadByUsernameAndPassword: loadByUsernameAndPassword 15 list: list
16} 16}
17 17
18mongoose.model('User', UserSchema) 18mongoose.model('User', UserSchema)
@@ -23,6 +23,6 @@ function list (callback) {
23 return this.find(callback) 23 return this.find(callback)
24} 24}
25 25
26function loadByUsernameAndPassword (username, password, callback) { 26function getByUsernameAndPassword (username, password) {
27 return this.findOne({ username: username, password: password }, callback) 27 return this.findOne({ username: username, password: password })
28} 28}