diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/models/oauth-token.js | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/models/oauth-token.js')
-rw-r--r-- | server/models/oauth-token.js | 144 |
1 files changed, 0 insertions, 144 deletions
diff --git a/server/models/oauth-token.js b/server/models/oauth-token.js deleted file mode 100644 index 68e7c9ff7..000000000 --- a/server/models/oauth-token.js +++ /dev/null | |||
@@ -1,144 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const logger = require('../helpers/logger') | ||
4 | |||
5 | // --------------------------------------------------------------------------- | ||
6 | |||
7 | module.exports = function (sequelize, DataTypes) { | ||
8 | const OAuthToken = sequelize.define('OAuthToken', | ||
9 | { | ||
10 | accessToken: { | ||
11 | type: DataTypes.STRING, | ||
12 | allowNull: false | ||
13 | }, | ||
14 | accessTokenExpiresAt: { | ||
15 | type: DataTypes.DATE, | ||
16 | allowNull: false | ||
17 | }, | ||
18 | refreshToken: { | ||
19 | type: DataTypes.STRING, | ||
20 | allowNull: false | ||
21 | }, | ||
22 | refreshTokenExpiresAt: { | ||
23 | type: DataTypes.DATE, | ||
24 | allowNull: false | ||
25 | } | ||
26 | }, | ||
27 | { | ||
28 | indexes: [ | ||
29 | { | ||
30 | fields: [ 'refreshToken' ], | ||
31 | unique: true | ||
32 | }, | ||
33 | { | ||
34 | fields: [ 'accessToken' ], | ||
35 | unique: true | ||
36 | }, | ||
37 | { | ||
38 | fields: [ 'userId' ] | ||
39 | }, | ||
40 | { | ||
41 | fields: [ 'oAuthClientId' ] | ||
42 | } | ||
43 | ], | ||
44 | classMethods: { | ||
45 | associate, | ||
46 | |||
47 | getByRefreshTokenAndPopulateClient, | ||
48 | getByTokenAndPopulateUser, | ||
49 | getByRefreshTokenAndPopulateUser, | ||
50 | removeByUserId | ||
51 | } | ||
52 | } | ||
53 | ) | ||
54 | |||
55 | return OAuthToken | ||
56 | } | ||
57 | |||
58 | // --------------------------------------------------------------------------- | ||
59 | |||
60 | function associate (models) { | ||
61 | this.belongsTo(models.User, { | ||
62 | foreignKey: { | ||
63 | name: 'userId', | ||
64 | allowNull: false | ||
65 | }, | ||
66 | onDelete: 'cascade' | ||
67 | }) | ||
68 | |||
69 | this.belongsTo(models.OAuthClient, { | ||
70 | foreignKey: { | ||
71 | name: 'oAuthClientId', | ||
72 | allowNull: false | ||
73 | }, | ||
74 | onDelete: 'cascade' | ||
75 | }) | ||
76 | } | ||
77 | |||
78 | function getByRefreshTokenAndPopulateClient (refreshToken) { | ||
79 | const query = { | ||
80 | where: { | ||
81 | refreshToken: refreshToken | ||
82 | }, | ||
83 | include: [ this.associations.OAuthClient ] | ||
84 | } | ||
85 | |||
86 | return this.findOne(query).then(function (token) { | ||
87 | if (!token) return token | ||
88 | |||
89 | const tokenInfos = { | ||
90 | refreshToken: token.refreshToken, | ||
91 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | ||
92 | client: { | ||
93 | id: token.client.id | ||
94 | }, | ||
95 | user: { | ||
96 | id: token.user | ||
97 | } | ||
98 | } | ||
99 | |||
100 | return tokenInfos | ||
101 | }).catch(function (err) { | ||
102 | logger.info('getRefreshToken error.', { error: err }) | ||
103 | }) | ||
104 | } | ||
105 | |||
106 | function getByTokenAndPopulateUser (bearerToken) { | ||
107 | const query = { | ||
108 | where: { | ||
109 | accessToken: bearerToken | ||
110 | }, | ||
111 | include: [ this.sequelize.models.User ] | ||
112 | } | ||
113 | |||
114 | return this.findOne(query).then(function (token) { | ||
115 | if (token) token.user = token.User | ||
116 | |||
117 | return token | ||
118 | }) | ||
119 | } | ||
120 | |||
121 | function getByRefreshTokenAndPopulateUser (refreshToken) { | ||
122 | const query = { | ||
123 | where: { | ||
124 | refreshToken: refreshToken | ||
125 | }, | ||
126 | include: [ this.sequelize.models.User ] | ||
127 | } | ||
128 | |||
129 | return this.findOne(query).then(function (token) { | ||
130 | token.user = token.User | ||
131 | |||
132 | return token | ||
133 | }) | ||
134 | } | ||
135 | |||
136 | function removeByUserId (userId, callback) { | ||
137 | const query = { | ||
138 | where: { | ||
139 | userId: userId | ||
140 | } | ||
141 | } | ||
142 | |||
143 | return this.destroy(query).asCallback(callback) | ||
144 | } | ||