Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add httpdata struct to get http auth information from tags in event #143

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/nostr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@ This project is distributed under the MIT software license - see the [LICENSE](.

⚡ Tips: <https://getalby.com/p/yuki>

⚡ Lightning Address: yuki@getalby.com
⚡ Lightning Address: yuki@getalby.com
2 changes: 1 addition & 1 deletion crates/nostr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod prelude;
pub mod types;

pub use self::event::tag::{
ExternalIdentity, Identity, ImageDimensions, Marker, Report, Tag, TagKind,
ExternalIdentity, HttpMethod, Identity, ImageDimensions, Marker, Report, Tag, TagKind,
};
pub use self::event::{Event, EventBuilder, EventId, Kind, UnsignedEvent};
pub use self::key::Keys;
Expand Down
1 change: 1 addition & 0 deletions crates/nostr/src/nips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pub mod nip53;
pub mod nip58;
pub mod nip65;
pub mod nip94;
pub mod nip98;
142 changes: 142 additions & 0 deletions crates/nostr/src/nips/nip98.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Distributed under the MIT software license

//! NIP98
//!
//! This NIP defines an ephemerial event used to authorize requests to HTTP servers using nostr events.
//! This is useful for HTTP services which are build for Nostr and deal with Nostr user accounts.
//! <https://github.com/nostr-protocol/nips/blob/master/98.md>
use core::fmt;

use crate::{HttpMethod, Tag, UncheckedUrl};
use bitcoin_hashes::sha256::Hash as Sha256Hash;

/// [`HttpData`] required tags
#[derive(Debug)]
pub enum RequiredTags {
/// [`Tag::AbsoluteURL`]
AbsoluteURL,
/// [`Tag::Method`]
Method,
}

impl fmt::Display for RequiredTags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AbsoluteURL => write!(f, "url"),
Self::Method => write!(f, "method"),
}
}
}

/// [`HttpData`] error
#[derive(Debug)]
pub enum Error {
/// Hex decoding error
Hex(bitcoin_hashes::hex::Error),
/// Tag missing when parsing
MissingTag(RequiredTags),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Hex(e) => write!(f, "{e}"),
Self::MissingTag(tag) => write!(f, r#"missing tag "{tag}""#),
}
}
}

impl From<bitcoin_hashes::hex::Error> for Error {
fn from(e: bitcoin_hashes::hex::Error) -> Self {
Self::Hex(e)
}
}

/// HTTP Data
pub struct HttpData {
/// absolute request URL
pub url: UncheckedUrl,
/// HTTP method
pub method: HttpMethod,
/// SHA256 hash of the request body
pub payload: Option<Sha256Hash>,
}

impl HttpData {
/// New [`HttpData`]
pub fn new(url: UncheckedUrl, method: HttpMethod) -> Self {
Self {
url,
method,
payload: None,
}
}

/// Add hex-encoded SHA256 hash of the request body
pub fn payload(self, payload: Sha256Hash) -> Self {
Self {
payload: Some(payload),
..self
}
}
}

impl From<HttpData> for Vec<Tag> {
fn from(value: HttpData) -> Self {
let mut tags = Vec::new();

let HttpData {
url,
method,
payload,
} = value;

tags.push(Tag::AbsoluteURL(url));
tags.push(Tag::Method(method));

if let Some(payload) = payload {
tags.push(Tag::Payload(payload));
}

tags
}
}

impl TryFrom<Vec<Tag>> for HttpData {
type Error = Error;

fn try_from(value: Vec<Tag>) -> Result<Self, Self::Error> {
let url = value
.iter()
.find_map(|t| match t {
Tag::AbsoluteURL(u) => Some(u),
_ => None,
})
.cloned()
.ok_or(Error::MissingTag(RequiredTags::AbsoluteURL))?;
let method = value
.iter()
.find_map(|t| match t {
Tag::Method(m) => Some(m),
_ => None,
})
.cloned()
.ok_or(Error::MissingTag(RequiredTags::Method))?;
let payload = value
.iter()
.find_map(|t| match t {
Tag::Payload(p) => Some(p),
_ => None,
})
.cloned();

Ok(Self {
url,
method,
payload,
})
}
}