2017-05-22 13:58:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-06-16 02:45:46 -05:00
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-05-22 13:58:25 -05:00
|
|
|
import {
|
|
|
|
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
|
|
|
|
|
2017-06-11 10:35:32 -05:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
OAuthClient = sequelize.define<OAuthClientInstance, OAuthClientAttributes>('OAuthClient',
|
2016-12-11 14:50:51 -06:00
|
|
|
{
|
|
|
|
clientId: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false
|
2016-12-11 14:50:51 -06:00
|
|
|
},
|
|
|
|
clientSecret: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false
|
2016-12-11 14:50:51 -06:00
|
|
|
},
|
|
|
|
grants: {
|
|
|
|
type: DataTypes.ARRAY(DataTypes.STRING)
|
|
|
|
},
|
|
|
|
redirectUris: {
|
|
|
|
type: DataTypes.ARRAY(DataTypes.STRING)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 02:33:28 -06:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'clientId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'clientId', 'clientSecret' ],
|
|
|
|
unique: true
|
|
|
|
}
|
2017-05-22 13:58:25 -05:00
|
|
|
]
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countTotal,
|
|
|
|
getByIdAndSecret,
|
|
|
|
loadFirstClient
|
|
|
|
]
|
|
|
|
addMethodsToModel(OAuthClient, classMethods)
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
return OAuthClient
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
function associate (models) {
|
|
|
|
OAuthClient.hasMany(models.OAuthToken, {
|
|
|
|
foreignKey: 'oAuthClientId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
countTotal = function () {
|
|
|
|
return OAuthClient.count()
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
loadFirstClient = function () {
|
|
|
|
return OAuthClient.findOne()
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
getByIdAndSecret = function (clientId: string, clientSecret: string) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
clientId: clientId,
|
|
|
|
clientSecret: clientSecret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
return OAuthClient.findOne(query)
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|