-
Notifications
You must be signed in to change notification settings - Fork 235
feat: Add Nip42 handler #629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| feat: NIP-42 AUTH handler and WebSocket session wiring |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,86 @@ | ||
| import { EventKinds, EventTags } from '../constants/base' | ||
| import { isEventIdValid, isEventSignatureValid } from '../utils/event' | ||
| import { AuthMessage } from '../@types/messages' | ||
| import { createCommandResult } from '../utils/messages' | ||
| import { createLogger } from '../factories/logger-factory' | ||
| import { IMessageHandler } from '../@types/message-handlers' | ||
| import { IWebSocketAdapter } from '../@types/adapters' | ||
| import { Settings } from '../@types/settings' | ||
| import { WebSocketAdapterEvent } from '../constants/adapter' | ||
|
|
||
| const logger = createLogger('auth-message-handler') | ||
|
|
||
| const AUTH_EVENT_KIND = EventKinds.AUTH // 22242 | ||
| const MAX_TIMESTAMP_DELTA_SECONDS = 600 // 10 minutes | ||
|
|
||
| export class AuthMessageHandler implements IMessageHandler { | ||
| public constructor( | ||
| private readonly webSocket: IWebSocketAdapter, | ||
| private readonly settings: () => Settings, | ||
| ) {} | ||
|
|
||
| public async handleMessage(message: AuthMessage): Promise<void> { | ||
| const event = message[1] | ||
|
|
||
| if (event.kind !== AUTH_EVENT_KIND) { | ||
| this.sendResult(event.id, false, 'invalid: auth event must be kind 22242') | ||
| return | ||
| } | ||
|
|
||
| if (!(await isEventIdValid(event))) { | ||
| this.sendResult(event.id, false, 'invalid: event id does not match') | ||
| return | ||
| } | ||
|
|
||
| if (!(await isEventSignatureValid(event))) { | ||
| this.sendResult(event.id, false, 'invalid: event signature verification failed') | ||
| return | ||
| } | ||
|
|
||
| const now = Math.floor(Date.now() / 1000) | ||
| const delta = Math.abs(now - event.created_at) | ||
| if (delta > MAX_TIMESTAMP_DELTA_SECONDS) { | ||
| this.sendResult(event.id, false, 'invalid: created_at is too far from the current time') | ||
| return | ||
| } | ||
|
|
||
| const challengeTag = event.tags.find( | ||
| (tag) => tag.length >= 2 && tag[0] === EventTags.Challenge, | ||
| ) | ||
| if (!challengeTag || challengeTag[1] !== this.webSocket.getChallenge()) { | ||
| this.sendResult(event.id, false, 'invalid: challenge does not match') | ||
| return | ||
| } | ||
|
|
||
| const relayTag = event.tags.find( | ||
| (tag) => tag.length >= 2 && tag[0] === EventTags.AuthRelay, | ||
| ) | ||
| const relayUrl = this.settings().info.relay_url | ||
| if (!relayTag || !this.isRelayUrlMatch(relayTag[1], relayUrl)) { | ||
| this.sendResult(event.id, false, 'invalid: relay url does not match') | ||
| return | ||
| } | ||
|
|
||
| logger('client %s authenticated as %s', this.webSocket.getClientId(), event.pubkey) | ||
| this.webSocket.addAuthenticatedPubkey(event.pubkey) | ||
| this.sendResult(event.id, true, '') | ||
| } | ||
|
|
||
| private sendResult(eventId: string, success: boolean, message: string): void { | ||
| this.webSocket.emit( | ||
| WebSocketAdapterEvent.Message, | ||
| createCommandResult(eventId, success, message), | ||
| ) | ||
| } | ||
|
|
||
| // NIP-42 says domain-match is sufficient for relay URL comparison | ||
| private isRelayUrlMatch(clientRelay: string, serverRelay: string): boolean { | ||
| try { | ||
| const clientHost = new URL(clientRelay).hostname.toLowerCase() | ||
|
cameri marked this conversation as resolved.
|
||
| const serverHost = new URL(serverRelay).hostname.toLowerCase() | ||
| return clientHost === serverHost | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.