diff options
| -rw-r--r-- | 41.md | 69 |
1 files changed, 69 insertions, 0 deletions
| @@ -0,0 +1,69 @@ | |||
| 1 | NIP-41 | ||
| 2 | ====== | ||
| 3 | |||
| 4 | Authentication of clients to relays | ||
| 5 | ----------------------------------- | ||
| 6 | |||
| 7 | `draft` `optional` `author:Semisol` | ||
| 8 | |||
| 9 | This NIP defines a way for clients to authenticate to relays by signing an ephemeral event. | ||
| 10 | |||
| 11 | ## Event format | ||
| 12 | |||
| 13 | An event should be signed with `kind: 22241` and `content` being the WebSockets URL of the relay. | ||
| 14 | The URL MUST: | ||
| 15 | - Have a trailing slash if the path is `/` | ||
| 16 | - Not have a port number if protocol is `ws` and port is `80` or protocol is `wss` and port is `443` | ||
| 17 | - Not include the search/query | ||
| 18 | |||
| 19 | An example event is shown below: | ||
| 20 | ```json | ||
| 21 | { | ||
| 22 | "id": "...", | ||
| 23 | "pubkey": "...", | ||
| 24 | "created_at": 1669695536, | ||
| 25 | "kind": 22223, | ||
| 26 | "tags": [], | ||
| 27 | "content": "wss://relay.example.com/", | ||
| 28 | "sig": "..." | ||
| 29 | } | ||
| 30 | ``` | ||
| 31 | |||
| 32 | ## Commands between the relay and the client | ||
| 33 | |||
| 34 | This NIP defines a new command, `AUTH`, which can be sent by either the relay or the client with different meanings. | ||
| 35 | |||
| 36 | A relay may send `["AUTH", <human readable reason>, <data object>]` when it needs authentication (examples: accessing kind 4 events, whitelist only, requiring proof of authorship for `EVENT`). | ||
| 37 | The human readable reason SHOULD be prefixed with a string in the format `<short desc string>: <description>`. A list of short descriptions is listed below: | ||
| 38 | - `restricted`: This relay is restricted and requires the pubkey of the client to check if it can access the relay. (requires a whitelist, payment, etc) | ||
| 39 | - `publish`: The relay requests that the client identify who is sending an `EVENT` command. | ||
| 40 | This can be used for where only the signer of an event can publish it, or a pay-as-you-go relay allowing for you to publish others' events | ||
| 41 | - `private`: The client has attempted to access a restricted set of events (example: kind 4 DMs) and should authenticate with the relay to receive them. | ||
| 42 | - `other`: Any reason not defined here. | ||
| 43 | |||
| 44 | `data object` MUST be a JSON object. It currently has one defined field, but may be extended by amendments to this NIP or other NIPs: | ||
| 45 | - `subscription_id`: The subscription ID that triggered the `AUTH` request. | ||
| 46 | |||
| 47 | A client may send one of the following to the relay: | ||
| 48 | - `["AUTH", <signed event>]` to indicate it has accepted the request. This may also be sent without an authentication request. | ||
| 49 | - `["AUTH", null]` to indicate it has rejected the request. | ||
| 50 | |||
| 51 | A relay SHOULD send the [`OK`](https://github.com/nostr-protocol/nips/blob/master/20.md) command after they receive a | ||
| 52 | non-rejecting authentication response, and use one of the following `message` prefixes if the event sent cannot be verified: | ||
| 53 | - `too-old:`: The event signed has a too old `created_at` date. | ||
| 54 | - `invalid-url:`: The URL in `content` is not matching. | ||
| 55 | - `already-used:`: This event was already used to authenticate to this relay. | ||
| 56 | - `bad-signature:`: The event has a bad signature. | ||
| 57 | |||
| 58 | Please note that the `OK` message should only be sent as a response to other commands that the client sends instead of the `AUTH` command if the issue is not related to the authentication event being incorrectly signed (example: not on whitelist). | ||
| 59 | |||
| 60 | Relays SHOULD send [`EOSE`](https://github.com/nostr-protocol/nips/blob/master/15.md) when an authentication request is triggered by a `REQ` command, and not send stored events after the `EOSE` when authentication is completed. | ||
| 61 | Relays SHOULD send `OK` as a response when a command triggers authentication with the reason starting with `auth:`. | ||
| 62 | |||
| 63 | Clients SHOULD retry the action (resending event, resubscribing) after they authenticate if they receive an `AUTH` request. | ||
| 64 | |||
| 65 | ## Signed event verification | ||
| 66 | Relays when processing `AUTH` responses SHOULD verify the following before accepting the response: | ||
| 67 | - that the `kind` is `22223` | ||
| 68 | - that the event was recently signed (~10 minutes, by `created_at`) | ||
| 69 | - that the `content` field matches the relay URL | ||