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.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 NewSignTransport(transport http.RoundTripper, opts ...signOption) http.RoundTripper
- func NewVerifyMiddleware(opts ...verifyOption) func(http.Handler) http.Handler
- func WithHeaders(hdr ...string) signOption
- func WithHmacSha256(keyID string, secret []byte) signOrVerifyOption
- func WithSignEcdsaP256Sha256(keyID string, pk *ecdsa.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
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func NewSignTransport ΒΆ
func NewSignTransport(transport http.RoundTripper, 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 WithHeaders ΒΆ
func WithHeaders(hdr ...string) signOption
WithHeaders sets the list of headers that will be included in the signature. The Digest header is always included (and the digest calculated).
If not provided, the default headers `content-type, content-length, host` are used.
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 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 ΒΆ
This section is empty.