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