upleb.uk

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

summaryrefslogtreecommitdiff
path: root/42.md
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 /42.md
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>
Diffstat (limited to '42.md')
-rw-r--r--42.md69
1 files changed, 45 insertions, 24 deletions
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