aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth-client.ts
blob: 22fae284271924b627f728109ae4085d1f8161a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import * as Sequelize from 'sequelize'

import { addMethodsToModel } from './utils'
import {
  OAuthClientClass,
  OAuthClientInstance,
  OAuthClientAttributes,

  OAuthClientMethods
} from './oauth-client-interface'

let OAuthClient: Sequelize.Model<OAuthClientInstance, OAuthClientAttributes>
let countTotal: OAuthClientMethods.CountTotal
let loadFirstClient: OAuthClientMethods.LoadFirstClient
let getByIdAndSecret: OAuthClientMethods.GetByIdAndSecret

export default function (sequelize, DataTypes) {
  OAuthClient = sequelize.define('OAuthClient',
    {
      clientId: {
        type: DataTypes.STRING,
        allowNull: false
      },
      clientSecret: {
        type: DataTypes.STRING,
        allowNull: false
      },
      grants: {
        type: DataTypes.ARRAY(DataTypes.STRING)
      },
      redirectUris: {
        type: DataTypes.ARRAY(DataTypes.STRING)
      }
    },
    {
      indexes: [
        {
          fields: [ 'clientId' ],
          unique: true
        },
        {
          fields: [ 'clientId', 'clientSecret' ],
          unique: true
        }
      ]
    }
  )

  const classMethods = [
    associate,

    countTotal,
    getByIdAndSecret,
    loadFirstClient
  ]
  addMethodsToModel(OAuthClient, classMethods)

  return OAuthClient
}

// ---------------------------------------------------------------------------

function associate (models) {
  OAuthClient.hasMany(models.OAuthToken, {
    foreignKey: 'oAuthClientId',
    onDelete: 'cascade'
  })
}

countTotal = function (callback: OAuthClientMethods.CountTotalCallback) {
  return OAuthClient.count().asCallback(callback)
}

loadFirstClient = function (callback: OAuthClientMethods.LoadFirstClientCallback) {
  return OAuthClient.findOne().asCallback(callback)
}

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

  return OAuthClient.findOne(query)
}