Paso 1 de 5
El relay emite un desafío
En cualquier momento, normalmente justo después de conectar, el relay envía ["AUTH", "<challenge>"]. El cliente lo guarda para esa conexión.
NIP-42
BorradorMensajes de relayRelay
Permite que un relay pregunte "¿quién eres?" antes de servir o aceptar eventos. El relay envía un desafío, el cliente firma un evento kind 22242 de corta vida que lo repite, y así el relay sabe qué pubkey está al otro lado de la conexión.
Edita el formulario o el JSON: ambos se mantienen sincronizados, la insignia indica si el resultado es válido y, al tocar cualquier campo, se explica qué hace.
Cómo se conectan las partes: Leer DMs de un relay que exige autenticación
Selecciona cualquier campo del formulario o del JSON para ver qué hace.
Paso 1 de 5
En cualquier momento, normalmente justo después de conectar, el relay envía ["AUTH", "<challenge>"]. El cliente lo guarda para esa conexión.
Añade un nuevo mensaje AUTH al protocolo básico cliente–relay y reutiliza OK y CLOSED.
Los relays que guardan DMs privados suelen exigir AUTH antes de servirlos.
Los relays pueden exigir AUTH antes de aceptar o servir gift wraps, para combatir el spam y proteger a los destinatarios.
Los relays pueden anunciar sus requisitos de autenticación en su documento de información.
La pista "auth" en EOSE le dice al cliente que hay más resultados disponibles tras autenticarse.
El texto de las especificaciones se cita en inglés del repositorio nostr-protocol/nips. Nuestras explicaciones y la interfaz están traducidas. Código fuente en GitHub en el commit 0046368
This NIP defines a way for clients to authenticate to relays by signing an ephemeral event.
A relay may want to require clients to authenticate to access restricted resources. For example,
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.This 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 has the following form:
["AUTH", <challenge-string>]
And, when sent by clients, the following form:
["AUTH", <signed-event-json>]
Clients MAY provide signed events from multiple pubkeys in a sequence of AUTH messages. Relays MUST treat all pubkeys as authenticated accordingly.
AUTH messages sent by clients MUST be answered with an OK message, like any EVENT message.
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, 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:
{
"kind": 22242,
"tags": [
["relay", "wss://relay.example.com/"],
["challenge", "challengestringhere"]
],
// other fields...
}
OK and CLOSED machine-readable prefixesThis 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):
"auth-required: " - for when a client has not performed AUTH and the relay requires that to fulfill the query or write the event."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.At 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.
auth-required in response to a REQ messageGiven 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:
relay: ["AUTH", "<challenge>"]
client: ["REQ", "sub_1", {"kinds": [4]}]
relay: ["CLOSED", "sub_1", "auth-required: we can't serve DMs to unauthenticated users"]
client: ["AUTH", {"id": "abcdef...", ...}]
client: ["AUTH", {"id": "abcde2...", ...}]
relay: ["OK", "abcdef...", true, ""]
relay: ["OK", "abcde2...", true, ""]
client: ["REQ", "sub_1", {"kinds": [4]}]
relay: ["EVENT", "sub_1", {...}]
relay: ["EVENT", "sub_1", {...}]
relay: ["EVENT", "sub_1", {...}]
relay: ["EVENT", "sub_1", {...}]
...
In 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.
auth hint in EOSERelays MAY use the "auth" hint in an EOSE message (as defined in NIP-67) to notify the client that more results may be available if they authenticate. In this case, the relay MUST send the AUTH challenge before the EOSE message.
auth-required in response to an EVENT messageThe 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:
relay: ["AUTH", "<challenge>"]
client: ["EVENT", {"id": "012345...", ...}]
relay: ["OK", "012345...", false, "auth-required: we only accept events from registered users"]
client: ["AUTH", {"id": "abcdef...", ...}]
relay: ["OK", "abcdef...", true, ""]
client: ["EVENT", {"id": "012345...", ...}]
relay: ["OK", "012345...", true, ""]
To verify AUTH messages, relays must ensure:
kind is 22242;created_at is close (e.g. within ~10 minutes) of the current time;"challenge" tag matches the challenge sent before;"relay" tag matches the relay URL: