1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// This file is part of HTTP Signatures // HTTP Signatures is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // HTTP Signatures is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with HTTP Signatures If not, see <http://www.gnu.org/licenses/>. //! This module defines useful traits for using HTTP Signatures. use crate::{ create::{CreateKey, HttpSignature}, error::{Error, VerificationError}, verify::VerifyKey, }; pub use crate::verify::AsVerifyKey; /// `AsHttpSignature` defines a trait for getting an Authorization or Signature Header string from /// any type that implements it. It provides three methods: `as_http_signature`, which implementors /// must define, and `authorization_header` and `signature_header`, which use `as_http_signature` /// to create the header string. pub trait AsHttpSignature { /// Gets an `HttpSignature` struct from an immutably borrowed Self fn as_http_signature<'a>(&self, key_id: String, key: CreateKey) -> Result<HttpSignature, Error>; /// Generates the Authorization Header from an immutably borrowed Self fn authorization_header(&self, key_id: String, key: CreateKey) -> Result<String, Error> { Ok(self .as_http_signature(key_id, key)? .authorization_header()?) } /// Generates the Signature Header from an immutably borrowed Self fn signature_header(&self, key_id: String, key: CreateKey) -> Result<String, Error> { Ok(self.as_http_signature(key_id, key)?.signature_header()?) } } /// `WithHttpSignature` defines a trait for adding Authorization and Signature headers to another /// library's request or response object. pub trait WithHttpSignature: AsHttpSignature { fn with_authorization_header( &mut self, key_id: String, key: CreateKey, ) -> Result<&mut Self, Error>; fn with_signature_header(&mut self, key_id: String, key: CreateKey) -> Result<&mut Self, Error>; } /// The `VerifyHeader` trait is meant to be implemented for the request types from /// http libraries (such as Hyper and Rocket). This trait makes verifying requests much easier, /// since the `verify_authorization_header()` and `verify_signature_header()` methods can be called /// directly on a Request type. /// /// For examples, see the /// [hyper server](https://git.asonix.dog/asonix/http-signatures/src/branch/master/examples/hyper-server) /// and [rocket](https://git.asonix.dog/asonix/http-signatures/src/branch/master/examples/rocket-server) files. pub trait VerifyHeader { fn verify_signature_header(&self, key: VerifyKey) -> Result<(), VerificationError>; fn verify_authorization_header(&self, key: VerifyKey) -> Result<(), VerificationError>; }