Add plugin auth migrations
authorChocobozzz <me@florianbigard.com>
Mon, 27 Apr 2020 08:19:14 +0000 (10:19 +0200)
committerChocobozzz <chocobozzz@cpy.re>
Mon, 4 May 2020 14:21:39 +0000 (16:21 +0200)
server/initializers/migrations/0485-video-file-metadata.ts [deleted file]
server/initializers/migrations/0490-plugin-auth.ts [new file with mode: 0644]
server/lib/auth.ts
server/lib/plugins/register-helpers-store.ts
server/models/server/plugin.ts
server/tests/plugins/id-and-pass-auth.ts
shared/models/plugins/plugin-settings-manager.model.ts

diff --git a/server/initializers/migrations/0485-video-file-metadata.ts b/server/initializers/migrations/0485-video-file-metadata.ts
deleted file mode 100644 (file)
index 5d95be0..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-import * as Sequelize from 'sequelize'
-
-async function up (utils: {
-  transaction: Sequelize.Transaction
-  queryInterface: Sequelize.QueryInterface
-  sequelize: Sequelize.Sequelize
-}): Promise<void> {
-
-  const metadata = {
-    type: Sequelize.JSONB,
-    allowNull: true
-  }
-  await utils.queryInterface.addColumn('videoFile', 'metadata', metadata)
-
-  const metadataUrl = {
-    type: Sequelize.STRING,
-    allowNull: true
-  }
-  await utils.queryInterface.addColumn('videoFile', 'metadataUrl', metadataUrl)
-
-}
-
-function down (options) {
-  throw new Error('Not implemented.')
-}
-
-export {
-  up,
-  down
-}
diff --git a/server/initializers/migrations/0490-plugin-auth.ts b/server/initializers/migrations/0490-plugin-auth.ts
new file mode 100644 (file)
index 0000000..ea636a4
--- /dev/null
@@ -0,0 +1,42 @@
+import * as Sequelize from 'sequelize'
+
+async function up (utils: {
+  transaction: Sequelize.Transaction
+  queryInterface: Sequelize.QueryInterface
+  sequelize: Sequelize.Sequelize
+}): Promise<void> {
+
+  {
+    const password = {
+      type: Sequelize.STRING,
+      allowNull: true
+    }
+    await utils.queryInterface.changeColumn('user', 'password', password)
+  }
+
+  {
+    const pluginAuth = {
+      type: Sequelize.STRING,
+      allowNull: true
+    }
+    await utils.queryInterface.addColumn('user', 'pluginAuth', pluginAuth)
+  }
+
+  {
+    const authName = {
+      type: Sequelize.STRING,
+      allowNull: true
+    }
+    await utils.queryInterface.addColumn('oAuthToken', 'authName', authName)
+  }
+
+}
+
+function down (options) {
+  throw new Error('Not implemented.')
+}
+
+export {
+  up,
+  down
+}
index c2a6fcaffd7e65f1658242a56cb12444b46dce17..c47ec62d08499140a95cfc50f72fa2e9db35d81b 100644 (file)
@@ -126,26 +126,30 @@ async function proxifyPasswordGrant (req: express.Request, res: express.Response
       authOptions.authName, pluginAuth.npmName, loginOptions.id, authOptions.getWeight()
     )
 
-    const loginResult = await authOptions.login(loginOptions)
-    if (loginResult) {
-      logger.info(
-        'Login success with auth method %s of plugin %s for %s.',
-        authOptions.authName, pluginAuth.npmName, loginOptions.id
-      )
-
-      res.locals.bypassLogin = {
-        bypass: true,
-        pluginName: pluginAuth.npmName,
-        authName: authOptions.authName,
-        user: {
-          username: loginResult.username,
-          email: loginResult.email,
-          role: loginResult.role || UserRole.USER,
-          displayName: loginResult.displayName || loginResult.username
+    try {
+      const loginResult = await authOptions.login(loginOptions)
+      if (loginResult) {
+        logger.info(
+          'Login success with auth method %s of plugin %s for %s.',
+          authOptions.authName, pluginAuth.npmName, loginOptions.id
+        )
+
+        res.locals.bypassLogin = {
+          bypass: true,
+          pluginName: pluginAuth.npmName,
+          authName: authOptions.authName,
+          user: {
+            username: loginResult.username,
+            email: loginResult.email,
+            role: loginResult.role || UserRole.USER,
+            displayName: loginResult.displayName || loginResult.username
+          }
         }
-      }
 
-      return
+        return
+      }
+    } catch (err) {
+      logger.error('Error in auth method %s of plugin %s', authOptions.authName, pluginAuth.npmName, { err })
     }
   }
 }
index 679ed365029f66d138e0ed9095d73a24f9dc6f7f..687974ccf0b3ccfdb40edc8fd587a49ae4d5f515 100644 (file)
@@ -198,6 +198,8 @@ export class RegisterHelpersStore {
     return {
       getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
 
+      getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
+
       setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
     }
   }
index 95774a4674634bf8609cedc5bdd5ff619054180e..83c873c5bad8dabc86c476e0c868a87f835e874e 100644 (file)
@@ -129,6 +129,31 @@ export class PluginModel extends Model<PluginModel> {
       })
   }
 
+  static getSettings (pluginName: string, pluginType: PluginType, settingNames: string[]) {
+    const query = {
+      attributes: [ 'settings' ],
+      where: {
+        name: pluginName,
+        type: pluginType
+      }
+    }
+
+    return PluginModel.findOne(query)
+      .then(p => {
+        if (!p || !p.settings) return {}
+
+        const result: { [settingName: string ]: string } = {}
+
+        for (const key of Object.keys(p.settings)) {
+          if (settingNames.includes(key)) {
+            result[key] = p.settings[key]
+          }
+        }
+
+        return result
+      })
+  }
+
   static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
     const query = {
       where: {
index 0268d35a037c96f736db70e4b50ca103c8dd1fbc..caf65b55f971b1008f724d1de439fbb7eff2a435 100644 (file)
@@ -143,7 +143,7 @@ describe('Test id and pass auth plugins', function () {
     expect(body.role).to.equal(UserRole.MODERATOR)
   })
 
-  it('Should correctly auth token of laguna', async function () {
+  it('Should reject token of laguna by the plugin hook', async function () {
     this.timeout(10000)
 
     await wait(5000)
index 63390a190b693cb49c38131ae5effc388a5a206f..f83f53b8f8535033c69a8a02865048f7c96d9a80 100644 (file)
@@ -1,7 +1,9 @@
 import * as Bluebird from 'bluebird'
 
 export interface PluginSettingsManager {
-  getSetting: (name: string) => Bluebird<string>
+  getSetting: (name: string) => Bluebird<string | boolean>
+
+  getSettings: (names: string[]) => Bluebird<{ [settingName: string]: string | boolean }>
 
   setSetting: (name: string, value: string) => Bluebird<any>
 }