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.js134
1 files changed, 108 insertions, 26 deletions
diff --git a/server/models/oauth-token.js b/server/models/oauth-token.js
index aff73bfb1..68e7c9ff7 100644
--- a/server/models/oauth-token.js
+++ b/server/models/oauth-token.js
@@ -1,42 +1,96 @@
1const mongoose = require('mongoose') 1'use strict'
2 2
3const logger = require('../helpers/logger') 3const logger = require('../helpers/logger')
4 4
5// --------------------------------------------------------------------------- 5// ---------------------------------------------------------------------------
6 6
7const OAuthTokenSchema = mongoose.Schema({ 7module.exports = function (sequelize, DataTypes) {
8 accessToken: String, 8 const OAuthToken = sequelize.define('OAuthToken',
9 accessTokenExpiresAt: Date, 9 {
10 client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' }, 10 accessToken: {
11 refreshToken: String, 11 type: DataTypes.STRING,
12 refreshTokenExpiresAt: Date, 12 allowNull: false
13 user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } 13 },
14}) 14 accessTokenExpiresAt: {
15 15 type: DataTypes.DATE,
16OAuthTokenSchema.path('accessToken').required(true) 16 allowNull: false
17OAuthTokenSchema.path('client').required(true) 17 },
18OAuthTokenSchema.path('user').required(true) 18 refreshToken: {
19 19 type: DataTypes.STRING,
20OAuthTokenSchema.statics = { 20 allowNull: false
21 getByRefreshTokenAndPopulateClient, 21 },
22 getByTokenAndPopulateUser, 22 refreshTokenExpiresAt: {
23 getByRefreshTokenAndPopulateUser, 23 type: DataTypes.DATE,
24 removeByUserId 24 allowNull: false
25} 25 }
26 },
27 {
28 indexes: [
29 {
30 fields: [ 'refreshToken' ],
31 unique: true
32 },
33 {
34 fields: [ 'accessToken' ],
35 unique: true
36 },
37 {
38 fields: [ 'userId' ]
39 },
40 {
41 fields: [ 'oAuthClientId' ]
42 }
43 ],
44 classMethods: {
45 associate,
46
47 getByRefreshTokenAndPopulateClient,
48 getByTokenAndPopulateUser,
49 getByRefreshTokenAndPopulateUser,
50 removeByUserId
51 }
52 }
53 )
26 54
27mongoose.model('OAuthToken', OAuthTokenSchema) 55 return OAuthToken
56}
28 57
29// --------------------------------------------------------------------------- 58// ---------------------------------------------------------------------------
30 59
60function associate (models) {
61 this.belongsTo(models.User, {
62 foreignKey: {
63 name: 'userId',
64 allowNull: false
65 },
66 onDelete: 'cascade'
67 })
68
69 this.belongsTo(models.OAuthClient, {
70 foreignKey: {
71 name: 'oAuthClientId',
72 allowNull: false
73 },
74 onDelete: 'cascade'
75 })
76}
77
31function getByRefreshTokenAndPopulateClient (refreshToken) { 78function getByRefreshTokenAndPopulateClient (refreshToken) {
32 return this.findOne({ refreshToken: refreshToken }).populate('client').exec().then(function (token) { 79 const query = {
80 where: {
81 refreshToken: refreshToken
82 },
83 include: [ this.associations.OAuthClient ]
84 }
85
86 return this.findOne(query).then(function (token) {
33 if (!token) return token 87 if (!token) return token
34 88
35 const tokenInfos = { 89 const tokenInfos = {
36 refreshToken: token.refreshToken, 90 refreshToken: token.refreshToken,
37 refreshTokenExpiresAt: token.refreshTokenExpiresAt, 91 refreshTokenExpiresAt: token.refreshTokenExpiresAt,
38 client: { 92 client: {
39 id: token.client._id.toString() 93 id: token.client.id
40 }, 94 },
41 user: { 95 user: {
42 id: token.user 96 id: token.user
@@ -50,13 +104,41 @@ function getByRefreshTokenAndPopulateClient (refreshToken) {
50} 104}
51 105
52function getByTokenAndPopulateUser (bearerToken) { 106function getByTokenAndPopulateUser (bearerToken) {
53 return this.findOne({ accessToken: bearerToken }).populate('user').exec() 107 const query = {
108 where: {
109 accessToken: bearerToken
110 },
111 include: [ this.sequelize.models.User ]
112 }
113
114 return this.findOne(query).then(function (token) {
115 if (token) token.user = token.User
116
117 return token
118 })
54} 119}
55 120
56function getByRefreshTokenAndPopulateUser (refreshToken) { 121function getByRefreshTokenAndPopulateUser (refreshToken) {
57 return this.findOne({ refreshToken: refreshToken }).populate('user').exec() 122 const query = {
123 where: {
124 refreshToken: refreshToken
125 },
126 include: [ this.sequelize.models.User ]
127 }
128
129 return this.findOne(query).then(function (token) {
130 token.user = token.User
131
132 return token
133 })
58} 134}
59 135
60function removeByUserId (userId, callback) { 136function removeByUserId (userId, callback) {
61 return this.remove({ user: userId }, callback) 137 const query = {
138 where: {
139 userId: userId
140 }
141 }
142
143 return this.destroy(query).asCallback(callback)
62} 144}