]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/redis.ts
Prevent object storage mock conflicts
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
index 451ddd0b6d8d837afc9350001435702e396e6d61..8430b2227cfad5a3f7d52ffb88bdbe9a889acfe5 100644 (file)
@@ -9,7 +9,7 @@ import {
   CONTACT_FORM_LIFETIME,
   RESUMABLE_UPLOAD_SESSION_LIFETIME,
   TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME,
-  USER_EMAIL_VERIFY_LIFETIME,
+  EMAIL_VERIFY_LIFETIME,
   USER_PASSWORD_CREATE_LIFETIME,
   USER_PASSWORD_RESET_LIFETIME,
   VIEW_LIFETIME,
@@ -32,7 +32,8 @@ class Redis {
     if (this.initialized === true) return
     this.initialized = true
 
-    logger.info('Connecting to redis...')
+    const redisMode = CONFIG.REDIS.SENTINEL.ENABLED ? 'sentinel' : 'standalone'
+    logger.info('Connecting to redis ' + redisMode + '...')
 
     this.client = new IoRedis(Redis.getRedisClientOptions('', { enableAutoPipelining: true }))
     this.client.on('error', err => logger.error('Redis failed to connect', { err }))
@@ -56,10 +57,25 @@ class Redis {
     this.prefix = 'redis-' + WEBSERVER.HOST + '-'
   }
 
-  static getRedisClientOptions (connectionName?: string, options: RedisOptions = {}): RedisOptions {
+  static getRedisClientOptions (name?: string, options: RedisOptions = {}): RedisOptions {
+    const connectionName = [ 'PeerTube', name ].join('')
+    const connectTimeout = 20000 // Could be slow since node use sync call to compile PeerTube
+
+    if (CONFIG.REDIS.SENTINEL.ENABLED) {
+      return {
+        connectionName,
+        connectTimeout,
+        enableTLSForSentinelMode: CONFIG.REDIS.SENTINEL.ENABLE_TLS,
+        sentinelPassword: CONFIG.REDIS.AUTH,
+        sentinels: CONFIG.REDIS.SENTINEL.SENTINELS,
+        name: CONFIG.REDIS.SENTINEL.MASTER_NAME,
+        ...options
+      }
+    }
+
     return {
-      connectionName: [ 'PeerTube', connectionName ].join(''),
-      connectTimeout: 20000, // Could be slow since node use sync call to compile PeerTube
+      connectionName,
+      connectTimeout,
       password: CONFIG.REDIS.AUTH,
       db: CONFIG.REDIS.DB,
       host: CONFIG.REDIS.HOSTNAME,
@@ -124,16 +140,28 @@ class Redis {
 
   /* ************ Email verification ************ */
 
-  async setVerifyEmailVerificationString (userId: number) {
+  async setUserVerifyEmailVerificationString (userId: number) {
+    const generatedString = await generateRandomString(32)
+
+    await this.setValue(this.generateUserVerifyEmailKey(userId), generatedString, EMAIL_VERIFY_LIFETIME)
+
+    return generatedString
+  }
+
+  async getUserVerifyEmailLink (userId: number) {
+    return this.getValue(this.generateUserVerifyEmailKey(userId))
+  }
+
+  async setRegistrationVerifyEmailVerificationString (registrationId: number) {
     const generatedString = await generateRandomString(32)
 
-    await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
+    await this.setValue(this.generateRegistrationVerifyEmailKey(registrationId), generatedString, EMAIL_VERIFY_LIFETIME)
 
     return generatedString
   }
 
-  async getVerifyEmailLink (userId: number) {
-    return this.getValue(this.generateVerifyEmailKey(userId))
+  async getRegistrationVerifyEmailLink (registrationId: number) {
+    return this.getValue(this.generateRegistrationVerifyEmailKey(registrationId))
   }
 
   /* ************ Contact form per IP ************ */
@@ -346,8 +374,12 @@ class Redis {
     return 'two-factor-request-' + userId + '-' + token
   }
 
-  private generateVerifyEmailKey (userId: number) {
-    return 'verify-email-' + userId
+  private generateUserVerifyEmailKey (userId: number) {
+    return 'verify-email-user-' + userId
+  }
+
+  private generateRegistrationVerifyEmailKey (registrationId: number) {
+    return 'verify-email-registration-' + registrationId
   }
 
   private generateIPViewKey (ip: string, videoUUID: string) {