Refactoring: better naming, to prepare other external authent.

This commit is contained in:
John Livingston 2024-04-19 09:53:23 +02:00
parent cfc5e98d90
commit 7afcbcf1a2
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
10 changed files with 33 additions and 33 deletions

View File

@ -1,4 +1,4 @@
import type { InitConverseJSParams, ChatIncludeMode, OIDCAuthResult } from 'shared/lib/types' import type { InitConverseJSParams, ChatIncludeMode, ExternalAuthResult } from 'shared/lib/types'
import { inIframe } from './lib/utils' import { inIframe } from './lib/utils'
import { initDom } from './lib/dom' import { initDom } from './lib/dom'
import { import {
@ -28,7 +28,7 @@ declare global {
initConversePlugins: typeof initConversePlugins initConversePlugins: typeof initConversePlugins
initConverse: typeof initConverse initConverse: typeof initConverse
reconnectConverse?: (room: string) => void reconnectConverse?: (room: string) => void
oidcGetResult?: (data: OIDCAuthResult) => void externalAuthGetResult?: (data: ExternalAuthResult) => void
} }
} }

View File

@ -18,9 +18,9 @@ class ExternalLoginModal extends BaseModal {
onHide () { onHide () {
super.onHide() super.onHide()
// kill the oidcGetResult handler if still there // kill the externalAuthGetResult handler if still there
try { try {
if (window.oidcGetResult) { window.oidcGetResult() } if (window.externalAuthGetResult) { window.externalAuthGetResult() }
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }

View File

@ -25,12 +25,12 @@ export const tplExternalLoginModal = (el, o) => {
const popup = window.open( const popup = window.open(
externalAuthOIDCUrl, externalAuthOIDCUrl,
'livechat-oidc', 'livechat-external-auth',
'popup' 'popup'
) )
window.oidcGetResult = (data) => { window.externalAuthGetResult = (data) => {
window.oidcGetResult = undefined window.externalAuthGetResult = undefined
if (!data) { if (!data) {
// special case: when this modal is closed, used to close the popup // special case: when this modal is closed, used to close the popup
@ -38,17 +38,17 @@ export const tplExternalLoginModal = (el, o) => {
return return
} }
console.log('Received an OIDC authentication result...', data) console.log('Received an external authentication result...', data)
if (!data.ok) { if (!data.ok) {
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
el.external_auth_oidc_alert_message = __(LOC_login_external_oidc_alert_message) + el.external_auth_oidc_alert_message = __(LOC_login_external_auth_alert_message) +
(data.message ? ` (${data.message})` : '') (data.message ? ` (${data.message})` : '')
return return
} }
console.info('Got external account information', data) console.info('Got external account information', data)
// Storing the token in sessionStorage. // Storing the token in sessionStorage.
window.sessionStorage.setItem('peertube-plugin-livechat-oidc-token', data.token) window.sessionStorage.setItem('peertube-plugin-livechat-external-auth-oidc-token', data.token)
const reconnectMode = api.settings.get('livechat_external_auth_reconnect_mode') const reconnectMode = api.settings.get('livechat_external_auth_reconnect_mode')
if (reconnectMode === 'button-close-open') { if (reconnectMode === 'button-close-open') {

View File

@ -9,7 +9,7 @@ interface AuthHeader { [key: string]: string }
async function getLocalAuthentInfos ( async function getLocalAuthentInfos (
authenticationUrl: string, authenticationUrl: string,
tryOIDC: boolean, tryExternalAuth: boolean,
peertubeAuthHeader?: AuthHeader | null peertubeAuthHeader?: AuthHeader | null
): Promise<false | AuthentInfos> { ): Promise<false | AuthentInfos> {
try { try {
@ -34,7 +34,7 @@ async function getLocalAuthentInfos (
const refreshToken = window.localStorage.getItem('refresh_token') ?? '' const refreshToken = window.localStorage.getItem('refresh_token') ?? ''
if (tokenType === '' && accessToken === '' && refreshToken === '') { if (tokenType === '' && accessToken === '' && refreshToken === '') {
console.info('User seems not to be logged in.') console.info('User seems not to be logged in.')
// We must continue, for OIDC workflow. // We must continue, for External Auth workflow.
peertubeAuthHeader = null peertubeAuthHeader = null
} else { } else {
peertubeAuthHeader = { peertubeAuthHeader = {
@ -43,16 +43,16 @@ async function getLocalAuthentInfos (
} }
} }
let oidcHeaders: any let externalAuthHeaders: any
// When user has used the External OIDC mechanisme to create an account, we got a token in sessionStorage. // When user has used the External Authentication mechanism to create an account, we got a token in sessionStorage.
if (tryOIDC && !peertubeAuthHeader && window.sessionStorage) { if (tryExternalAuth && !peertubeAuthHeader && window.sessionStorage) {
const token = window.sessionStorage.getItem('peertube-plugin-livechat-oidc-token') const token = window.sessionStorage.getItem('peertube-plugin-livechat-external-auth-oidc-token')
if (token && (typeof token === 'string')) { if (token && (typeof token === 'string')) {
oidcHeaders = { 'X-Peertube-Plugin-Livechat-OIDC-Token': token } externalAuthHeaders = { 'X-Peertube-Plugin-Livechat-External-Auth-OIDC-Token': token }
} }
} }
if (peertubeAuthHeader === null && oidcHeaders === undefined) { if (peertubeAuthHeader === null && externalAuthHeaders === undefined) {
console.info('User is not logged in.') console.info('User is not logged in.')
return false return false
} }
@ -63,7 +63,7 @@ async function getLocalAuthentInfos (
Object.assign( Object.assign(
{}, {},
peertubeAuthHeader ?? {}, peertubeAuthHeader ?? {},
oidcHeaders ?? {}, externalAuthHeaders ?? {},
{ {
'content-type': 'application/json;charset=UTF-8' 'content-type': 'application/json;charset=UTF-8'
} }

View File

@ -27,7 +27,7 @@ export const livechatSpecificsPlugin = {
if (!result) { return } if (!result) { return }
// Deleting access token in sessionStorage. // Deleting access token in sessionStorage.
window.sessionStorage.removeItem('peertube-plugin-livechat-oidc-token') window.sessionStorage.removeItem('peertube-plugin-livechat-external-auth-oidc-token')
const reconnectMode = _converse.api.settings.get('livechat_external_auth_reconnect_mode') const reconnectMode = _converse.api.settings.get('livechat_external_auth_reconnect_mode')
if (reconnectMode === 'button-close-open') { if (reconnectMode === 'button-close-open') {

View File

@ -14,7 +14,7 @@ const locKeys = [
'login_remote_peertube_video_not_found', 'login_remote_peertube_video_not_found',
'login_remote_peertube_video_not_found_try_anyway', 'login_remote_peertube_video_not_found_try_anyway',
'login_remote_peertube_video_not_found_try_anyway_button', 'login_remote_peertube_video_not_found_try_anyway_button',
'login_external_oidc_alert_message' 'login_external_auth_alert_message'
] ]
module.exports = locKeys module.exports = locKeys

View File

@ -421,4 +421,4 @@ login_remote_peertube_no_livechat: "The livechat plugin is not installed on this
login_remote_peertube_video_not_found: "This video is not available on this Peertube instance." login_remote_peertube_video_not_found: "This video is not available on this Peertube instance."
login_remote_peertube_video_not_found_try_anyway: "In some cases, the video can still be retrieved if you connect to the remote instance." login_remote_peertube_video_not_found_try_anyway: "In some cases, the video can still be retrieved if you connect to the remote instance."
login_remote_peertube_video_not_found_try_anyway_button: "Try anyway to open the video on the Peertube instance" login_remote_peertube_video_not_found_try_anyway_button: "Try anyway to open the video on the Peertube instance"
login_external_oidc_alert_message: "Authentication failed" login_external_auth_alert_message: "Authentication failed"

View File

@ -18,7 +18,7 @@ async function initAuthApiRouter (options: RegisterServerOptions, router: Router
if (!user) { if (!user) {
// No Peertube user, but perhaps an external authentication? // No Peertube user, but perhaps an external authentication?
const token = req.header('X-Peertube-Plugin-Livechat-OIDC-Token') const token = req.header('X-Peertube-Plugin-Livechat-External-Auth-OIDC-Token')
if (token) { if (token) {
try { try {
const oidc = ExternalAuthOIDC.singleton() const oidc = ExternalAuthOIDC.singleton()

View File

@ -1,6 +1,6 @@
import type { RegisterServerOptions } from '@peertube/peertube-types' import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { Router, Request, Response, NextFunction } from 'express' import type { Router, Request, Response, NextFunction } from 'express'
import type { OIDCAuthResult } from '../../../shared/lib/types' import type { ExternalAuthResult } from '../../../shared/lib/types'
import { asyncMiddleware } from '../middlewares/async' import { asyncMiddleware } from '../middlewares/async'
import { ExternalAuthOIDC } from '../external-auth/oidc' import { ExternalAuthOIDC } from '../external-auth/oidc'
import { ExternalAuthenticationError } from '../external-auth/error' import { ExternalAuthenticationError } from '../external-auth/error'
@ -11,17 +11,17 @@ import { ensureUser } from '../prosody/api/manage-users'
* and send the result to the parent window. * and send the result to the parent window.
* @param result the result to send to the parent window * @param result the result to send to the parent window
*/ */
function popupResultHTML (result: OIDCAuthResult): string { function popupResultHTML (result: ExternalAuthResult): string {
return `<!DOCTYPE html><html> return `<!DOCTYPE html><html>
<body> <body>
<noscript>Your browser must enable javascript for this page to work.</noscript> <noscript>Your browser must enable javascript for this page to work.</noscript>
<script> <script>
try { try {
const data = ${JSON.stringify(result)}; const data = ${JSON.stringify(result)};
if (!window.opener || !window.opener.oidcGetResult) { if (!window.opener || !window.opener.externalAuthGetResult) {
throw new Error("Can't find parent window callback handler.") throw new Error("Can't find parent window callback handler.")
} }
window.opener.oidcGetResult(data); window.opener.externalAuthGetResult(data);
window.close(); window.close();
} catch (err) { } catch (err) {
document.body.innerText = 'Error: ' + err; document.body.innerText = 'Error: ' + err;

View File

@ -107,17 +107,17 @@ type ChatPeertubeIncludeMode = 'peertube-fullpage' | 'peertube-video'
*/ */
type ChatIncludeMode = 'chat-only' | ChatPeertubeIncludeMode type ChatIncludeMode = 'chat-only' | ChatPeertubeIncludeMode
interface OIDCAuthResultOk { interface ExternalAuthResultOk {
ok: true ok: true
token: string token: string
} }
interface OIDCAuthResultError { interface ExternalAuthResultError {
ok: false ok: false
message?: string message?: string
} }
type OIDCAuthResult = OIDCAuthResultError | OIDCAuthResultOk type ExternalAuthResult = ExternalAuthResultError | ExternalAuthResultOk
export type { export type {
ConverseJSTheme, ConverseJSTheme,
@ -130,7 +130,7 @@ export type {
ChannelConfiguration, ChannelConfiguration,
ChatIncludeMode, ChatIncludeMode,
ChatPeertubeIncludeMode, ChatPeertubeIncludeMode,
OIDCAuthResultError, ExternalAuthResultError,
OIDCAuthResultOk, ExternalAuthResultOk,
OIDCAuthResult ExternalAuthResult
} }