]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/plugins/register-helpers-store.ts
Support logout and add id and pass tests
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / register-helpers-store.ts
index 5ca52b1517aaf8251123870364c6857940aa40a3..679ed365029f66d138e0ed9095d73a24f9dc6f7f 100644 (file)
@@ -20,6 +20,12 @@ import { RegisterServerSettingOptions } from '@shared/models/plugins/register-se
 import * as express from 'express'
 import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model'
 import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model'
+import {
+  RegisterServerAuthExternalOptions,
+  RegisterServerAuthExternalResult,
+  RegisterServerAuthPassOptions
+} from '@shared/models/plugins/register-server-auth.model'
+import { onExternalAuthPlugin } from '@server/lib/auth'
 
 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
 type VideoConstant = { [key in number | string]: string }
@@ -42,6 +48,9 @@ export class RegisterHelpersStore {
 
   private readonly settings: RegisterServerSettingOptions[] = []
 
+  private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []
+  private readonly externalAuths: RegisterServerAuthExternalOptions[] = []
+
   private readonly router: express.Router
 
   constructor (
@@ -69,6 +78,9 @@ export class RegisterHelpersStore {
     const videoPrivacyManager = this.buildVideoPrivacyManager()
     const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
 
+    const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
+    const registerExternalAuth = this.buildRegisterExternalAuth()
+
     const peertubeHelpers = buildPluginHelpers(this.npmName)
 
     return {
@@ -87,6 +99,9 @@ export class RegisterHelpersStore {
       videoPrivacyManager,
       playlistPrivacyManager,
 
+      registerIdAndPassAuth,
+      registerExternalAuth,
+
       peertubeHelpers
     }
   }
@@ -125,6 +140,14 @@ export class RegisterHelpersStore {
     return this.router
   }
 
+  getIdAndPassAuths () {
+    return this.idAndPassAuths
+  }
+
+  getExternalAuths () {
+    return this.externalAuths
+  }
+
   private buildGetRouter () {
     return () => this.router
   }
@@ -146,6 +169,31 @@ export class RegisterHelpersStore {
     }
   }
 
+  private buildRegisterIdAndPassAuth () {
+    return (options: RegisterServerAuthPassOptions) => {
+      if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
+        logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
+        return
+      }
+
+      this.idAndPassAuths.push(options)
+    }
+  }
+
+  private buildRegisterExternalAuth () {
+    const self = this
+
+    return (options: RegisterServerAuthExternalOptions) => {
+      this.externalAuths.push(options)
+
+      return {
+        onAuth (options: { username: string, email: string }): void {
+          onExternalAuthPlugin(self.npmName, options.username, options.email)
+        }
+      } as RegisterServerAuthExternalResult
+    }
+  }
+
   private buildSettingsManager (): PluginSettingsManager {
     return {
       getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),