diff options
| author | fiatjaf <fiatjaf@gmail.com> | 2023-01-07 19:53:42 -0300 |
|---|---|---|
| committer | fiatjaf <fiatjaf@gmail.com> | 2023-01-07 19:53:42 -0300 |
| commit | 6a70967f0eb6e7b00fc811e34631f8db046d6e1f (patch) | |
| tree | e13d666f889487585d00dc64cbd590c570e77e10 /42.md | |
| parent | 50faceef0965042714e7f50a45b102f35c5231ca (diff) | |
add challenge from relay.
Diffstat (limited to '42.md')
| -rw-r--r-- | 42.md | 37 |
1 files changed, 27 insertions, 10 deletions
| @@ -19,16 +19,23 @@ A relay may want to require clients to authenticate to access restricted resourc | |||
| 19 | before clients can query for that kind. | 19 | before clients can query for that kind. |
| 20 | - A relay may limit subscriptions of any kind to paying users or users whitelisted through any other means, and require authentication. | 20 | - A relay may limit subscriptions of any kind to paying users or users whitelisted through any other means, and require authentication. |
| 21 | 21 | ||
| 22 | ## Protocol flow | 22 | ## Definitions |
| 23 | |||
| 24 | This NIP defines a new message, `AUTH`, which relays can send when they support authentication and clients can send to relays when they want | ||
| 25 | to authenticate. When sent by relays, the message is of the following form: | ||
| 26 | |||
| 27 | ``` | ||
| 28 | ["AUTH", <challenge-string>] | ||
| 29 | ``` | ||
| 23 | 30 | ||
| 24 | This NIP defines a new message, `AUTH`, which clients can send to relays when they want to authenticate. The message is of the following form: | 31 | And, when sent by clients, of the following form: |
| 25 | 32 | ||
| 26 | ``` | 33 | ``` |
| 27 | ["AUTH", <signed-event>] | 34 | ["AUTH", <signed-event-json>] |
| 28 | ``` | 35 | ``` |
| 29 | 36 | ||
| 30 | The signed event is an ephemeral event not meant to be published or queried, it must be of `kind: 22242` and content must be set to the | 37 | The signed event is an ephemeral event not meant to be published or queried, it must be of `kind: 22242` and it should have at least two tags, |
| 31 | WebSocket URL of the relay. `created_at` should be the current time. Example: | 38 | one for the relay URL and one for the challenge string as received from the relay. `created_at` should be the current time. Example: |
| 32 | 39 | ||
| 33 | ```json | 40 | ```json |
| 34 | { | 41 | { |
| @@ -36,18 +43,27 @@ WebSocket URL of the relay. `created_at` should be the current time. Example: | |||
| 36 | "pubkey": "...", | 43 | "pubkey": "...", |
| 37 | "created_at": 1669695536, | 44 | "created_at": 1669695536, |
| 38 | "kind": 22242, | 45 | "kind": 22242, |
| 39 | "tags": [], | 46 | "tags": [ |
| 40 | "content": "wss://relay.example.com/", | 47 | ["relay", "wss://relay.example.com/"], |
| 48 | ["challenge", "challengestringhere"] | ||
| 49 | ], | ||
| 50 | "content": "", | ||
| 41 | "sig": "..." | 51 | "sig": "..." |
| 42 | } | 52 | } |
| 43 | ``` | 53 | ``` |
| 44 | 54 | ||
| 55 | ## Protocol flow | ||
| 56 | |||
| 57 | At any moment the relay may send an `AUTH` message to the client containing a challenge. After receiving that the client may decide to | ||
| 58 | authenticate itself or not. The challenge is expected to be valid for the duration of the connection or until a next challenge is sent by | ||
| 59 | the relay. | ||
| 60 | |||
| 45 | The client may send an auth message right before performing an action for which it knows authentication will be required -- for example, right | 61 | The client may send an auth message right before performing an action for which it knows authentication will be required -- for example, right |
| 46 | before requesting `kind: 4` chat messages --, or it may do right on connection start or at some other moment it deems best. The authentication | 62 | before requesting `kind: 4` chat messages --, or it may do right on connection start or at some other moment it deems best. The authentication |
| 47 | is expected to last for the duration of the WebSocket connection. | 63 | is expected to last for the duration of the WebSocket connection. |
| 48 | 64 | ||
| 49 | Upon receiving a message from an unauthenticated user it can't fulfill without authentication, a relay may choose to notify the client. For | 65 | Upon receiving a message from an unauthenticated user it can't fulfill without authentication, a relay may choose to notify the client. For |
| 50 | that it can use a `NOTICE` message with a standard prefix `"restricted: "` that is readable both by humans and machines, for example: | 66 | that it can use a `NOTICE` or `OK` message with a standard prefix `"restricted: "` that is readable both by humans and machines, for example: |
| 51 | 67 | ||
| 52 | ``` | 68 | ``` |
| 53 | ["NOTICE", "restricted: we can't serve DMs to unauthenticated users, does your client implement NIP-42?"] | 69 | ["NOTICE", "restricted: we can't serve DMs to unauthenticated users, does your client implement NIP-42?"] |
| @@ -56,7 +72,7 @@ that it can use a `NOTICE` message with a standard prefix `"restricted: "` that | |||
| 56 | or it can return an `OK` message noting the reason an event was not written using the same prefix: | 72 | or it can return an `OK` message noting the reason an event was not written using the same prefix: |
| 57 | 73 | ||
| 58 | ``` | 74 | ``` |
| 59 | ["OK", "b1a649ebe8...", false, "restricted: we do not accept events from unauthenticated users, please sign up at https://example.com/"] | 75 | ["OK", <event-id>, false, "restricted: we do not accept events from unauthenticated users, please sign up at https://example.com/"] |
| 60 | ``` | 76 | ``` |
| 61 | 77 | ||
| 62 | ## Signed Event Verification | 78 | ## Signed Event Verification |
| @@ -65,5 +81,6 @@ To verify `AUTH` messages, relays must ensure: | |||
| 65 | 81 | ||
| 66 | - that the `kind` is `22242`; | 82 | - that the `kind` is `22242`; |
| 67 | - that the event `created_at` is close (e.g. within ~10 minutes) of the current time; | 83 | - that the event `created_at` is close (e.g. within ~10 minutes) of the current time; |
| 68 | - that the `content` field matches the relay URL: | 84 | - that the `"challenge"` tag matches the challenge sent before; |
| 85 | - that the `"relay"` tag matches the relay URL: | ||
| 69 | - URL normalization techniques can be applied. For most cases just checking if the domain name is correct should be enough. | 86 | - URL normalization techniques can be applied. For most cases just checking if the domain name is correct should be enough. |