diff options
Diffstat (limited to 'server/models/oauth-client.js')
-rw-r--r-- | server/models/oauth-client.js | 75 |
1 files changed, 52 insertions, 23 deletions
diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js index a1aefa985..021a34007 100644 --- a/server/models/oauth-client.js +++ b/server/models/oauth-client.js | |||
@@ -1,33 +1,62 @@ | |||
1 | const mongoose = require('mongoose') | 1 | 'use strict' |
2 | 2 | ||
3 | // --------------------------------------------------------------------------- | 3 | module.exports = function (sequelize, DataTypes) { |
4 | 4 | const OAuthClient = sequelize.define('OAuthClient', | |
5 | const OAuthClientSchema = mongoose.Schema({ | 5 | { |
6 | clientSecret: String, | 6 | clientId: { |
7 | grants: Array, | 7 | type: DataTypes.STRING, |
8 | redirectUris: Array | 8 | allowNull: false |
9 | }) | 9 | }, |
10 | 10 | clientSecret: { | |
11 | OAuthClientSchema.path('clientSecret').required(true) | 11 | type: DataTypes.STRING, |
12 | 12 | allowNull: false | |
13 | OAuthClientSchema.statics = { | 13 | }, |
14 | getByIdAndSecret, | 14 | grants: { |
15 | list, | 15 | type: DataTypes.ARRAY(DataTypes.STRING) |
16 | loadFirstClient | 16 | }, |
17 | redirectUris: { | ||
18 | type: DataTypes.ARRAY(DataTypes.STRING) | ||
19 | } | ||
20 | }, | ||
21 | { | ||
22 | indexes: [ | ||
23 | { | ||
24 | fields: [ 'clientId' ], | ||
25 | unique: true | ||
26 | }, | ||
27 | { | ||
28 | fields: [ 'clientId', 'clientSecret' ], | ||
29 | unique: true | ||
30 | } | ||
31 | ], | ||
32 | classMethods: { | ||
33 | countTotal, | ||
34 | getByIdAndSecret, | ||
35 | loadFirstClient | ||
36 | } | ||
37 | } | ||
38 | ) | ||
39 | |||
40 | return OAuthClient | ||
17 | } | 41 | } |
18 | 42 | ||
19 | mongoose.model('OAuthClient', OAuthClientSchema) | ||
20 | |||
21 | // --------------------------------------------------------------------------- | 43 | // --------------------------------------------------------------------------- |
22 | 44 | ||
23 | function list (callback) { | 45 | function countTotal (callback) { |
24 | return this.find(callback) | 46 | return this.count().asCallback(callback) |
25 | } | 47 | } |
26 | 48 | ||
27 | function loadFirstClient (callback) { | 49 | function loadFirstClient (callback) { |
28 | return this.findOne({}, callback) | 50 | return this.findOne().asCallback(callback) |
29 | } | 51 | } |
30 | 52 | ||
31 | function getByIdAndSecret (id, clientSecret) { | 53 | function getByIdAndSecret (clientId, clientSecret) { |
32 | return this.findOne({ _id: id, clientSecret: clientSecret }).exec() | 54 | const query = { |
55 | where: { | ||
56 | clientId: clientId, | ||
57 | clientSecret: clientSecret | ||
58 | } | ||
59 | } | ||
60 | |||
61 | return this.findOne(query) | ||
33 | } | 62 | } |