]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/oauth-client.js
Server: add database field validations
[github/Chocobozzz/PeerTube.git] / server / models / oauth-client.js
1 'use strict'
2
3 module.exports = function (sequelize, DataTypes) {
4 const OAuthClient = sequelize.define('OAuthClient',
5 {
6 clientId: {
7 type: DataTypes.STRING,
8 allowNull: false
9 },
10 clientSecret: {
11 type: DataTypes.STRING,
12 allowNull: false
13 },
14 grants: {
15 type: DataTypes.ARRAY(DataTypes.STRING)
16 },
17 redirectUris: {
18 type: DataTypes.ARRAY(DataTypes.STRING)
19 }
20 },
21 {
22 classMethods: {
23 associate,
24
25 getByIdAndSecret,
26 list,
27 loadFirstClient
28 }
29 }
30 )
31
32 return OAuthClient
33 }
34
35 // ---------------------------------------------------------------------------
36
37 function associate (models) {
38 this.hasMany(models.OAuthToken, {
39 foreignKey: {
40 name: 'oAuthClientId',
41 allowNull: false
42 },
43 onDelete: 'cascade'
44 })
45 }
46
47 function list (callback) {
48 return this.findAll().asCallback(callback)
49 }
50
51 function loadFirstClient (callback) {
52 return this.findOne().asCallback(callback)
53 }
54
55 function getByIdAndSecret (clientId, clientSecret) {
56 const query = {
57 where: {
58 clientId: clientId,
59 clientSecret: clientSecret
60 }
61 }
62
63 return this.findOne(query)
64 }