aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth-token.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-12-11 21:50:51 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-12-19 21:22:28 +0100
commitfeb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (patch)
tree2abc9fbc9569760e218fd52835850b757344b420 /server/models/oauth-token.js
parent108626609eda75e4ecc0a83a650a4d53c46220e0 (diff)
downloadPeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.gz
PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.zst
PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.zip
First version with PostgreSQL
Diffstat (limited to 'server/models/oauth-token.js')
-rw-r--r--server/models/oauth-token.js109
1 files changed, 83 insertions, 26 deletions
diff --git a/server/models/oauth-token.js b/server/models/oauth-token.js
index aff73bfb1..c9108bf95 100644
--- a/server/models/oauth-token.js
+++ b/server/models/oauth-token.js
@@ -1,42 +1,71 @@
1const mongoose = require('mongoose')
2
3const logger = require('../helpers/logger') 1const logger = require('../helpers/logger')
4 2
5// --------------------------------------------------------------------------- 3// ---------------------------------------------------------------------------
6 4
7const OAuthTokenSchema = mongoose.Schema({ 5module.exports = function (sequelize, DataTypes) {
8 accessToken: String, 6 const OAuthToken = sequelize.define('OAuthToken',
9 accessTokenExpiresAt: Date, 7 {
10 client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' }, 8 accessToken: {
11 refreshToken: String, 9 type: DataTypes.STRING
12 refreshTokenExpiresAt: Date, 10 },
13 user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } 11 accessTokenExpiresAt: {
14}) 12 type: DataTypes.DATE
15 13 },
16OAuthTokenSchema.path('accessToken').required(true) 14 refreshToken: {
17OAuthTokenSchema.path('client').required(true) 15 type: DataTypes.STRING
18OAuthTokenSchema.path('user').required(true) 16 },
19 17 refreshTokenExpiresAt: {
20OAuthTokenSchema.statics = { 18 type: DataTypes.DATE
21 getByRefreshTokenAndPopulateClient, 19 }
22 getByTokenAndPopulateUser, 20 },
23 getByRefreshTokenAndPopulateUser, 21 {
24 removeByUserId 22 classMethods: {
23 associate,
24
25 getByRefreshTokenAndPopulateClient,
26 getByTokenAndPopulateUser,
27 getByRefreshTokenAndPopulateUser,
28 removeByUserId
29 }
30 }
31 )
32
33 return OAuthToken
25} 34}
26 35
27mongoose.model('OAuthToken', OAuthTokenSchema) 36// TODO: validation
37// OAuthTokenSchema.path('accessToken').required(true)
38// OAuthTokenSchema.path('client').required(true)
39// OAuthTokenSchema.path('user').required(true)
28 40
29// --------------------------------------------------------------------------- 41// ---------------------------------------------------------------------------
30 42
43function associate (models) {
44 this.belongsTo(models.User, {
45 foreignKey: {
46 name: 'userId',
47 allowNull: false
48 },
49 onDelete: 'cascade'
50 })
51}
52
31function getByRefreshTokenAndPopulateClient (refreshToken) { 53function getByRefreshTokenAndPopulateClient (refreshToken) {
32 return this.findOne({ refreshToken: refreshToken }).populate('client').exec().then(function (token) { 54 const query = {
55 where: {
56 refreshToken: refreshToken
57 },
58 include: [ this.associations.OAuthClient ]
59 }
60
61 return this.findOne(query).then(function (token) {
33 if (!token) return token 62 if (!token) return token
34 63
35 const tokenInfos = { 64 const tokenInfos = {
36 refreshToken: token.refreshToken, 65 refreshToken: token.refreshToken,
37 refreshTokenExpiresAt: token.refreshTokenExpiresAt, 66 refreshTokenExpiresAt: token.refreshTokenExpiresAt,
38 client: { 67 client: {
39 id: token.client._id.toString() 68 id: token.client.id
40 }, 69 },
41 user: { 70 user: {
42 id: token.user 71 id: token.user
@@ -50,13 +79,41 @@ function getByRefreshTokenAndPopulateClient (refreshToken) {
50} 79}
51 80
52function getByTokenAndPopulateUser (bearerToken) { 81function getByTokenAndPopulateUser (bearerToken) {
53 return this.findOne({ accessToken: bearerToken }).populate('user').exec() 82 const query = {
83 where: {
84 accessToken: bearerToken
85 },
86 include: [ this.sequelize.models.User ]
87 }
88
89 return this.findOne(query).then(function (token) {
90 if (token) token.user = token.User
91
92 return token
93 })
54} 94}
55 95
56function getByRefreshTokenAndPopulateUser (refreshToken) { 96function getByRefreshTokenAndPopulateUser (refreshToken) {
57 return this.findOne({ refreshToken: refreshToken }).populate('user').exec() 97 const query = {
98 where: {
99 refreshToken: refreshToken
100 },
101 include: [ this.sequelize.models.User ]
102 }
103
104 return this.findOne(query).then(function (token) {
105 token.user = token.User
106
107 return token
108 })
58} 109}
59 110
60function removeByUserId (userId, callback) { 111function removeByUserId (userId, callback) {
61 return this.remove({ user: userId }, callback) 112 const query = {
113 where: {
114 userId: userId
115 }
116 }
117
118 return this.destroy(query).asCallback(callback)
62} 119}