diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-20 16:23:58 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-20 16:23:58 +0200 |
commit | 2f372a865487427ff97ad17edd0e6adfbb478c80 (patch) | |
tree | 84e8b2ba2240e83dcdeeb057579327b0a663974b /server/models | |
parent | 66698b833f77806045b36fdeba612acda43dd7f7 (diff) | |
download | PeerTube-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.js | 4 | ||||
-rw-r--r-- | server/models/oauth-token.js | 37 | ||||
-rw-r--r-- | server/models/user.js | 8 |
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({ | |||
11 | OAuthClientSchema.path('clientSecret').required(true) | 11 | OAuthClientSchema.path('clientSecret').required(true) |
12 | 12 | ||
13 | OAuthClientSchema.statics = { | 13 | OAuthClientSchema.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 | ||
31 | function loadByIdAndSecret (id, clientSecret) { | 31 | function 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 @@ | |||
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 | } | ||
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) | |||
11 | UserSchema.path('username').required(true) | 11 | UserSchema.path('username').required(true) |
12 | 12 | ||
13 | UserSchema.statics = { | 13 | UserSchema.statics = { |
14 | list: list, | 14 | getByUsernameAndPassword: getByUsernameAndPassword, |
15 | loadByUsernameAndPassword: loadByUsernameAndPassword | 15 | list: list |
16 | } | 16 | } |
17 | 17 | ||
18 | mongoose.model('User', UserSchema) | 18 | mongoose.model('User', UserSchema) |
@@ -23,6 +23,6 @@ function list (callback) { | |||
23 | return this.find(callback) | 23 | return this.find(callback) |
24 | } | 24 | } |
25 | 25 | ||
26 | function loadByUsernameAndPassword (username, password, callback) { | 26 | function getByUsernameAndPassword (username, password) { |
27 | return this.findOne({ username: username, password: password }, callback) | 27 | return this.findOne({ username: username, password: password }) |
28 | } | 28 | } |