-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hacky, working, first pass * wip * refactor to use y-protocols approach with binary messages * docs * sp * undo unused changes * More docs * Hook up custom callbacks * tests: test the new onAuthenticate hook and the onAuthenticated and onAuthenticationFailed callbacks * authentication -> token * consts -> enum * cleanup * fix: Add missing object to authentication payload Co-authored-by: Hans Pagel <[email protected]>
- Loading branch information
Showing
27 changed files
with
530 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 46,4 @@ | |
"sass-loader": "^10.1.1", | ||
"style-resources-loader": "^1.4.1" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,64 @@ | ||
# onAuthenticate | ||
|
||
## toc | ||
|
||
## Introduction | ||
|
||
The `onAuthenticate` hook will be called when the server receives an authentication request from the client provider. It should return a Promise. Throwing an exception or rejecting the Promise will terminate the connection. | ||
|
||
## Hook payload | ||
|
||
The `data` passed to the `onAuthenticate` hook has the following attributes: | ||
|
||
```typescript | ||
import { IncomingHttpHeaders } from 'http' | ||
import { URLSearchParams } from 'url' | ||
import { Doc } from 'yjs' | ||
|
||
const data = { | ||
clientsCount: number, | ||
document: Doc, | ||
documentName: string, | ||
requestHeaders: IncomingHttpHeaders, | ||
requestParameters: URLSearchParams, | ||
socketId: string, | ||
token: string, | ||
connection: { | ||
readOnly: boolean, | ||
}, | ||
} | ||
``` | ||
|
||
## Example | ||
|
||
```typescript | ||
import { Server } from '@hocuspocus/server' | ||
|
||
const hocuspocus = Server.configure({ | ||
async onAuthenticate(data) { | ||
const { token } = data | ||
|
||
// Example test if a user is authenticated using a | ||
// request parameter | ||
if (token !== 'super-secret-token') { | ||
throw new Error('Not authorized!') | ||
} | ||
|
||
// Example to set a document to read only for the current user | ||
// thus changes will not be accepted and synced to other clients | ||
if (someCondition === true) { | ||
data.connection.readOnly = true | ||
} | ||
|
||
// You can set contextual data to use it in other hooks | ||
return { | ||
user: { | ||
id: 1234, | ||
name: 'John', | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
hocuspocus.listen() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.