Finished NIP42 implementation
This commit is contained in:
parent
f7b74bd22c
commit
d06206bb24
8 changed files with 101 additions and 17 deletions
|
@ -215,36 +215,60 @@ fn send(client: &Client, message: Message) {
|
|||
}
|
||||
}
|
||||
|
||||
fn needs_auth(context: &Context, client: &Client, event: Option<&Event>) -> bool {
|
||||
if !context.config.auth_required() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if event.is_some() {
|
||||
let event = event.unwrap();
|
||||
let admin_pk = context.config.get_admin_pubkey();
|
||||
if event.pubkey == *admin_pk {
|
||||
return false;
|
||||
}
|
||||
|
||||
if event.kind() == nostr::Kind::Metadata {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if !client.authenticated {
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
async fn handle_msg(context: &Context, client: &mut Client, client_message: ClientMessage) {
|
||||
match client_message {
|
||||
ClientMessage::Event(event) => {
|
||||
if context.config.auth_required()
|
||||
&& event.kind() != nostr::Kind::Metadata
|
||||
&& !client.authenticated
|
||||
{
|
||||
if needs_auth(context, client, Some(&event)) {
|
||||
request_auth(context, client).await;
|
||||
return;
|
||||
}
|
||||
|
||||
handle_event(context, client, event).await
|
||||
}
|
||||
ClientMessage::Req {
|
||||
subscription_id,
|
||||
filters,
|
||||
} => {
|
||||
if context.config.auth_required() && !client.authenticated {
|
||||
if needs_auth(context, client, None) {
|
||||
request_auth(context, client).await;
|
||||
return;
|
||||
}
|
||||
|
||||
handle_req(context, client, subscription_id, filters).await
|
||||
}
|
||||
ClientMessage::Count {
|
||||
subscription_id,
|
||||
filters,
|
||||
} => {
|
||||
if context.config.auth_required() && !client.authenticated {
|
||||
if needs_auth(context, client, None) {
|
||||
request_auth(context, client).await;
|
||||
return;
|
||||
}
|
||||
|
||||
handle_count(context, client, subscription_id, filters).await
|
||||
}
|
||||
ClientMessage::Close(subscription_id) => handle_close(client, subscription_id).await,
|
||||
|
@ -378,9 +402,51 @@ async fn handle_close(client: &mut Client, subscription_id: SubscriptionId) {
|
|||
}
|
||||
|
||||
async fn handle_auth(context: &Context, client: &mut Client, event: Box<Event>) {
|
||||
client.authenticate(&event);
|
||||
let client_status = format!("Client authenticated: {}", client.authenticated);
|
||||
let message = nostr::RelayMessage::notice(client_status);
|
||||
let mut subscriber = context.pubsub.subscribe(channels::MSG_AUTH).await;
|
||||
context
|
||||
.pubsub
|
||||
.publish(
|
||||
channels::MSG_NOOSE,
|
||||
crate::bussy::Message {
|
||||
source: channels::MSG_AUTH,
|
||||
content: crate::bussy::Command::DbReqGetProfile(event.pubkey),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut message = nostr::RelayMessage::ok(
|
||||
event.id,
|
||||
client.authenticated,
|
||||
"auth-required: User not registered",
|
||||
);
|
||||
|
||||
let Ok(result) = subscriber.recv().await else {
|
||||
context
|
||||
.pubsub
|
||||
.publish(
|
||||
channels::MSG_RELAY,
|
||||
crate::bussy::Message {
|
||||
source: channels::MSG_RELAY,
|
||||
content: crate::bussy::Command::DbResOkWithStatus(client.client_id, message),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
};
|
||||
|
||||
if let crate::bussy::Command::DbResGetProfile(profile) = result.content {
|
||||
client.authenticate(context.config.get_relay_url(), &event);
|
||||
|
||||
let client_status = format!("Client authenticated: {}", client.authenticated);
|
||||
let status_message = if client.authenticated {
|
||||
""
|
||||
} else {
|
||||
"auth-required: we only accept events from registered users"
|
||||
};
|
||||
|
||||
message = nostr::RelayMessage::ok(event.id, client.authenticated, status_message);
|
||||
};
|
||||
|
||||
context
|
||||
.pubsub
|
||||
.publish(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue