Skip to content

Commit 6afbc7b

Browse files
committed
Add usage example
1 parent 1deb5b2 commit 6afbc7b

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

README.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,46 @@ on signing headers and request paths, and you probably want to sign the
2121
request body too, so body digest calculation according to
2222
[Digest Headers][dighdr] is included.
2323

24+
## Usage
25+
26+
### Signing HTTP Requests in Clients
27+
28+
To sign HTTP requests from a client, wrap an `http.Client`'s transport with `NewSignTransport`:
29+
30+
```go
31+
client := http.Client{
32+
// Wrap the transport:
33+
Transport: httpsig.NewSignTransport(http.DefaultTransport, httpsig.WithSignEcdsaP256Sha256("key1", privKey)),
34+
}
35+
36+
var buf bytes.Buffer
37+
38+
// construct body, etc
39+
// ...
40+
41+
resp, err := client.Post("https://some-url.com", "application/json", &buf)
42+
if err != nil {
43+
return
44+
}
45+
defer resp.Body.Close()
46+
47+
// ...
48+
```
49+
50+
### Verifying HTTP Requests in Servers
51+
52+
To verify HTTP requests on the server, wrap the `http.Handler`s you wish to protect with `NewVerifyMiddleware`. `NewVerifyMiddleware` returns the wrapping func, so you can reuse
53+
configuration across multiple handlers.
54+
55+
```go
56+
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
w.Header().Set("Content-Type", "text/plain")
58+
io.WriteString(w, "Your request has an valid signature!")
59+
})
60+
61+
middleware := httpsig.NewVerifyMiddleware(httpsig.WithVerifyEcdsaP256Sha256("key1", pubkey))
62+
http.Handle("/", middleware(h))
63+
```
2464
## The Big Feature Matrix
2565

2666
This implementation is based on version `05` of [Signing HTTP Messages][msgsig]
@@ -55,14 +95,6 @@ version `05` of [Digest Headers][dighdr] (`draft-ietf-httpbis-digest-headers-05`
5595
| digest: `id-sha-256` || |
5696
| custom digest formats | ||
5797

58-
59-
60-
61-
62-
63-
64-
65-
6698
## Contributing
6799

68100
I would love your help!

httpsig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func NewSignTransport(transport http.RoundTripper, opts ...signOption) http.Roun
6262
}
6363

6464
// Always set a digest (for now)
65+
// TODO: we could skip setting digest on an empty body if content-length is included in the sig
6566
r.Header.Set("Digest", calcDigest(b.Bytes()))
6667

6768
msg := messageFromRequest(nr)

0 commit comments

Comments
 (0)