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

87 lines
1.9 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 {
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'
})
}
countTotal = function () {
return OAuthClient.count()
}
loadFirstClient = function () {
return OAuthClient.findOne()
}
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)
}