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:
@ -12,7 +12,7 @@ import {
|
||||
remoteRoomAnonymousParams,
|
||||
remoteRoomAuthenticatedParams
|
||||
} from './lib/converse-params'
|
||||
import { getLocalAuthentInfos } from './lib/auth'
|
||||
import { getLocalAuthentInfos, getLivechatTokenAuthInfos } from './lib/auth'
|
||||
import { randomNick } from './lib/nick'
|
||||
import { slowModePlugin } from './lib/plugins/slow-mode'
|
||||
import { windowTitlePlugin } from './lib/plugins/window-title'
|
||||
@ -120,7 +120,12 @@ async function initConverse (
|
||||
// OIDC (OpenID Connect):
|
||||
const tryOIDC = (initConverseParams.externalAuthOIDC?.length ?? 0) > 0
|
||||
|
||||
const auth = await getLocalAuthentInfos(authenticationUrl, tryOIDC, peertubeAuthHeader)
|
||||
let auth
|
||||
if (chatIncludeMode === 'chat-only') {
|
||||
// In this mode, we can check if there is a token in the url.
|
||||
auth = getLivechatTokenAuthInfos()
|
||||
}
|
||||
auth ??= await getLocalAuthentInfos(authenticationUrl, tryOIDC, peertubeAuthHeader)
|
||||
|
||||
if (auth) {
|
||||
if (!isRemoteChat) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user