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