From 4a8d113b9b57d97ff13ad1608798eabca99643e4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 28 Apr 2020 14:49:03 +0200 Subject: Begin support for external auths --- server/controllers/api/config.ts | 56 ++++++++++++++++++++++++++++++++-------- server/controllers/plugins.ts | 20 +++++++++++++- 2 files changed, 64 insertions(+), 12 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 06fe30371..e8941bc73 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -1,22 +1,22 @@ +import { Hooks } from '@server/lib/plugins/hooks' import * as express from 'express' +import { remove, writeJSON } from 'fs-extra' import { snakeCase } from 'lodash' -import { ServerConfig, UserRight } from '../../../shared' +import validator from 'validator' +import { RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig, UserRight } from '../../../shared' import { About } from '../../../shared/models/server/about.model' import { CustomConfig } from '../../../shared/models/server/custom-config.model' -import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' -import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '../../initializers/constants' -import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' -import { customConfigUpdateValidator } from '../../middlewares/validators/config' -import { ClientHtml } from '../../lib/client-html' import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' -import { remove, writeJSON } from 'fs-extra' -import { getServerCommit } from '../../helpers/utils' -import validator from 'validator' import { objectConverter } from '../../helpers/core-utils' +import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' +import { getServerCommit } from '../../helpers/utils' import { CONFIG, isEmailEnabled, reloadConfig } from '../../initializers/config' +import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '../../initializers/constants' +import { ClientHtml } from '../../lib/client-html' import { PluginManager } from '../../lib/plugins/plugin-manager' import { getThemeOrDefault } from '../../lib/plugins/theme-utils' -import { Hooks } from '@server/lib/plugins/hooks' +import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' +import { customConfigUpdateValidator } from '../../middlewares/validators/config' const configRouter = express.Router() @@ -79,7 +79,9 @@ async function getConfig (req: express.Request, res: express.Response) { } }, plugin: { - registered: getRegisteredPlugins() + registered: getRegisteredPlugins(), + registeredExternalAuths: getExternalAuthsPlugins(), + registeredIdAndPassAuths: getIdAndPassAuthPlugins() }, theme: { registered: getRegisteredThemes(), @@ -269,6 +271,38 @@ function getRegisteredPlugins () { })) } +function getIdAndPassAuthPlugins () { + const result: RegisteredIdAndPassAuthConfig[] = [] + + for (const p of PluginManager.Instance.getIdAndPassAuths()) { + for (const auth of p.idAndPassAuths) { + result.push({ + npmName: p.npmName, + authName: auth.authName, + weight: auth.getWeight() + }) + } + } + + return result +} + +function getExternalAuthsPlugins () { + const result: RegisteredExternalAuthConfig[] = [] + + for (const p of PluginManager.Instance.getExternalAuths()) { + for (const auth of p.externalAuths) { + result.push({ + npmName: p.npmName, + authName: auth.authName, + authDisplayName: auth.authDisplayName + }) + } + } + + return result +} + // --------------------------------------------------------------------------- export { diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts index 1fc49b646..f12e1c0f5 100644 --- a/server/controllers/plugins.ts +++ b/server/controllers/plugins.ts @@ -2,11 +2,12 @@ import * as express from 'express' import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' import { join } from 'path' import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' -import { getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins' +import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } from '../middlewares/validators/plugins' import { serveThemeCSSValidator } from '../middlewares/validators/themes' import { PluginType } from '../../shared/models/plugins/plugin.type' import { isTestInstance } from '../helpers/core-utils' import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' +import { logger } from '@server/helpers/logger' const sendFileOptions = { maxAge: '30 days', @@ -23,6 +24,12 @@ pluginsRouter.get('/plugins/translations/:locale.json', getPluginTranslations ) +pluginsRouter.get('/plugins/:pluginName/:pluginVersion/auth/:authName', + getPluginValidator(PluginType.PLUGIN), + getExternalAuthValidator, + handleAuthInPlugin +) + pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', getPluginValidator(PluginType.PLUGIN), pluginStaticDirectoryValidator, @@ -134,3 +141,14 @@ function serveThemeCSSDirectory (req: express.Request, res: express.Response) { return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) } + +function handleAuthInPlugin (req: express.Request, res: express.Response) { + const authOptions = res.locals.externalAuth + + try { + logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName) + authOptions.onAuthRequest(req, res) + } catch (err) { + logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName) + } +} -- cgit v1.2.3