oauth-client.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { AllowNull, Column, CreatedAt, DataType, HasMany, Model, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { AttributesOnly } from '@shared/typescript-utils'
  3. import { OAuthTokenModel } from './oauth-token'
  4. @Table({
  5. tableName: 'oAuthClient',
  6. indexes: [
  7. {
  8. fields: [ 'clientId' ],
  9. unique: true
  10. },
  11. {
  12. fields: [ 'clientId', 'clientSecret' ],
  13. unique: true
  14. }
  15. ]
  16. })
  17. export class OAuthClientModel extends Model<Partial<AttributesOnly<OAuthClientModel>>> {
  18. @AllowNull(false)
  19. @Column
  20. clientId: string
  21. @AllowNull(false)
  22. @Column
  23. clientSecret: string
  24. @Column(DataType.ARRAY(DataType.STRING))
  25. grants: string[]
  26. @Column(DataType.ARRAY(DataType.STRING))
  27. redirectUris: string[]
  28. @CreatedAt
  29. createdAt: Date
  30. @UpdatedAt
  31. updatedAt: Date
  32. @HasMany(() => OAuthTokenModel, {
  33. onDelete: 'cascade'
  34. })
  35. OAuthTokens: OAuthTokenModel[]
  36. static countTotal () {
  37. return OAuthClientModel.count()
  38. }
  39. static loadFirstClient () {
  40. return OAuthClientModel.findOne()
  41. }
  42. static getByIdAndSecret (clientId: string, clientSecret: string) {
  43. const query = {
  44. where: {
  45. clientId,
  46. clientSecret
  47. }
  48. }
  49. return OAuthClientModel.findOne(query)
  50. }
  51. }