diff options
Diffstat (limited to 'server/models/oauth-token.js')
-rw-r--r-- | server/models/oauth-token.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/server/models/oauth-token.js b/server/models/oauth-token.js new file mode 100644 index 000000000..5da5da417 --- /dev/null +++ b/server/models/oauth-token.js | |||
@@ -0,0 +1,34 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | // --------------------------------------------------------------------------- | ||
4 | |||
5 | const OAuthTokenSchema = mongoose.Schema({ | ||
6 | accessToken: String, | ||
7 | accessTokenExpiresOn: Date, | ||
8 | client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' }, | ||
9 | refreshToken: String, | ||
10 | refreshTokenExpiresOn: Date, | ||
11 | user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } | ||
12 | }) | ||
13 | |||
14 | OAuthTokenSchema.path('accessToken').required(true) | ||
15 | OAuthTokenSchema.path('client').required(true) | ||
16 | OAuthTokenSchema.path('user').required(true) | ||
17 | |||
18 | OAuthTokenSchema.statics = { | ||
19 | loadByRefreshToken: loadByRefreshToken, | ||
20 | loadByTokenAndPopulateUser: loadByTokenAndPopulateUser | ||
21 | } | ||
22 | |||
23 | mongoose.model('OAuthToken', OAuthTokenSchema) | ||
24 | |||
25 | // --------------------------------------------------------------------------- | ||
26 | |||
27 | function loadByRefreshToken (refreshToken, callback) { | ||
28 | return this.findOne({ refreshToken: refreshToken }, callback) | ||
29 | } | ||
30 | |||
31 | function loadByTokenAndPopulateUser (bearerToken, callback) { | ||
32 | // FIXME: allow to use callback | ||
33 | return this.findOne({ accessToken: bearerToken }).populate('user') | ||
34 | } | ||