Authentication token generation WIP (#98)

You can now generate links to join chatrooms with your current user. This can be used to create Docks in OBS for example. This could also be used to generate authentication token to join the chat from 3rd party tools.
This commit is contained in:
John Livingston
2024-06-16 19:48:02 +02:00
parent e83150cf87
commit 90afdafbd9
24 changed files with 988 additions and 205 deletions

View File

@ -2,12 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-only
interface AuthentInfos {
type: 'peertube' | 'oidc'
jid: string
password: string
nickname?: string
}
import type { ProsodyAuthentInfos } from 'shared/lib/types'
interface AuthHeader { [key: string]: string }
@ -15,7 +10,7 @@ async function getLocalAuthentInfos (
authenticationUrl: string,
tryExternalAuth: boolean,
peertubeAuthHeader?: AuthHeader | null
): Promise<false | AuthentInfos> {
): Promise<false | ProsodyAuthentInfos> {
try {
if (authenticationUrl === '') {
console.error('Missing authenticationUrl')
@ -101,7 +96,37 @@ async function getLocalAuthentInfos (
}
}
export {
AuthentInfos,
getLocalAuthentInfos
/**
* Reads the livechat-token if relevant.
* This token can be passed to the page by adding the following hash to the window.location:
* `?j=the_xmpp_id&p=XXXXXXX&n=MyNickname`
*/
function getLivechatTokenAuthInfos (): ProsodyAuthentInfos | undefined {
try {
const hash = window.location.hash
if (!hash || !hash.startsWith('#?')) { return undefined }
// We try to read the hash as a queryString.
const u = new URL('http://localhost' + hash.substring(1))
const jid = u.searchParams.get('j')
const password = u.searchParams.get('p')
if (!jid || !password) { return undefined }
const nickname = u.searchParams.get('n') ?? undefined
return {
type: 'livechat-token',
jid,
password,
nickname
}
} catch (error) {
console.error(error)
return undefined
}
}
export {
ProsodyAuthentInfos,
getLocalAuthentInfos,
getLivechatTokenAuthInfos
}

View File

@ -3,7 +3,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import type { InitConverseJSParams } from 'shared/lib/types'
import type { AuthentInfos } from './auth'
import type { ProsodyAuthentInfos } from './auth'
/**
* Instanciate defaults params to use for ConverseJS.
@ -140,7 +140,7 @@ function defaultConverseParams (
*/
function localRoomAuthenticatedParams (
initConverseParams: InitConverseJSParams,
auth: AuthentInfos, params: any
auth: ProsodyAuthentInfos, params: any
): void {
_fillAuthenticatedParams(initConverseParams, auth, params)
_fillLocalProtocols(initConverseParams, params)
@ -164,7 +164,7 @@ function localRoomAnonymousParams (initConverseParams: InitConverseJSParams, par
*/
function remoteRoomAuthenticatedParams (
initConverseParams: InitConverseJSParams,
auth: AuthentInfos, params: any
auth: ProsodyAuthentInfos, params: any
): void {
_fillAuthenticatedParams(initConverseParams, auth, params)
_fillLocalProtocols(initConverseParams, params)
@ -178,7 +178,7 @@ function remoteRoomAuthenticatedParams (
*/
function remoteRoomAnonymousParams (
initConverseParams: InitConverseJSParams,
auth: AuthentInfos | null,
auth: ProsodyAuthentInfos | null,
params: any
): void {
params.jid = initConverseParams.remoteAnonymousJID
@ -188,7 +188,11 @@ function remoteRoomAnonymousParams (
_fillRemoteProtocols(initConverseParams, params)
}
function _fillAuthenticatedParams (initConverseParams: InitConverseJSParams, auth: AuthentInfos, params: any): void {
function _fillAuthenticatedParams (
initConverseParams: InitConverseJSParams,
auth: ProsodyAuthentInfos,
params: any
): void {
params.authentication = 'login'
params.auto_login = true
params.jid = auth.jid