fix: use readonly connections for guests (#8221)
Some checks are pending
CI / build (push) Waiting to run
CI / svelte-check (push) Blocked by required conditions
CI / formatting (push) Blocked by required conditions
CI / test (push) Blocked by required conditions
CI / uitest (push) Waiting to run
CI / uitest-pg (push) Waiting to run
CI / uitest-qms (push) Waiting to run
CI / uitest-workspaces (push) Waiting to run
CI / docker-build (push) Blocked by required conditions
CI / dist-build (push) Blocked by required conditions

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov 2025-03-13 17:38:26 +07:00 committed by GitHub
parent 88357fe68a
commit 31bf564ffc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View File

@ -19,7 +19,7 @@ import { decodeToken } from '@hcengineering/server-token'
import { Extension, onAuthenticatePayload } from '@hocuspocus/server'
import { Context, buildContext } from '../context'
import { getWorkspaceIds } from '../utils'
import { getWorkspaceIds, isGuest } from '../utils'
export interface AuthenticationConfiguration {
ctx: MeasureContext
@ -38,8 +38,13 @@ export class AuthenticationExtension implements Extension {
return await ctx.with('authenticate', { workspaceId }, async () => {
const token = decodeToken(data.token)
const readonly = isGuest(token)
ctx.info('authenticate', { workspaceId, mode: token.extra?.mode ?? '' })
ctx.info('authenticate', { workspaceId, mode: token.extra?.mode ?? '', readonly })
if (readonly) {
data.connection.readOnly = true
}
// verify workspace can be accessed with the token
const ids = await getWorkspaceIds(data.token)

View File

@ -12,11 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { type WorkspaceIds } from '@hcengineering/core'
import { GUEST_ACCOUNT } from '@hcengineering/account'
import { getClient as getAccountClient } from '@hcengineering/account-client'
import { type WorkspaceIds } from '@hcengineering/core'
import { type Token } from '@hcengineering/server-token'
import config from './config'
export function isGuest (token: Token): boolean {
return token.account === GUEST_ACCOUNT && token.extra?.guest === 'true'
}
// TODO: consider storing this in a cache for some short period of time
export async function getWorkspaceIds (token: string): Promise<WorkspaceIds> {
const workspaceInfo = await getAccountClient(config.AccountsUrl, token).getWorkspaceInfo()