aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/redis.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-30 13:27:07 +0100
committerChocobozzz <me@florianbigard.com>2018-01-30 13:27:07 +0100
commitecb4e35f4e6c7304cb274593c13cb47fd5078b75 (patch)
tree1e238002340bc521afde59d52f406e41298a7aac /server/lib/redis.ts
parent80d1057bfcd3582af0dacf5ccd5a7a93ef95410b (diff)
downloadPeerTube-ecb4e35f4e6c7304cb274593c13cb47fd5078b75.tar.gz
PeerTube-ecb4e35f4e6c7304cb274593c13cb47fd5078b75.tar.zst
PeerTube-ecb4e35f4e6c7304cb274593c13cb47fd5078b75.zip
Add ability to reset our password
Diffstat (limited to 'server/lib/redis.ts')
-rw-r--r--server/lib/redis.ts84
1 files changed, 84 insertions, 0 deletions
diff --git a/server/lib/redis.ts b/server/lib/redis.ts
new file mode 100644
index 000000000..4240cc162
--- /dev/null
+++ b/server/lib/redis.ts
@@ -0,0 +1,84 @@
1import { createClient, RedisClient } from 'redis'
2import { logger } from '../helpers/logger'
3import { generateRandomString } from '../helpers/utils'
4import { CONFIG, USER_PASSWORD_RESET_LIFETIME } from '../initializers'
5
6class Redis {
7
8 private static instance: Redis
9 private initialized = false
10 private client: RedisClient
11 private prefix: string
12
13 private constructor () {}
14
15 init () {
16 // Already initialized
17 if (this.initialized === true) return
18 this.initialized = true
19
20 this.client = createClient({
21 host: CONFIG.REDIS.HOSTNAME,
22 port: CONFIG.REDIS.PORT
23 })
24
25 this.client.on('error', err => {
26 logger.error('Error in Redis client.', err)
27 process.exit(-1)
28 })
29
30 if (CONFIG.REDIS.AUTH) {
31 this.client.auth(CONFIG.REDIS.AUTH)
32 }
33
34 this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-'
35 }
36
37 async setResetPasswordVerificationString (userId: number) {
38 const generatedString = await generateRandomString(32)
39
40 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
41
42 return generatedString
43 }
44
45 async getResetPasswordLink (userId: number) {
46 return this.getValue(this.generateResetPasswordKey(userId))
47 }
48
49 private getValue (key: string) {
50 return new Promise<string>((res, rej) => {
51 this.client.get(this.prefix + key, (err, value) => {
52 if (err) return rej(err)
53
54 return res(value)
55 })
56 })
57 }
58
59 private setValue (key: string, value: string, expirationMilliseconds: number) {
60 return new Promise<void>((res, rej) => {
61 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
62 if (err) return rej(err)
63
64 if (ok !== 'OK') return rej(new Error('Redis result is not OK.'))
65
66 return res()
67 })
68 })
69 }
70
71 private generateResetPasswordKey (userId: number) {
72 return 'reset-password-' + userId
73 }
74
75 static get Instance () {
76 return this.instance || (this.instance = new this())
77 }
78}
79
80// ---------------------------------------------------------------------------
81
82export {
83 Redis
84}