oauth-client.ts 1.2 KB

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