Skip to content

Cannot authenticate with a valid token after failed authentication on the same document #944

@situam

Description

@situam

Description
If a provider attempts to authenticate with a bad token (and fails), a subsequent attempt to authenticate with a good token for the same document does not succeed. The following test demonstrates the issue:

Steps to reproduce the bug

  1. Add test file tests/provider/onAuthenticationFailedRetry.ts:
import test from 'ava'
import { newHocuspocus, newHocuspocusProvider, newHocuspocusProviderWebsocket, sleep } from '../utils/index.ts'

test('onAuthenticate bad token then good token on same doc', async t => {
  const docName = 'superSecretDoc'
  const requiredToken = 'SUPER-SECRET-TOKEN'

  const server = await newHocuspocus({
    async onAuthenticate({ token, documentName }) {
      if (documentName !== docName) {
        throw new Error()
      }

      if (token !== requiredToken) {
        throw new Error()
      }
    },
  })

  const socket = newHocuspocusProviderWebsocket(server)

  const providerFail = newHocuspocusProvider(server, {
    websocketProvider: socket,
    token: 'wrongToken',
    name: docName,
    onAuthenticated() {
      t.fail()
    },
  })

  await sleep(100)
  
  const providerOK = newHocuspocusProvider(server, {
    websocketProvider: socket,
    token: requiredToken,
    name: docName,
    onAuthenticationFailed() {
      t.fail()
    },
  })

  sleep(1000)
  t.is(providerFail.isAuthenticated, false)
  t.is(providerOK.isAuthenticated, true)
})
  1. Run npx ava tests/provider/onAuthenticationFailedRetry.ts

Expected behavior
t.is(providerOK.isAuthenticated, true) should pass

Actual behavior
t.is(providerOK.isAuthenticated, true) fails

Environment?

  • Hocuspocus version: 3.1.1
  • Node.js version: 23.6.0
  • OS: macOS 15.4.1

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions