PeerTube/server/models/oauth/oauth-client.ts

88 lines
2.0 KiB
TypeScript
Raw Normal View History

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 {
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
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
}
// ---------------------------------------------------------------------------
2017-05-22 13:58:25 -05:00
function associate (models) {
OAuthClient.hasMany(models.OAuthToken, {
foreignKey: 'oAuthClientId',
onDelete: 'cascade'
})
}
2017-06-10 15:15:25 -05:00
countTotal = function (callback: OAuthClientMethods.CountTotalCallback) {
2017-05-22 13:58:25 -05:00
return OAuthClient.count().asCallback(callback)
}
2017-06-10 15:15:25 -05:00
loadFirstClient = function (callback: OAuthClientMethods.LoadFirstClientCallback) {
2017-05-22 13:58:25 -05:00
return OAuthClient.findOne().asCallback(callback)
}
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)
}