Skip to main content

HttpSigner

Struct HttpSigner 

Source
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 headers

Implementations§

Source§

impl HttpSigner

Source

pub fn new(keypair: Ed25519Keypair, key_id: impl Into<String>) -> Self

Create a new signer with a keypair and key identifier.

Source

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()]);
Source

pub fn with_label(self, label: impl Into<String>) -> Self

Set the signature label (default: “sig1”).

Source

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 request
  • body - 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 headers
Source

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 response
  • body - Response body bytes

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V