From 7afcbcf1a2f44d4e7c08c208bc5e17f060eed1e1 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 19 Apr 2024 09:53:23 +0200 Subject: [PATCH] Refactoring: better naming, to prepare other external authent. --- conversejs/builtin.ts | 4 ++-- .../shared/modals/livechat-external-login.js | 4 ++-- .../templates/livechat-external-login-modal.js | 12 ++++++------ conversejs/lib/auth.ts | 18 +++++++++--------- conversejs/lib/plugins/livechat-specific.ts | 2 +- conversejs/loc.keys.js | 2 +- languages/en.yml | 2 +- server/lib/routers/api/auth.ts | 2 +- server/lib/routers/oidc.ts | 8 ++++---- shared/lib/types.ts | 12 ++++++------ 10 files changed, 33 insertions(+), 33 deletions(-) diff --git a/conversejs/builtin.ts b/conversejs/builtin.ts index e09943c0..08874057 100644 --- a/conversejs/builtin.ts +++ b/conversejs/builtin.ts @@ -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 { initDom } from './lib/dom' import { @@ -28,7 +28,7 @@ declare global { initConversePlugins: typeof initConversePlugins initConverse: typeof initConverse reconnectConverse?: (room: string) => void - oidcGetResult?: (data: OIDCAuthResult) => void + externalAuthGetResult?: (data: ExternalAuthResult) => void } } diff --git a/conversejs/custom/shared/modals/livechat-external-login.js b/conversejs/custom/shared/modals/livechat-external-login.js index 7d9d74fb..1b9b15f2 100644 --- a/conversejs/custom/shared/modals/livechat-external-login.js +++ b/conversejs/custom/shared/modals/livechat-external-login.js @@ -18,9 +18,9 @@ class ExternalLoginModal extends BaseModal { onHide () { super.onHide() - // kill the oidcGetResult handler if still there + // kill the externalAuthGetResult handler if still there try { - if (window.oidcGetResult) { window.oidcGetResult() } + if (window.externalAuthGetResult) { window.externalAuthGetResult() } } catch (err) { console.error(err) } diff --git a/conversejs/custom/templates/livechat-external-login-modal.js b/conversejs/custom/templates/livechat-external-login-modal.js index d55516bb..c63bb8af 100644 --- a/conversejs/custom/templates/livechat-external-login-modal.js +++ b/conversejs/custom/templates/livechat-external-login-modal.js @@ -25,12 +25,12 @@ export const tplExternalLoginModal = (el, o) => { const popup = window.open( externalAuthOIDCUrl, - 'livechat-oidc', + 'livechat-external-auth', 'popup' ) - window.oidcGetResult = (data) => { - window.oidcGetResult = undefined + window.externalAuthGetResult = (data) => { + window.externalAuthGetResult = undefined if (!data) { // special case: when this modal is closed, used to close the popup @@ -38,17 +38,17 @@ export const tplExternalLoginModal = (el, o) => { return } - console.log('Received an OIDC authentication result...', data) + console.log('Received an external authentication result...', data) if (!data.ok) { // 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})` : '') return } console.info('Got external account information', data) // 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') if (reconnectMode === 'button-close-open') { diff --git a/conversejs/lib/auth.ts b/conversejs/lib/auth.ts index 9745e5c3..14d67809 100644 --- a/conversejs/lib/auth.ts +++ b/conversejs/lib/auth.ts @@ -9,7 +9,7 @@ interface AuthHeader { [key: string]: string } async function getLocalAuthentInfos ( authenticationUrl: string, - tryOIDC: boolean, + tryExternalAuth: boolean, peertubeAuthHeader?: AuthHeader | null ): Promise { try { @@ -34,7 +34,7 @@ async function getLocalAuthentInfos ( const refreshToken = window.localStorage.getItem('refresh_token') ?? '' if (tokenType === '' && accessToken === '' && refreshToken === '') { console.info('User seems not to be logged in.') - // We must continue, for OIDC workflow. + // We must continue, for External Auth workflow. peertubeAuthHeader = null } else { peertubeAuthHeader = { @@ -43,16 +43,16 @@ async function getLocalAuthentInfos ( } } - let oidcHeaders: any - // When user has used the External OIDC mechanisme to create an account, we got a token in sessionStorage. - if (tryOIDC && !peertubeAuthHeader && window.sessionStorage) { - const token = window.sessionStorage.getItem('peertube-plugin-livechat-oidc-token') + let externalAuthHeaders: any + // When user has used the External Authentication mechanism to create an account, we got a token in sessionStorage. + if (tryExternalAuth && !peertubeAuthHeader && window.sessionStorage) { + const token = window.sessionStorage.getItem('peertube-plugin-livechat-external-auth-oidc-token') 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.') return false } @@ -63,7 +63,7 @@ async function getLocalAuthentInfos ( Object.assign( {}, peertubeAuthHeader ?? {}, - oidcHeaders ?? {}, + externalAuthHeaders ?? {}, { 'content-type': 'application/json;charset=UTF-8' } diff --git a/conversejs/lib/plugins/livechat-specific.ts b/conversejs/lib/plugins/livechat-specific.ts index 3020f1d1..bbedc6e4 100644 --- a/conversejs/lib/plugins/livechat-specific.ts +++ b/conversejs/lib/plugins/livechat-specific.ts @@ -27,7 +27,7 @@ export const livechatSpecificsPlugin = { if (!result) { return } // 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') if (reconnectMode === 'button-close-open') { diff --git a/conversejs/loc.keys.js b/conversejs/loc.keys.js index ac16a300..1eb159e1 100644 --- a/conversejs/loc.keys.js +++ b/conversejs/loc.keys.js @@ -14,7 +14,7 @@ const locKeys = [ 'login_remote_peertube_video_not_found', 'login_remote_peertube_video_not_found_try_anyway', 'login_remote_peertube_video_not_found_try_anyway_button', - 'login_external_oidc_alert_message' + 'login_external_auth_alert_message' ] module.exports = locKeys diff --git a/languages/en.yml b/languages/en.yml index 2f3fc312..11783b9b 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -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_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_external_oidc_alert_message: "Authentication failed" +login_external_auth_alert_message: "Authentication failed" diff --git a/server/lib/routers/api/auth.ts b/server/lib/routers/api/auth.ts index 221dc2b9..c8237e85 100644 --- a/server/lib/routers/api/auth.ts +++ b/server/lib/routers/api/auth.ts @@ -18,7 +18,7 @@ async function initAuthApiRouter (options: RegisterServerOptions, router: Router if (!user) { // 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) { try { const oidc = ExternalAuthOIDC.singleton() diff --git a/server/lib/routers/oidc.ts b/server/lib/routers/oidc.ts index 4deb161c..a9b3a6e2 100644 --- a/server/lib/routers/oidc.ts +++ b/server/lib/routers/oidc.ts @@ -1,6 +1,6 @@ import type { RegisterServerOptions } from '@peertube/peertube-types' 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 { ExternalAuthOIDC } from '../external-auth/oidc' 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. * @param result the result to send to the parent window */ -function popupResultHTML (result: OIDCAuthResult): string { +function popupResultHTML (result: ExternalAuthResult): string { return `