aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/models/oauth/oauth-client.ts
blob: 890954bdb6f54a04e46871d5ec6ae9776ef2d533 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                                                                                               
                                                   
                                               
 


                           
     

                             

      

                                             
     

   
                                                                                        
 


                   
 


                      
 
                                          

                  
                                          



                        
 

                 
 
                                   

                       
                                
 


                                   
 


                                     
 





                                                                    
     
 

                                          
 
import { AllowNull, Column, CreatedAt, DataType, HasMany, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { AttributesOnly } from '@shared/core-utils'
import { OAuthTokenModel } from './oauth-token'

@Table({
  tableName: 'oAuthClient',
  indexes: [
    {
      fields: [ 'clientId' ],
      unique: true
    },
    {
      fields: [ 'clientId', 'clientSecret' ],
      unique: true
    }
  ]
})
export class OAuthClientModel extends Model<Partial<AttributesOnly<OAuthClientModel>>> {

  @AllowNull(false)
  @Column
  clientId: string

  @AllowNull(false)
  @Column
  clientSecret: string

  @Column(DataType.ARRAY(DataType.STRING))
  grants: string[]

  @Column(DataType.ARRAY(DataType.STRING))
  redirectUris: string[]

  @CreatedAt
  createdAt: Date

  @UpdatedAt
  updatedAt: Date

  @HasMany(() => OAuthTokenModel, {
    onDelete: 'cascade'
  })
  OAuthTokens: OAuthTokenModel[]

  static countTotal () {
    return OAuthClientModel.count()
  }

  static loadFirstClient () {
    return OAuthClientModel.findOne()
  }

  static getByIdAndSecret (clientId: string, clientSecret: string) {
    const query = {
      where: {
        clientId: clientId,
        clientSecret: clientSecret
      }
    }

    return OAuthClientModel.findOne(query)
  }
}