mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-02-25 18:55:32 -06:00
Begin support for external auths
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user