X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fmodels%2Foauth-client.js;h=b56838d4c9918126a35b82c2f38d52caf5c77eb9;hb=67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677;hp=a1aefa985283ad279a6e925e3a918784b4c8e244;hpb=c4403b29ad4db097af528a7f04eea07e0ed320d0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js index a1aefa985..b56838d4c 100644 --- a/server/models/oauth-client.js +++ b/server/models/oauth-client.js @@ -1,33 +1,64 @@ -const mongoose = require('mongoose') +'use strict' -// --------------------------------------------------------------------------- - -const OAuthClientSchema = mongoose.Schema({ - clientSecret: String, - grants: Array, - redirectUris: Array -}) +module.exports = function (sequelize, DataTypes) { + const OAuthClient = sequelize.define('OAuthClient', + { + clientId: { + type: DataTypes.STRING, + allowNull: false + }, + clientSecret: { + type: DataTypes.STRING, + allowNull: false + }, + grants: { + type: DataTypes.ARRAY(DataTypes.STRING) + }, + redirectUris: { + type: DataTypes.ARRAY(DataTypes.STRING) + } + }, + { + classMethods: { + associate, -OAuthClientSchema.path('clientSecret').required(true) + getByIdAndSecret, + list, + loadFirstClient + } + } + ) -OAuthClientSchema.statics = { - getByIdAndSecret, - list, - loadFirstClient + return OAuthClient } -mongoose.model('OAuthClient', OAuthClientSchema) - // --------------------------------------------------------------------------- +function associate (models) { + this.hasMany(models.OAuthToken, { + foreignKey: { + name: 'oAuthClientId', + allowNull: false + }, + onDelete: 'cascade' + }) +} + function list (callback) { - return this.find(callback) + return this.findAll().asCallback(callback) } function loadFirstClient (callback) { - return this.findOne({}, callback) + return this.findOne().asCallback(callback) } -function getByIdAndSecret (id, clientSecret) { - return this.findOne({ _id: id, clientSecret: clientSecret }).exec() +function getByIdAndSecret (clientId, clientSecret) { + const query = { + where: { + clientId: clientId, + clientSecret: clientSecret + } + } + + return this.findOne(query) }