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