aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth-token.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-06-16 09:45:46 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-06-16 09:45:46 +0200
commit74889a71fe687dda74f2a687653122327807af36 (patch)
treee938e8b6401b74fbec80513a877d9967f2c0dbcd /server/models/oauth-token.ts
parent15a302943d84bc0978b84fe33110c4daa451d311 (diff)
downloadPeerTube-74889a71fe687dda74f2a687653122327807af36.tar.gz
PeerTube-74889a71fe687dda74f2a687653122327807af36.tar.zst
PeerTube-74889a71fe687dda74f2a687653122327807af36.zip
Reorganize model files
Diffstat (limited to 'server/models/oauth-token.ts')
-rw-r--r--server/models/oauth-token.ts160
1 files changed, 0 insertions, 160 deletions
diff --git a/server/models/oauth-token.ts b/server/models/oauth-token.ts
deleted file mode 100644
index 91cef11dd..000000000
--- a/server/models/oauth-token.ts
+++ /dev/null
@@ -1,160 +0,0 @@
1import * as Sequelize from 'sequelize'
2
3import { logger } from '../helpers'
4
5import { addMethodsToModel } from './utils'
6import {
7 OAuthTokenClass,
8 OAuthTokenInstance,
9 OAuthTokenAttributes,
10
11 OAuthTokenMethods,
12 OAuthTokenInfo
13} from './oauth-token-interface'
14
15let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
16let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
17let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
18let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
19let removeByUserId: OAuthTokenMethods.RemoveByUserId
20
21export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
22 OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken',
23 {
24 accessToken: {
25 type: DataTypes.STRING,
26 allowNull: false
27 },
28 accessTokenExpiresAt: {
29 type: DataTypes.DATE,
30 allowNull: false
31 },
32 refreshToken: {
33 type: DataTypes.STRING,
34 allowNull: false
35 },
36 refreshTokenExpiresAt: {
37 type: DataTypes.DATE,
38 allowNull: false
39 }
40 },
41 {
42 indexes: [
43 {
44 fields: [ 'refreshToken' ],
45 unique: true
46 },
47 {
48 fields: [ 'accessToken' ],
49 unique: true
50 },
51 {
52 fields: [ 'userId' ]
53 },
54 {
55 fields: [ 'oAuthClientId' ]
56 }
57 ]
58 }
59 )
60
61 const classMethods = [
62 associate,
63
64 getByRefreshTokenAndPopulateClient,
65 getByTokenAndPopulateUser,
66 getByRefreshTokenAndPopulateUser,
67 removeByUserId
68 ]
69 addMethodsToModel(OAuthToken, classMethods)
70
71 return OAuthToken
72}
73
74// ---------------------------------------------------------------------------
75
76function associate (models) {
77 OAuthToken.belongsTo(models.User, {
78 foreignKey: {
79 name: 'userId',
80 allowNull: false
81 },
82 onDelete: 'cascade'
83 })
84
85 OAuthToken.belongsTo(models.OAuthClient, {
86 foreignKey: {
87 name: 'oAuthClientId',
88 allowNull: false
89 },
90 onDelete: 'cascade'
91 })
92}
93
94getByRefreshTokenAndPopulateClient = function (refreshToken: string) {
95 const query = {
96 where: {
97 refreshToken: refreshToken
98 },
99 include: [ OAuthToken['sequelize'].models.OAuthClient ]
100 }
101
102 return OAuthToken.findOne(query).then(function (token) {
103 if (!token) return null
104
105 const tokenInfos: OAuthTokenInfo = {
106 refreshToken: token.refreshToken,
107 refreshTokenExpiresAt: token.refreshTokenExpiresAt,
108 client: {
109 id: token['client'].id
110 },
111 user: {
112 id: token['user']
113 }
114 }
115
116 return tokenInfos
117 }).catch(function (err) {
118 logger.info('getRefreshToken error.', { error: err })
119 })
120}
121
122getByTokenAndPopulateUser = function (bearerToken: string) {
123 const query = {
124 where: {
125 accessToken: bearerToken
126 },
127 include: [ OAuthToken['sequelize'].models.User ]
128 }
129
130 return OAuthToken.findOne(query).then(function (token) {
131 if (token) token['user'] = token.User
132
133 return token
134 })
135}
136
137getByRefreshTokenAndPopulateUser = function (refreshToken: string) {
138 const query = {
139 where: {
140 refreshToken: refreshToken
141 },
142 include: [ OAuthToken['sequelize'].models.User ]
143 }
144
145 return OAuthToken.findOne(query).then(function (token) {
146 token['user'] = token.User
147
148 return token
149 })
150}
151
152removeByUserId = function (userId, callback) {
153 const query = {
154 where: {
155 userId: userId
156 }
157 }
158
159 return OAuthToken.destroy(query).asCallback(callback)
160}