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

88 lines
2.0 KiB
TypeScript
Raw Normal View History

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