pub struct HttpSigner { /* private fields */ }Expand description
HTTP request signer using Ed25519 signatures.
§Example
use herolib_crypt::httpsig::HttpSigner;
use herolib_crypt::keys::Ed25519Keypair;
use http::Request;
let keypair = Ed25519Keypair::generate()?;
let signer = HttpSigner::new(keypair, "user-123");
let body = b"{\"amount\": 100}";
let mut request = Request::post("https://api.example.com/api/v1/payments")
.header("content-type", "application/json")
.body(body.to_vec())?;
signer.sign_request(&mut request, body)?;
// Request now has signature headersImplementations§
Source§impl HttpSigner
impl HttpSigner
Sourcepub fn new(keypair: Ed25519Keypair, key_id: impl Into<String>) -> Self
pub fn new(keypair: Ed25519Keypair, key_id: impl Into<String>) -> Self
Create a new signer with a keypair and key identifier.
Sourcepub fn with_headers(self, headers: Vec<String>) -> Self
pub fn with_headers(self, headers: Vec<String>) -> Self
Add additional headers to include in the signature.
§Example
let signer = HttpSigner::new(keypair, "user-123")
.with_headers(vec!["content-type".to_string(), "x-request-id".to_string()]);Sourcepub fn with_label(self, label: impl Into<String>) -> Self
pub fn with_label(self, label: impl Into<String>) -> Self
Set the signature label (default: “sig1”).
Sourcepub fn sign_request<B>(
&self,
request: &mut Request<B>,
body: &[u8],
) -> Result<(), HttpSigError>
pub fn sign_request<B>( &self, request: &mut Request<B>, body: &[u8], ) -> Result<(), HttpSigError>
Sign an HTTP request.
This method works with any HTTP library that uses http::Request.
The signature headers are automatically added to the request.
§Arguments
request- Mutable reference to the HTTP requestbody- Request body bytes
§Example
use herolib_crypt::httpsig::HttpSigner;
use herolib_crypt::keys::Ed25519Keypair;
use http::Request;
let keypair = Ed25519Keypair::generate()?;
let signer = HttpSigner::new(keypair, "user-123");
let body = b"{\"amount\": 100}";
let mut request = Request::post("https://api.example.com/payments")
.header("content-type", "application/json")
.body(body.to_vec())?;
signer.sign_request(&mut request, body)?;
// Request now has Signature-Input, Signature, and Content-Digest headersSourcepub fn sign_response<B>(
&self,
response: &mut Response<B>,
body: &[u8],
) -> Result<(), HttpSigError>
pub fn sign_response<B>( &self, response: &mut Response<B>, body: &[u8], ) -> Result<(), HttpSigError>
Sign an HTTP response.
This method works with any HTTP library that uses http::Response.
The signature headers are automatically added to the response.
§Arguments
response- Mutable reference to the HTTP responsebody- Response body bytes
Auto Trait Implementations§
impl Freeze for HttpSigner
impl RefUnwindSafe for HttpSigner
impl Send for HttpSigner
impl Sync for HttpSigner
impl Unpin for HttpSigner
impl UnwindSafe for HttpSigner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more