diff options
Diffstat (limited to 'server/models/oauth/oauth-client.ts')
-rw-r--r-- | server/models/oauth/oauth-client.ts | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/server/models/oauth/oauth-client.ts b/server/models/oauth/oauth-client.ts new file mode 100644 index 000000000..fbc2a3393 --- /dev/null +++ b/server/models/oauth/oauth-client.ts | |||
@@ -0,0 +1,87 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | import { addMethodsToModel } from '../utils' | ||
4 | import { | ||
5 | OAuthClientClass, | ||
6 | OAuthClientInstance, | ||
7 | OAuthClientAttributes, | ||
8 | |||
9 | OAuthClientMethods | ||
10 | } from './oauth-client-interface' | ||
11 | |||
12 | let OAuthClient: Sequelize.Model<OAuthClientInstance, OAuthClientAttributes> | ||
13 | let countTotal: OAuthClientMethods.CountTotal | ||
14 | let loadFirstClient: OAuthClientMethods.LoadFirstClient | ||
15 | let getByIdAndSecret: OAuthClientMethods.GetByIdAndSecret | ||
16 | |||
17 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
18 | OAuthClient = sequelize.define<OAuthClientInstance, OAuthClientAttributes>('OAuthClient', | ||
19 | { | ||
20 | clientId: { | ||
21 | type: DataTypes.STRING, | ||
22 | allowNull: false | ||
23 | }, | ||
24 | clientSecret: { | ||
25 | type: DataTypes.STRING, | ||
26 | allowNull: false | ||
27 | }, | ||
28 | grants: { | ||
29 | type: DataTypes.ARRAY(DataTypes.STRING) | ||
30 | }, | ||
31 | redirectUris: { | ||
32 | type: DataTypes.ARRAY(DataTypes.STRING) | ||
33 | } | ||
34 | }, | ||
35 | { | ||
36 | indexes: [ | ||
37 | { | ||
38 | fields: [ 'clientId' ], | ||
39 | unique: true | ||
40 | }, | ||
41 | { | ||
42 | fields: [ 'clientId', 'clientSecret' ], | ||
43 | unique: true | ||
44 | } | ||
45 | ] | ||
46 | } | ||
47 | ) | ||
48 | |||
49 | const classMethods = [ | ||
50 | associate, | ||
51 | |||
52 | countTotal, | ||
53 | getByIdAndSecret, | ||
54 | loadFirstClient | ||
55 | ] | ||
56 | addMethodsToModel(OAuthClient, classMethods) | ||
57 | |||
58 | return OAuthClient | ||
59 | } | ||
60 | |||
61 | // --------------------------------------------------------------------------- | ||
62 | |||
63 | function associate (models) { | ||
64 | OAuthClient.hasMany(models.OAuthToken, { | ||
65 | foreignKey: 'oAuthClientId', | ||
66 | onDelete: 'cascade' | ||
67 | }) | ||
68 | } | ||
69 | |||
70 | countTotal = function (callback: OAuthClientMethods.CountTotalCallback) { | ||
71 | return OAuthClient.count().asCallback(callback) | ||
72 | } | ||
73 | |||
74 | loadFirstClient = function (callback: OAuthClientMethods.LoadFirstClientCallback) { | ||
75 | return OAuthClient.findOne().asCallback(callback) | ||
76 | } | ||
77 | |||
78 | getByIdAndSecret = function (clientId: string, clientSecret: string) { | ||
79 | const query = { | ||
80 | where: { | ||
81 | clientId: clientId, | ||
82 | clientSecret: clientSecret | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return OAuthClient.findOne(query) | ||
87 | } | ||