diff options
Diffstat (limited to 'server/models/oauth-token.js')
-rw-r--r-- | server/models/oauth-token.js | 109 |
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 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | const logger = require('../helpers/logger') | 1 | const logger = require('../helpers/logger') |
4 | 2 | ||
5 | // --------------------------------------------------------------------------- | 3 | // --------------------------------------------------------------------------- |
6 | 4 | ||
7 | const OAuthTokenSchema = mongoose.Schema({ | 5 | module.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 | }, | |
16 | OAuthTokenSchema.path('accessToken').required(true) | 14 | refreshToken: { |
17 | OAuthTokenSchema.path('client').required(true) | 15 | type: DataTypes.STRING |
18 | OAuthTokenSchema.path('user').required(true) | 16 | }, |
19 | 17 | refreshTokenExpiresAt: { | |
20 | OAuthTokenSchema.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 | ||
27 | mongoose.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 | ||
43 | function associate (models) { | ||
44 | this.belongsTo(models.User, { | ||
45 | foreignKey: { | ||
46 | name: 'userId', | ||
47 | allowNull: false | ||
48 | }, | ||
49 | onDelete: 'cascade' | ||
50 | }) | ||
51 | } | ||
52 | |||
31 | function getByRefreshTokenAndPopulateClient (refreshToken) { | 53 | function 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 | ||
52 | function getByTokenAndPopulateUser (bearerToken) { | 81 | function 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 | ||
56 | function getByRefreshTokenAndPopulateUser (refreshToken) { | 96 | function 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 | ||
60 | function removeByUserId (userId, callback) { | 111 | function 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 | } |