]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/redis.ts
Refactor auth flow
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
CommitLineData
4195cd2b 1import * as express from 'express'
ecb4e35f
C
2import { createClient, RedisClient } from 'redis'
3import { logger } from '../helpers/logger'
4import { generateRandomString } from '../helpers/utils'
a4101923 5import {
a4101923
C
6 CONTACT_FORM_LIFETIME,
7 USER_EMAIL_VERIFY_LIFETIME,
8 USER_PASSWORD_RESET_LIFETIME,
45f1bd72 9 USER_PASSWORD_CREATE_LIFETIME,
e4bf7856 10 VIEW_LIFETIME,
db48de85
C
11 WEBSERVER,
12 TRACKER_RATE_LIMITS
74dc3bca 13} from '../initializers/constants'
6dd9de95 14import { CONFIG } from '../initializers/config'
4195cd2b
C
15
16type CachedRoute = {
a1587156 17 body: string
2cebd797
C
18 contentType?: string
19 statusCode?: string
4195cd2b 20}
ecb4e35f
C
21
22class Redis {
23
24 private static instance: Redis
25 private initialized = false
26 private client: RedisClient
27 private prefix: string
28
a1587156
C
29 private constructor () {
30 }
ecb4e35f
C
31
32 init () {
33 // Already initialized
34 if (this.initialized === true) return
35 this.initialized = true
36
47f6409b 37 this.client = createClient(Redis.getRedisClientOptions())
ecb4e35f
C
38
39 this.client.on('error', err => {
d5b7d911 40 logger.error('Error in Redis client.', { err })
ecb4e35f
C
41 process.exit(-1)
42 })
43
44 if (CONFIG.REDIS.AUTH) {
45 this.client.auth(CONFIG.REDIS.AUTH)
46 }
47
6dd9de95 48 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
ecb4e35f
C
49 }
50
47f6409b 51 static getRedisClientOptions () {
19f7b248
RK
52 return Object.assign({},
53 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
54 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
a1587156
C
55 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
56 ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
57 : { path: CONFIG.REDIS.SOCKET }
19f7b248
RK
58 )
59 }
60
47f6409b
C
61 getClient () {
62 return this.client
63 }
64
65 getPrefix () {
66 return this.prefix
67 }
68
a1587156 69 /* ************ Forgot password ************ */
6e46de09 70
ecb4e35f
C
71 async setResetPasswordVerificationString (userId: number) {
72 const generatedString = await generateRandomString(32)
73
74 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
75
45f1bd72
JL
76 return generatedString
77 }
78
79 async setCreatePasswordVerificationString (userId: number) {
80 const generatedString = await generateRandomString(32)
81
82 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
83
ecb4e35f
C
84 return generatedString
85 }
86
e9c5f123
C
87 async removePasswordVerificationString (userId: number) {
88 return this.removeValue(this.generateResetPasswordKey(userId))
89 }
90
ecb4e35f
C
91 async getResetPasswordLink (userId: number) {
92 return this.getValue(this.generateResetPasswordKey(userId))
93 }
94
a1587156 95 /* ************ Email verification ************ */
6e46de09 96
d9eaee39
JM
97 async setVerifyEmailVerificationString (userId: number) {
98 const generatedString = await generateRandomString(32)
99
100 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
101
102 return generatedString
103 }
104
105 async getVerifyEmailLink (userId: number) {
106 return this.getValue(this.generateVerifyEmailKey(userId))
107 }
108
a1587156 109 /* ************ Contact form per IP ************ */
a4101923
C
110
111 async setContactFormIp (ip: string) {
112 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
113 }
114
0f6acda1 115 async doesContactFormIpExist (ip: string) {
a4101923
C
116 return this.exists(this.generateContactFormKey(ip))
117 }
118
a1587156 119 /* ************ Views per IP ************ */
6e46de09 120
e4bf7856
C
121 setIPVideoView (ip: string, videoUUID: string, isLive: boolean) {
122 const lifetime = isLive
123 ? VIEW_LIFETIME.LIVE
124 : VIEW_LIFETIME.VIDEO
125
126 return this.setValue(this.generateViewKey(ip, videoUUID), '1', lifetime)
b5c0e955
C
127 }
128
0f6acda1 129 async doesVideoIPViewExist (ip: string, videoUUID: string) {
6e46de09 130 return this.exists(this.generateViewKey(ip, videoUUID))
b5c0e955
C
131 }
132
db48de85
C
133 /* ************ Tracker IP block ************ */
134
135 setTrackerBlockIP (ip: string) {
136 return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
137 }
138
139 async doesTrackerBlockIPExist (ip: string) {
140 return this.exists(this.generateTrackerBlockIPKey(ip))
141 }
142
a1587156 143 /* ************ API cache ************ */
6e46de09 144
4195cd2b 145 async getCachedRoute (req: express.Request) {
6e46de09 146 const cached = await this.getObject(this.generateCachedRouteKey(req))
4195cd2b
C
147
148 return cached as CachedRoute
149 }
150
fd4484f1 151 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
a1587156
C
152 const cached: CachedRoute = Object.assign(
153 {},
154 { body: body.toString() },
155 (contentType) ? { contentType } : null,
156 (statusCode) ? { statusCode: statusCode.toString() } : null
c1e791ba 157 )
4195cd2b 158
6e46de09 159 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
4195cd2b
C
160 }
161
a1587156 162 /* ************ Video views ************ */
6e46de09 163
6b616860
C
164 addVideoView (videoId: number) {
165 const keyIncr = this.generateVideoViewKey(videoId)
166 const keySet = this.generateVideosViewKey()
167
168 return Promise.all([
169 this.addToSet(keySet, videoId.toString()),
170 this.increment(keyIncr)
171 ])
172 }
173
174 async getVideoViews (videoId: number, hour: number) {
175 const key = this.generateVideoViewKey(videoId, hour)
176
177 const valueString = await this.getValue(key)
6040f87d
C
178 const valueInt = parseInt(valueString, 10)
179
180 if (isNaN(valueInt)) {
181 logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
182 return undefined
183 }
184
185 return valueInt
6b616860
C
186 }
187
188 async getVideosIdViewed (hour: number) {
189 const key = this.generateVideosViewKey(hour)
190
191 const stringIds = await this.getSet(key)
192 return stringIds.map(s => parseInt(s, 10))
193 }
194
195 deleteVideoViews (videoId: number, hour: number) {
196 const keySet = this.generateVideosViewKey(hour)
197 const keyIncr = this.generateVideoViewKey(videoId, hour)
198
199 return Promise.all([
200 this.deleteFromSet(keySet, videoId.toString()),
201 this.deleteKey(keyIncr)
202 ])
203 }
204
a1587156 205 /* ************ Keys generation ************ */
6e46de09
C
206
207 generateCachedRouteKey (req: express.Request) {
208 return req.method + '-' + req.originalUrl
209 }
210
211 private generateVideosViewKey (hour?: number) {
6b616860
C
212 if (!hour) hour = new Date().getHours()
213
214 return `videos-view-h${hour}`
215 }
216
6e46de09 217 private generateVideoViewKey (videoId: number, hour?: number) {
cc49be3e 218 if (hour === undefined || hour === null) hour = new Date().getHours()
6b616860
C
219
220 return `video-view-${videoId}-h${hour}`
221 }
222
6e46de09 223 private generateResetPasswordKey (userId: number) {
b40f0575
C
224 return 'reset-password-' + userId
225 }
226
6e46de09 227 private generateVerifyEmailKey (userId: number) {
d9eaee39
JM
228 return 'verify-email-' + userId
229 }
230
6e46de09 231 private generateViewKey (ip: string, videoUUID: string) {
a4101923
C
232 return `views-${videoUUID}-${ip}`
233 }
234
db48de85
C
235 private generateTrackerBlockIPKey (ip: string) {
236 return `tracker-block-ip-${ip}`
237 }
238
a4101923
C
239 private generateContactFormKey (ip: string) {
240 return 'contact-form-' + ip
b40f0575
C
241 }
242
a1587156 243 /* ************ Redis helpers ************ */
b40f0575 244
ecb4e35f
C
245 private getValue (key: string) {
246 return new Promise<string>((res, rej) => {
247 this.client.get(this.prefix + key, (err, value) => {
248 if (err) return rej(err)
249
250 return res(value)
251 })
252 })
253 }
254
6b616860
C
255 private getSet (key: string) {
256 return new Promise<string[]>((res, rej) => {
257 this.client.smembers(this.prefix + key, (err, value) => {
258 if (err) return rej(err)
259
260 return res(value)
261 })
262 })
263 }
264
265 private addToSet (key: string, value: string) {
ba5a8d89 266 return new Promise<void>((res, rej) => {
6b616860
C
267 this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res())
268 })
269 }
270
271 private deleteFromSet (key: string, value: string) {
272 return new Promise<void>((res, rej) => {
273 this.client.srem(this.prefix + key, value, err => err ? rej(err) : res())
274 })
275 }
276
277 private deleteKey (key: string) {
278 return new Promise<void>((res, rej) => {
279 this.client.del(this.prefix + key, err => err ? rej(err) : res())
280 })
281 }
282
6e46de09
C
283 private deleteFieldInHash (key: string, field: string) {
284 return new Promise<void>((res, rej) => {
285 this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res())
286 })
287 }
288
ecb4e35f
C
289 private setValue (key: string, value: string, expirationMilliseconds: number) {
290 return new Promise<void>((res, rej) => {
291 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
292 if (err) return rej(err)
293
4195cd2b 294 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
ecb4e35f
C
295
296 return res()
297 })
298 })
299 }
300
e9c5f123
C
301 private removeValue (key: string) {
302 return new Promise<void>((res, rej) => {
303 this.client.del(this.prefix + key, err => {
304 if (err) return rej(err)
305
306 return res()
307 })
308 })
309 }
310
a1587156 311 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
4195cd2b
C
312 return new Promise<void>((res, rej) => {
313 this.client.hmset(this.prefix + key, obj, (err, ok) => {
314 if (err) return rej(err)
315 if (!ok) return rej(new Error('Redis mset result is not OK.'))
316
317 this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
318 if (err) return rej(err)
319 if (!ok) return rej(new Error('Redis expiration result is not OK.'))
320
321 return res()
322 })
323 })
324 })
325 }
326
327 private getObject (key: string) {
a1587156 328 return new Promise<{ [id: string]: string }>((res, rej) => {
4195cd2b
C
329 this.client.hgetall(this.prefix + key, (err, value) => {
330 if (err) return rej(err)
331
332 return res(value)
333 })
334 })
335 }
336
6e46de09
C
337 private setValueInHash (key: string, field: string, value: string) {
338 return new Promise<void>((res, rej) => {
339 this.client.hset(this.prefix + key, field, value, (err) => {
340 if (err) return rej(err)
341
342 return res()
343 })
344 })
345 }
346
6b616860
C
347 private increment (key: string) {
348 return new Promise<number>((res, rej) => {
349 this.client.incr(this.prefix + key, (err, value) => {
350 if (err) return rej(err)
351
352 return res(value)
353 })
354 })
355 }
356
b5c0e955
C
357 private exists (key: string) {
358 return new Promise<boolean>((res, rej) => {
359 this.client.exists(this.prefix + key, (err, existsNumber) => {
360 if (err) return rej(err)
361
362 return res(existsNumber === 1)
363 })
364 })
365 }
366
ecb4e35f
C
367 static get Instance () {
368 return this.instance || (this.instance = new this())
369 }
370}
371
372// ---------------------------------------------------------------------------
373
374export {
375 Redis
376}