2020-04-30 12:16:20 +00:00
|
|
|
const openidModule = require('openid-client')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
|
|
|
|
const store = {
|
|
|
|
client: null,
|
|
|
|
userAuthenticated: null,
|
|
|
|
secretKey: null,
|
2020-04-30 13:10:20 +00:00
|
|
|
redirectUrl: null,
|
|
|
|
authDisplayName: 'OpenID Connect'
|
2020-04-30 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const encryptionOptions = {
|
|
|
|
algorithm: 'aes256',
|
|
|
|
inputEncoding: 'utf8',
|
|
|
|
outputEncoding: 'hex'
|
|
|
|
}
|
|
|
|
|
2020-12-29 14:01:41 +00:00
|
|
|
const cookieNamePrefix = 'plugin-auth-openid-code-verifier-'
|
2020-04-30 12:16:20 +00:00
|
|
|
|
|
|
|
async function register ({
|
|
|
|
registerExternalAuth,
|
|
|
|
unregisterExternalAuth,
|
|
|
|
registerSetting,
|
|
|
|
settingsManager,
|
|
|
|
peertubeHelpers,
|
|
|
|
getRouter
|
|
|
|
}) {
|
|
|
|
const { logger } = peertubeHelpers
|
|
|
|
|
2020-04-30 13:10:20 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'auth-display-name',
|
|
|
|
label: 'Auth display name',
|
|
|
|
type: 'input',
|
|
|
|
private: true,
|
|
|
|
default: 'OpenID Connect'
|
|
|
|
})
|
|
|
|
|
2020-04-30 12:16:20 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'discover-url',
|
|
|
|
label: 'Discover URL',
|
|
|
|
type: 'input',
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
|
|
|
|
registerSetting({
|
|
|
|
name: 'client-id',
|
|
|
|
label: 'Client ID',
|
|
|
|
type: 'input',
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
|
|
|
|
registerSetting({
|
|
|
|
name: 'client-secret',
|
|
|
|
label: 'Client secret',
|
|
|
|
type: 'input',
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
|
2020-05-05 09:29:37 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'scope',
|
|
|
|
label: 'Scope',
|
|
|
|
type: 'input',
|
|
|
|
private: true,
|
|
|
|
default: 'openid email profile'
|
|
|
|
})
|
|
|
|
|
2020-04-30 12:16:20 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'username-property',
|
|
|
|
label: 'Username property',
|
|
|
|
type: 'input',
|
|
|
|
private: true,
|
|
|
|
default: 'preferred_username'
|
|
|
|
})
|
|
|
|
|
|
|
|
registerSetting({
|
|
|
|
name: 'mail-property',
|
|
|
|
label: 'Email property',
|
|
|
|
type: 'input',
|
|
|
|
private: true,
|
|
|
|
default: 'email'
|
|
|
|
})
|
|
|
|
|
|
|
|
registerSetting({
|
|
|
|
name: 'display-name-property',
|
|
|
|
label: 'Display name property',
|
|
|
|
type: 'input',
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
|
|
|
|
registerSetting({
|
|
|
|
name: 'role-property',
|
|
|
|
label: 'Role property',
|
|
|
|
type: 'input',
|
|
|
|
private: true
|
|
|
|
})
|
|
|
|
|
2021-06-30 12:32:30 +00:00
|
|
|
registerSetting({
|
|
|
|
name: 'group-property',
|
|
|
|
label: 'Group property',
|
|
|
|
type: 'input',
|
|
|
|
private: true,
|
2021-06-30 14:47:04 +00:00
|
|
|
descriptionHTML: 'Property/claim that contains user groups (array)'
|
2021-06-30 12:32:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
registerSetting({
|
|
|
|
name: 'allowed-group',
|
|
|
|
label: 'Allowed group',
|
|
|
|
type: 'input',
|
|
|
|
private: true,
|
|
|
|
descriptionHTML: 'Will only allow login for users whose group array contains this group'
|
|
|
|
})
|
|
|
|
|
2020-04-30 12:16:20 +00:00
|
|
|
const router = getRouter()
|
2020-04-30 13:40:52 +00:00
|
|
|
router.use('/code-cb', (req, res) => handleCb(peertubeHelpers, settingsManager, req, res))
|
2020-04-30 12:16:20 +00:00
|
|
|
|
2020-04-30 13:40:52 +00:00
|
|
|
store.redirectUrl = peertubeHelpers.config.getWebserverUrl() + '/plugins/auth-openid-connect/router/code-cb'
|
2020-04-30 12:16:20 +00:00
|
|
|
|
|
|
|
const secretKeyBuf = await getRandomBytes(16)
|
|
|
|
store.secretKey = secretKeyBuf.toString('hex')
|
|
|
|
|
2020-04-30 13:10:20 +00:00
|
|
|
settingsManager.onSettingsChange(settings => {
|
2020-04-30 12:16:20 +00:00
|
|
|
loadSettingsAndCreateClient(registerExternalAuth, unregisterExternalAuth, peertubeHelpers, settingsManager)
|
|
|
|
.catch(err => logger.error('Cannot load settings and create client after settings changes.', { err }))
|
2020-04-30 13:10:20 +00:00
|
|
|
|
|
|
|
if (settings['auth-display-name']) store.authDisplayName = settings['auth-display-name']
|
2020-04-30 12:16:20 +00:00
|
|
|
})
|
2020-05-05 09:29:37 +00:00
|
|
|
|
2021-12-23 10:25:54 +00:00
|
|
|
try {
|
|
|
|
await loadSettingsAndCreateClient(registerExternalAuth, unregisterExternalAuth, peertubeHelpers, settingsManager)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot load settings and create open id client.', { err })
|
|
|
|
}
|
|
|
|
|
2020-05-05 09:29:37 +00:00
|
|
|
store.authDisplayName = await settingsManager.getSetting('auth-display-name')
|
2020-04-30 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function unregister () {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
register,
|
|
|
|
unregister
|
|
|
|
}
|
|
|
|
|
|
|
|
// ############################################################################
|
|
|
|
|
|
|
|
async function loadSettingsAndCreateClient (registerExternalAuth, unregisterExternalAuth, peertubeHelpers, settingsManager) {
|
2021-12-23 15:22:43 +00:00
|
|
|
const { logger } = peertubeHelpers
|
2020-04-30 12:16:20 +00:00
|
|
|
|
|
|
|
if (store.client) {
|
2020-04-30 13:10:20 +00:00
|
|
|
unregisterExternalAuth('openid-connect')
|
2020-04-30 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
store.client = null
|
|
|
|
store.userAuthenticated = null
|
|
|
|
|
|
|
|
const settings = await settingsManager.getSettings([
|
2020-05-05 09:29:37 +00:00
|
|
|
'scope',
|
2020-04-30 12:16:20 +00:00
|
|
|
'discover-url',
|
|
|
|
'client-id',
|
|
|
|
'client-secret'
|
|
|
|
])
|
|
|
|
|
|
|
|
if (!settings['discover-url']) {
|
|
|
|
logger.info('Do not register external openid auth because discover URL is not set.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!settings['client-id']) {
|
|
|
|
logger.info('Do not register external openid auth because client ID is not set.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const discoverUrl = settings['discover-url']
|
|
|
|
const issuer = await openidModule.Issuer.discover(discoverUrl)
|
|
|
|
|
|
|
|
logger.debug('Discovered issuer %s.', discoverUrl)
|
|
|
|
|
|
|
|
const clientOptions = {
|
|
|
|
client_id: settings['client-id'],
|
|
|
|
redirect_uris: [ store.redirectUrl ],
|
|
|
|
response_types: [ 'code' ]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings['client-secret']) {
|
|
|
|
clientOptions.client_secret = settings['client-secret']
|
|
|
|
} else {
|
|
|
|
clientOptions.token_endpoint_auth_method = 'none'
|
|
|
|
}
|
|
|
|
|
|
|
|
store.client = new issuer.Client(clientOptions)
|
|
|
|
|
|
|
|
const result = registerExternalAuth({
|
|
|
|
authName: 'openid-connect',
|
2020-04-30 13:10:20 +00:00
|
|
|
authDisplayName: () => store.authDisplayName,
|
2020-04-30 12:16:20 +00:00
|
|
|
onAuthRequest: async (req, res) => {
|
|
|
|
try {
|
|
|
|
const codeVerifier = openidModule.generators.codeVerifier()
|
|
|
|
const codeChallenge = openidModule.generators.codeChallenge(codeVerifier)
|
2020-12-29 14:01:41 +00:00
|
|
|
const state = openidModule.generators.state()
|
2020-04-30 12:16:20 +00:00
|
|
|
|
|
|
|
const redirectUrl = store.client.authorizationUrl({
|
2020-05-05 09:29:37 +00:00
|
|
|
scope: settings['scope'],
|
2020-04-30 12:16:20 +00:00
|
|
|
response_mode: 'form_post',
|
|
|
|
code_challenge: codeChallenge,
|
2020-12-29 14:01:41 +00:00
|
|
|
code_challenge_method: 'S256',
|
|
|
|
state,
|
2020-04-30 12:16:20 +00:00
|
|
|
})
|
|
|
|
|
2020-12-29 14:23:16 +00:00
|
|
|
const cookieOptions = {
|
2021-12-23 15:22:43 +00:00
|
|
|
secure: true,
|
2020-12-29 14:01:41 +00:00
|
|
|
httpOnly: true,
|
|
|
|
sameSite: 'none',
|
|
|
|
maxAge: 1000 * 60 * 10 // 10 minutes
|
2020-12-29 14:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const encryptedCodeVerifier = await encrypt(codeVerifier)
|
|
|
|
res.cookie(cookieNamePrefix + 'code-verifier', encryptedCodeVerifier, cookieOptions)
|
2020-12-29 14:01:41 +00:00
|
|
|
|
|
|
|
const encryptedState = await encrypt(state)
|
2020-12-29 14:23:16 +00:00
|
|
|
res.cookie(cookieNamePrefix + 'state', encryptedState, cookieOptions)
|
2020-04-30 12:16:20 +00:00
|
|
|
|
|
|
|
return res.redirect(redirectUrl)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot handle auth request.', { err })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
store.userAuthenticated = result.userAuthenticated
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleCb (peertubeHelpers, settingsManager, req, res) {
|
|
|
|
const { logger } = peertubeHelpers
|
|
|
|
|
|
|
|
if (!store.userAuthenticated) {
|
|
|
|
logger.info('Received callback but cannot userAuthenticated function does not exist.')
|
2020-04-30 12:58:31 +00:00
|
|
|
return onCBError(res)
|
2020-04-30 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 14:01:41 +00:00
|
|
|
const encryptedCodeVerifier = req.cookies[cookieNamePrefix + 'code-verifier']
|
2020-04-30 12:16:20 +00:00
|
|
|
if (!encryptedCodeVerifier) {
|
2020-12-29 14:01:41 +00:00
|
|
|
logger.error('Received callback but code verifier not found in request cookies.')
|
|
|
|
return onCBError(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
const encryptedState = req.cookies[cookieNamePrefix + 'state']
|
|
|
|
if (!encryptedState) {
|
|
|
|
logger.error('Received callback but state not found in request cookies.')
|
2020-04-30 12:58:31 +00:00
|
|
|
return onCBError(res)
|
2020-04-30 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const codeVerifier = await decrypt(encryptedCodeVerifier)
|
2020-12-29 14:01:41 +00:00
|
|
|
const state = await decrypt(encryptedState)
|
2020-04-30 12:16:20 +00:00
|
|
|
|
|
|
|
const params = store.client.callbackParams(req)
|
2020-12-29 14:01:41 +00:00
|
|
|
const tokenSet = await store.client.callback(store.redirectUrl, params, {
|
|
|
|
code_verifier: codeVerifier,
|
|
|
|
state,
|
|
|
|
})
|
2020-04-30 12:16:20 +00:00
|
|
|
|
|
|
|
const accessToken = tokenSet.access_token
|
|
|
|
const userInfo = await store.client.userinfo(accessToken)
|
|
|
|
|
|
|
|
const settings = await settingsManager.getSettings([
|
|
|
|
'mail-property',
|
|
|
|
'username-property',
|
|
|
|
'display-name-property',
|
2021-06-30 12:32:30 +00:00
|
|
|
'role-property',
|
|
|
|
'group-property',
|
|
|
|
'allowed-group'
|
2020-04-30 12:16:20 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
logger.debug('Got userinfo from openid auth.', { userInfo, settings })
|
|
|
|
|
|
|
|
let role
|
|
|
|
if (settings['role-property']) {
|
2021-02-26 15:48:55 +00:00
|
|
|
let roleToParse = userInfo[settings['role-property']]
|
|
|
|
if (Array.isArray(roleToParse)) roleToParse = roleToParse[0]
|
|
|
|
|
|
|
|
role = parseInt('' + roleToParse, 10)
|
|
|
|
|
|
|
|
if (isNaN(role)) {
|
|
|
|
logger.error('Cannot load role ' + roleToParse + ' from OpenID: not a number.')
|
|
|
|
role = null
|
2020-08-08 12:40:34 +00:00
|
|
|
}
|
2020-04-30 12:16:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 12:32:30 +00:00
|
|
|
if (settings['group-property'] && settings['allowed-group']) {
|
2021-06-30 14:47:04 +00:00
|
|
|
const groups = userInfo[settings['group-property']]
|
|
|
|
|
|
|
|
if (!groups.includes(settings['allowed-group'])) {
|
2021-06-30 12:32:30 +00:00
|
|
|
throw {
|
|
|
|
name: "AllowedGroupNotFound",
|
|
|
|
message: "User is not in allowed group"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:16:20 +00:00
|
|
|
let displayName
|
|
|
|
if (settings['display-name-property']) {
|
|
|
|
displayName = userInfo[settings['display-name-property']]
|
|
|
|
}
|
|
|
|
|
|
|
|
let username = userInfo[settings['username-property']] || ''
|
|
|
|
username = username.replace(/[^a-z0-9._]/g, '_')
|
|
|
|
|
|
|
|
store.userAuthenticated({
|
|
|
|
res,
|
|
|
|
req,
|
|
|
|
username,
|
|
|
|
email: userInfo[settings['mail-property']],
|
|
|
|
displayName,
|
|
|
|
role
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Error in handle callback.', { err })
|
2020-04-30 12:58:31 +00:00
|
|
|
onCBError(res)
|
2020-04-30 12:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:58:31 +00:00
|
|
|
function onCBError (res) {
|
|
|
|
res.redirect('/login?externalAuthError=true')
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:16:20 +00:00
|
|
|
async function encrypt (data) {
|
|
|
|
const { algorithm, inputEncoding, outputEncoding } = encryptionOptions
|
|
|
|
|
|
|
|
const iv = await getRandomBytes(16)
|
|
|
|
|
|
|
|
const cipher = crypto.createCipheriv(algorithm, store.secretKey, iv)
|
|
|
|
let encrypted = cipher.update(data, inputEncoding, outputEncoding)
|
|
|
|
encrypted += cipher.final(outputEncoding)
|
|
|
|
|
|
|
|
return iv.toString(outputEncoding) + ':' + encrypted
|
|
|
|
}
|
|
|
|
|
|
|
|
async function decrypt (data) {
|
|
|
|
const { algorithm, inputEncoding, outputEncoding } = encryptionOptions
|
|
|
|
|
|
|
|
const encryptedArray = data.split(':')
|
|
|
|
const iv = Buffer.from(encryptedArray[0], outputEncoding)
|
|
|
|
const encrypted = Buffer.from(encryptedArray[1], outputEncoding)
|
|
|
|
const decipher = crypto.createDecipheriv(algorithm, store.secretKey, iv)
|
|
|
|
|
|
|
|
return decipher.update(encrypted, outputEncoding, inputEncoding) + decipher.final(inputEncoding)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomBytes (size) {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
crypto.randomBytes(size, (err, buf) => {
|
|
|
|
if (err) return rej(err)
|
|
|
|
|
|
|
|
return res(buf)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|