| 1 | # httpsig |
| 2 | |
| 3 | Forked from <https://github.com/go-fed/httpsig> |
| 4 | |
| 5 | > HTTP Signatures made simple |
| 6 | |
| 7 | [![Build Status][Build-Status-Image]][Build-Status-Url] [![Go Reference][Go-Reference-Image]][Go-Reference-Url] |
| 8 | [![Go Report Card][Go-Report-Card-Image]][Go-Report-Card-Url] [![License][License-Image]][License-Url] |
| 9 | [![Chat][Chat-Image]][Chat-Url] [![OpenCollective][OpenCollective-Image]][OpenCollective-Url] |
| 10 | |
| 11 | `go get github.com/42wim/httpsig` |
| 12 | |
| 13 | Implementation of [HTTP Signatures](https://tools.ietf.org/html/draft-cavage-http-signatures). |
| 14 | |
| 15 | Supports many different combinations of MAC, HMAC signing of hash, or RSA |
| 16 | signing of hash schemes. Its goals are: |
| 17 | |
| 18 | * Have a very simple interface for signing and validating |
| 19 | * Support a variety of signing algorithms and combinations |
| 20 | * Support setting either headers (`Authorization` or `Signature`) |
| 21 | * Remaining flexible with headers included in the signing string |
| 22 | * Support both HTTP requests and responses |
| 23 | * Explicitly not support known-cryptographically weak algorithms |
| 24 | * Support automatic signing and validating Digest headers |
| 25 | |
| 26 | ## How to use |
| 27 | |
| 28 | `import "github.com/42wim/httpsig"` |
| 29 | |
| 30 | ### Signing |
| 31 | |
| 32 | Signing a request or response requires creating a new `Signer` and using it: |
| 33 | |
| 34 | ``` |
| 35 | func sign(privateKey crypto.PrivateKey, pubKeyId string, r *http.Request) error { |
| 36 | prefs := []httpsig.Algorithm{httpsig.RSA_SHA512, httpsig.RSA_SHA256} |
| 37 | digestAlgorithm := DigestSha256 |
| 38 | // The "Date" and "Digest" headers must already be set on r, as well as r.URL. |
| 39 | headersToSign := []string{httpsig.RequestTarget, "date", "digest"} |
| 40 | signer, chosenAlgo, err := httpsig.NewSigner(prefs, digestAlgorithm, headersToSign, httpsig.Signature) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | // To sign the digest, we need to give the signer a copy of the body... |
| 45 | // ...but it is optional, no digest will be signed if given "nil" |
| 46 | body := ... |
| 47 | // If r were a http.ResponseWriter, call SignResponse instead. |
| 48 | return signer.SignRequest(privateKey, pubKeyId, r, body) |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | `Signer`s are not safe for concurrent use by goroutines, so be sure to guard |
| 53 | access: |
| 54 | |
| 55 | ``` |
| 56 | type server struct { |
| 57 | signer httpsig.Signer |
| 58 | mu *sync.Mutex |
| 59 | } |
| 60 | |
| 61 | func (s *server) handlerFunc(w http.ResponseWriter, r *http.Request) { |
| 62 | privateKey := ... |
| 63 | pubKeyId := ... |
| 64 | // Set headers and such on w |
| 65 | s.mu.Lock() |
| 66 | defer s.mu.Unlock() |
| 67 | // To sign the digest, we need to give the signer a copy of the response body... |
| 68 | // ...but it is optional, no digest will be signed if given "nil" |
| 69 | body := ... |
| 70 | err := s.signer.SignResponse(privateKey, pubKeyId, w, body) |
| 71 | if err != nil { |
| 72 | ... |
| 73 | } |
| 74 | ... |
| 75 | } |
| 76 | ``` |
| 77 | |
| 78 | The `pubKeyId` will be used at verification time. |
| 79 | |
| 80 | ### Verifying |
| 81 | |
| 82 | Verifying requires an application to use the `pubKeyId` to both retrieve the key |
| 83 | needed for verification as well as determine the algorithm to use. Use a |
| 84 | `Verifier`: |
| 85 | |
| 86 | ``` |
| 87 | func verify(r *http.Request) error { |
| 88 | verifier, err := httpsig.NewVerifier(r) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | pubKeyId := verifier.KeyId() |
| 93 | var algo httpsig.Algorithm = ... |
| 94 | var pubKey crypto.PublicKey = ... |
| 95 | // The verifier will verify the Digest in addition to the HTTP signature |
| 96 | return verifier.Verify(pubKey, algo) |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | `Verifier`s are not safe for concurrent use by goroutines, but since they are |
| 101 | constructed on a per-request or per-response basis it should not be a common |
| 102 | restriction. |
| 103 | |
| 104 | [Build-Status-Image]: https://travis-ci.org/42wim/httpsig.svg?branch=master |
| 105 | [Build-Status-Url]: https://travis-ci.org/42wim/httpsig |
| 106 | [Go-Reference-Image]: https://pkg.go.dev/badge/github.com/42wim/httpsig |
| 107 | [Go-Reference-Url]: https://pkg.go.dev/github.com/42wim/httpsig |
| 108 | [Go-Report-Card-Image]: https://goreportcard.com/badge/github.com/42wim/httpsig |
| 109 | [Go-Report-Card-Url]: https://goreportcard.com/report/github.com/42wim/httpsig |
| 110 | [License-Image]: https://img.shields.io/github/license/42wim/httpsig?color=blue |
| 111 | [License-Url]: https://opensource.org/licenses/BSD-3-Clause |
| 112 | [Chat-Image]: https://img.shields.io/matrix/42wim:feneas.org?server_fqdn=matrix.org |
| 113 | [Chat-Url]: https://matrix.to/#/!BLOSvIyKTDLIVjRKSc:feneas.org?via=feneas.org&via=matrix.org |
| 114 | [OpenCollective-Image]: https://img.shields.io/opencollective/backers/42wim-activitypub-labs |
| 115 | [OpenCollective-Url]: https://opencollective.com/42wim-activitypub-labs |