upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfiatjaf_ <fiatjaf@gmail.com>2023-12-06 12:01:27 -0300
committerGitHub <noreply@github.com>2023-12-06 12:01:27 -0300
commit2bd4bf784112aabff9d0fc2dd90fed9107aa5cde (patch)
treec11ffc54f6549f0f337cb75ddce6cd66f77bd449
parentc07f0eab1a74c3de5666ce0d2245d40f90543a3d (diff)
`CLOSED` messages for relays that want to reject REQs and NIP-42 `AUTH` integration (#902)
Co-authored-by: monlovesmango <96307647+monlovesmango@users.noreply.github.com>
-rw-r--r--01.md12
-rw-r--r--42.md69
-rw-r--r--45.md11
-rw-r--r--README.md1
4 files changed, 65 insertions, 28 deletions
diff --git a/01.md b/01.md
index 681fc99..99c991a 100644
--- a/01.md
+++ b/01.md
@@ -141,19 +141,25 @@ Relays can send 4 types of messages, which must also be JSON arrays, according t
141 * `["EVENT", <subscription_id>, <event JSON as defined above>]`, used to send events requested by clients. 141 * `["EVENT", <subscription_id>, <event JSON as defined above>]`, used to send events requested by clients.
142 * `["OK", <event_id>, <true|false>, <message>]`, used to indicate acceptance or denial of an `EVENT` message. 142 * `["OK", <event_id>, <true|false>, <message>]`, used to indicate acceptance or denial of an `EVENT` message.
143 * `["EOSE", <subscription_id>]`, used to indicate the _end of stored events_ and the beginning of events newly received in real-time. 143 * `["EOSE", <subscription_id>]`, used to indicate the _end of stored events_ and the beginning of events newly received in real-time.
144 * `["CLOSED", <subscription_id>, <message>]`, used to indicate that a subscription was ended on the server side.
144 * `["NOTICE", <message>]`, used to send human-readable error messages or other things to clients. 145 * `["NOTICE", <message>]`, used to send human-readable error messages or other things to clients.
145 146
146This NIP defines no rules for how `NOTICE` messages should be sent or treated. 147This NIP defines no rules for how `NOTICE` messages should be sent or treated.
147 148
148- `EVENT` messages MUST be sent only with a subscription ID related to a subscription previously initiated by the client (using the `REQ` message above). 149- `EVENT` messages MUST be sent only with a subscription ID related to a subscription previously initiated by the client (using the `REQ` message above).
149- `OK` messages MUST be sent in response to `EVENT` messages received from clients, they must have the 3rd parameter set to `true` when an event has been accepted by the relay, `false` otherwise. The 4th parameter MUST always be present, but MAY be an empty string when the 3rd is `true`, otherwise it MUST be a string formed by a machine-readable single-word prefix followed by a `:` and then a human-readable message. The standardized machine-readable prefixes are: `duplicate`, `pow`, `blocked`, `rate-limited`, `invalid`, and `error` for when none of that fits. Some examples: 150- `OK` messages MUST be sent in response to `EVENT` messages received from clients, they must have the 3rd parameter set to `true` when an event has been accepted by the relay, `false` otherwise. The 4th parameter MUST always be present, but MAY be an empty string when the 3rd is `true`, otherwise it MUST be a string formed by a machine-readable single-word prefix followed by a `:` and then a human-readable message. Some examples:
150
151 * `["OK", "b1a649ebe8...", true, ""]` 151 * `["OK", "b1a649ebe8...", true, ""]`
152 * `["OK", "b1a649ebe8...", true, "pow: difficulty 25>=24"]` 152 * `["OK", "b1a649ebe8...", true, "pow: difficulty 25>=24"]`
153 * `["OK", "b1a649ebe8...", true, "duplicate: already have this event"]` 153 * `["OK", "b1a649ebe8...", true, "duplicate: already have this event"]`
154 * `["OK", "b1a649ebe8...", false, "blocked: you are banned from posting here"]` 154 * `["OK", "b1a649ebe8...", false, "blocked: you are banned from posting here"]`
155 * `["OK", "b1a649ebe8...", false, "blocked: please register your pubkey at https://my-expensive-relay.example.com"]` 155 * `["OK", "b1a649ebe8...", false, "blocked: please register your pubkey at https://my-expensive-relay.example.com"]`
156 * `["OK", "b1a649ebe8...", false, "rate-limited: slow down there chief"]` 156 * `["OK", "b1a649ebe8...", false, "rate-limited: slow down there chief"]`
157 * `["OK", "b1a649ebe8...", false, "invalid: event creation date is too far off from the current time. Is your system clock in sync?"]` 157 * `["OK", "b1a649ebe8...", false, "invalid: event creation date is too far off from the current time"]`
158 * `["OK", "b1a649ebe8...", false, "pow: difficulty 26 is less than 30"]` 158 * `["OK", "b1a649ebe8...", false, "pow: difficulty 26 is less than 30"]`
159 * `["OK", "b1a649ebe8...", false, "error: could not connect to the database"]` 159 * `["OK", "b1a649ebe8...", false, "error: could not connect to the database"]`
160- `CLOSED` messages MUST be sent in response to a `REQ` when the relay refuses to fulfill it. It can also be sent when a relay decides to kill a subscription on its side before a client has disconnected or sent a `CLOSE`. This message uses the same pattern of `OK` messages with the machine-readable prefix and human-readable message. Some examples:
161 * `["CLOSED", "sub1", "duplicate: sub1 already opened"]`
162 * `["CLOSED", "sub1", "unsupported: filter contains unknown elements"]`
163 * `["CLOSED", "sub1", "error: could not connect to the database"]`
164 * `["CLOSED", "sub1", "error: shutting down idle subscription"]`
165- The standardized machine-readable prefixes for `OK` and `CLOSED` are: `duplicate`, `pow`, `blocked`, `rate-limited`, `invalid`, and `error` for when none of that fits.
diff --git a/42.md b/42.md
index e380e89..07a0486 100644
--- a/42.md
+++ b/42.md
@@ -12,17 +12,15 @@ This NIP defines a way for clients to authenticate to relays by signing an ephem
12 12
13A relay may want to require clients to authenticate to access restricted resources. For example, 13A relay may want to require clients to authenticate to access restricted resources. For example,
14 14
15 - A relay may request payment or other forms of whitelisting to publish events -- this can naïvely be achieved by limiting publication 15 - A relay may request payment or other forms of whitelisting to publish events -- this can naïvely be achieved by limiting publication to events signed by the whitelisted key, but with this NIP they may choose to accept any events as long as they are published from an authenticated user;
16 to events signed by the whitelisted key, but with this NIP they may choose to accept any events as long as they are published from an 16 - A relay may limit access to `kind: 4` DMs to only the parties involved in the chat exchange, and for that it may require authentication before clients can query for that kind.
17 authenticated user;
18 - A relay may limit access to `kind: 4` DMs to only the parties involved in the chat exchange, and for that it may require authentication
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. 17 - A relay may limit subscriptions of any kind to paying users or users whitelisted through any other means, and require authentication.
21 18
22## Definitions 19## Definitions
23 20
24This NIP defines a new message, `AUTH`, which relays can send when they support authentication and clients can send to relays when they want 21### New client-relay protocol messages
25to authenticate. When sent by relays, the message is of the following form: 22
23This NIP defines a new message, `AUTH`, which relays can send when they support authentication and clients can send to relays when they want to authenticate. When sent by relays, the message is of the following form:
26 24
27```json 25```json
28["AUTH", <challenge-string>] 26["AUTH", <challenge-string>]
@@ -34,10 +32,11 @@ And, when sent by clients, of the following form:
34["AUTH", <signed-event-json>] 32["AUTH", <signed-event-json>]
35``` 33```
36 34
37The 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, 35`AUTH` messages sent by clients should be answered with an `OK` message, like any `EVENT` message.
38one for the relay URL and one for the challenge string as received from the relay. 36
39Relays MUST exclude `kind: 22242` events from being broadcasted to any client. 37### Canonical authentication event
40`created_at` should be the current time. Example: 38
39The 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, one for the relay URL and one for the challenge string as received from the relay. Relays MUST exclude `kind: 22242` events from being broadcasted to any client. `created_at` should be the current time. Example:
41 40
42```json 41```json
43{ 42{
@@ -50,27 +49,49 @@ Relays MUST exclude `kind: 22242` events from being broadcasted to any client.
50} 49}
51``` 50```
52 51
52### `OK` and `CLOSED` machine-readable prefixes
53
54This NIP defines two new prefixes that can be used in `OK` (in response to event writes by clients) and `CLOSED` (in response to rejected subscriptions by clients):
55
56- `"auth-required: "` - for when a client has not performed `AUTH` and the relay requires that to fulfill the query or write the event.
57- `"restricted: "` - for when a client has already performed `AUTH` but the key used to perform it is still not allowed by the relay or is exceeding its authorization.
58
53## Protocol flow 59## Protocol flow
54 60
55At any moment the relay may send an `AUTH` message to the client containing a challenge. After receiving that the client may decide to 61At any moment the relay may send an `AUTH` message to the client containing a challenge. The challenge is valid for the duration of the connection or until another challenge is sent by the relay. The client MAY decide to send its `AUTH` event at any point and the authenticated session is valid afterwards for the duration of the connection.
56authenticate itself or not. The challenge is expected to be valid for the duration of the connection or until a next challenge is sent by
57the relay.
58 62
59The client may send an auth message right before performing an action for which it knows authentication will be required -- for example, right 63### `auth-required` in response to a `REQ` message
60before requesting `kind: 4` chat messages --, or it may do right on connection start or at some other moment it deems best. The authentication
61is expected to last for the duration of the WebSocket connection.
62 64
63Upon receiving a message from an unauthenticated user it can't fulfill without authentication, a relay may choose to notify the client. For 65Given that a relay is likely to require clients to perform authentication only for certain jobs, like answering a `REQ` or accepting an `EVENT` write, these are some expected common flows:
64that it can use a `NOTICE` or `OK` message with a standard prefix `"restricted: "` that is readable both by humans and machines, for example:
65 66
66```json 67```
67["NOTICE", "restricted: we can't serve DMs to unauthenticated users, does your client implement NIP-42?"] 68relay: ["AUTH", "<challenge>"]
69client: ["REQ", "sub_1", {"kinds": [4]}]
70relay: ["CLOSED", "sub_1", "auth-required: we can't serve DMs to unauthenticated users"]
71client: ["AUTH", {"id": "abcdef...", ...}]
72relay: ["OK", "abcdef...", true, ""]
73client: ["REQ", "sub_1", {"kinds": [4]}]
74relay: ["EVENT", "sub_1", {...}]
75relay: ["EVENT", "sub_1", {...}]
76relay: ["EVENT", "sub_1", {...}]
77relay: ["EVENT", "sub_1", {...}]
78...
68``` 79```
69 80
70or it can return an `OK` message noting the reason an event was not written using the same prefix: 81In this case, the `AUTH` message from the relay could be sent right as the client connects or it can be sent immediately before the `CLOSED` is sent. The only requirement is that _the client must have a stored challenge associated with that relay_ so it can act upon that in response to the `auth-required` `CLOSED` message.
71 82
72```json 83### `auth-required` in response to an `EVENT` message
73["OK", <event-id>, false, "restricted: we do not accept events from unauthenticated users, please sign up at https://example.com/"] 84
85The same flow is valid for when a client wants to write an `EVENT` to the relay, except now the relay sends back an `OK` message instead of a `CLOSED` message:
86
87```
88relay: ["AUTH", "<challenge>"]
89client: ["EVENT", {"id": "012345...", ...}]
90relay: ["OK", "012345...", false, "auth-required: we only accept events from registered users"]
91client: ["AUTH", {"id": "abcdef...", ...}]
92relay: ["OK", "abcdef...", true, ""]
93client: ["EVENT", {"id": "012345...", ...}]
94relay: ["OK", "012345...", true, ""]
74``` 95```
75 96
76## Signed Event Verification 97## Signed Event Verification
diff --git a/45.md b/45.md
index 998b952..780dfb6 100644
--- a/45.md
+++ b/45.md
@@ -27,7 +27,9 @@ In case a relay uses probabilistic counts, it MAY indicate it in the response wi
27["COUNT", <subscription_id>, {"count": <integer>}] 27["COUNT", <subscription_id>, {"count": <integer>}]
28``` 28```
29 29
30## Examples: 30Whenever the relay decides to refuse to fulfill the `COUNT` request, it MUST return a `CLOSED` message.
31
32## Examples
31 33
32### Followers count 34### Followers count
33 35
@@ -49,3 +51,10 @@ In case a relay uses probabilistic counts, it MAY indicate it in the response wi
49["COUNT", <subscription_id>, {"kinds": [1]}] 51["COUNT", <subscription_id>, {"kinds": [1]}]
50["COUNT", <subscription_id>, {"count": 93412452, "approximate": true}] 52["COUNT", <subscription_id>, {"count": 93412452, "approximate": true}]
51``` 53```
54
55### Relay refuses to count
56
57```
58["COUNT", <subscription_id>, {"kinds": [4], "authors": [<pubkey>], "#p": [<pubkey>]}]
59["CLOSED", <subscription_id>, "auth-required: cannot count other people's DMs"]
60```
diff --git a/README.md b/README.md
index 68c295f..47b5986 100644
--- a/README.md
+++ b/README.md
@@ -172,6 +172,7 @@ They exist to document what may be implemented by [Nostr](https://github.com/nos
172| `EVENT` | used to send events requested to clients | [01](01.md) | 172| `EVENT` | used to send events requested to clients | [01](01.md) |
173| `NOTICE` | used to send human-readable messages to clients | [01](01.md) | 173| `NOTICE` | used to send human-readable messages to clients | [01](01.md) |
174| `OK` | used to notify clients if an EVENT was successful | [01](01.md) | 174| `OK` | used to notify clients if an EVENT was successful | [01](01.md) |
175| `CLOSED` | used to notify clients that a REQ was ended and why | [01](01.md) |
175| `AUTH` | used to send authentication challenges | [42](42.md) | 176| `AUTH` | used to send authentication challenges | [42](42.md) |
176| `COUNT` | used to send requested event counts to clients | [45](45.md) | 177| `COUNT` | used to send requested event counts to clients | [45](45.md) |
177 178