Documentation ¶
Overview ¶
Package httpsig signs and verifies HTTP requests (with body digests) according to the "HTTP Message Signatures" draft standard https://datatracker.ietf.org/doc/draft-ietf-httpbis-message-signatures/
Example (Round_trip) ¶
package main import ( "fmt" "io" "net/http" "time" "github.com/jbowes/httpsig" ) const secret = "support-your-local-cat-bonnet-store" func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") _, _ = io.WriteString(w, "Your request has a valid signature!") }) middleware := httpsig.NewVerifyMiddleware(httpsig.WithHmacSha256("key1", []byte(secret))) http.Handle("/", middleware(h)) go func() { _ = http.ListenAndServe("127.0.0.1:1234", http.DefaultServeMux) }() // Give the server time to sleep. Terrible, I know. time.Sleep(100 * time.Millisecond) client := http.Client{ // Wrap the transport: Transport: httpsig.NewSignTransport( http.DefaultTransport, httpsig.HttpSigningConfigs{}, httpsig.WithHmacSha256("key1", []byte(secret)), ), } resp, err := client.Get("http://127.0.0.1:1234/") if err != nil { fmt.Println("got err: ", err) return } defer resp.Body.Close() fmt.Println(resp.Status) }
Output: 200 OK
Index ¶
- func ContentDigesSHA216(in []byte) string
- func NewSignTransport(transport http.RoundTripper, params HttpSigningConfigs, opts ...signOption) http.RoundTripper
- func NewVerifyMiddleware(opts ...verifyOption) func(http.Handler) http.Handler
- func WithHmacSha256(keyID string, secret []byte) signOrVerifyOption
- func WithSignEcdsaP256Sha256(keyID string, pk *ecdsa.PrivateKey) signOption
- func WithSignRSA256(pk *rsa.PrivateKey) signOption
- func WithSignRsaPssSha512(keyID string, pk *rsa.PrivateKey) signOption
- func WithVerifyEcdsaP256Sha256(keyID string, pk *ecdsa.PublicKey) verifyOption
- func WithVerifyRsaPssSha512(keyID string, pk *rsa.PublicKey) verifyOption
- type HttpSigningConfigs
- type Verifier
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentDigesSHA216 ¶
func NewSignTransport ¶
func NewSignTransport(transport http.RoundTripper, params HttpSigningConfigs, opts ...signOption) http.RoundTripper
NewSignTransport returns a new client transport that wraps the provided transport with http message signing and body digest creation.
Use the various `WithSign*` option funcs to configure signature algorithms with their provided key ids. You must provide at least one signing option. A signature for every provided key id is included on each request. Multiple included signatures allow you to gracefully introduce stronger algorithms, rotate keys, etc.
func NewVerifyMiddleware ¶
NewVerifyMiddleware returns a configured http server middleware that can be used to wrap multiple handlers for http message signature and digest verification.
Use the `WithVerify*` option funcs to configure signature verification algorithms that map to their provided key ids.
Requests with missing signatures, malformed signature headers, expired signatures, or invalid signatures are rejected with a `400` response. Only one valid signature is required from the known key ids. However, only the first known key id is checked.
func WithHmacSha256 ¶
WithHmacSha256 adds signing or signature verification using `hmac-sha256` with the given shared secret using the given key id.
func WithSignEcdsaP256Sha256 ¶
func WithSignEcdsaP256Sha256(keyID string, pk *ecdsa.PrivateKey) signOption
WithSignEcdsaP256Sha256 adds signing using `ecdsa-p256-sha256` with the given private key using the given key id.
func WithSignRSA256 ¶
func WithSignRSA256(pk *rsa.PrivateKey) signOption
WithSignEcdsaP256Sha256 adds signing using `ecdsa-p256-sha256` with the given private key using the given key id.
func WithSignRsaPssSha512 ¶
func WithSignRsaPssSha512(keyID string, pk *rsa.PrivateKey) signOption
WithSignRsaPssSha512 adds signing using `rsa-pss-sha512` with the given private key using the given key id.
func WithVerifyEcdsaP256Sha256 ¶
WithVerifyEcdsaP256Sha256 adds signature verification using `ecdsa-p256-sha256` with the given public key using the given key id.
func WithVerifyRsaPssSha512 ¶
WithVerifyRsaPssSha512 adds signature verification using `rsa-pss-sha512` with the given public key using the given key id.
Types ¶
type HttpSigningConfigs ¶
type HttpSigningConfigs struct { // Set of derived components that will be used to sign the outbound message // An exaustive list can be found in section 2.2 of the spec // Ex: ["@query", "@path", "@method"] DerivedComponents map[string]struct{} // Set of required HTTP headers that must be present and included in the signature // If header is not provided in request this will result in an error before being sent Headers map[string]struct{} InputHeaderLabel string SignatureHeaderLabel string SignatureLabel string PubKey *rsa.PublicKey }