-
Notifications
You must be signed in to change notification settings - Fork 15
/
examples_test.go
53 lines (40 loc) · 1.23 KB
/
examples_test.go
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
package httpsignatures_test
import (
"net/http"
"github.com/99designs/httpsignatures-go"
)
func Example_signing() {
r, _ := http.NewRequest("GET", "http://example.com/some-api", nil)
// Sign using the 'Signature' header
httpsignatures.DefaultSha256Signer.SignRequest("KeyId", "Key", r)
// OR Sign using the 'Authorization' header
httpsignatures.DefaultSha256Signer.AuthRequest("KeyId", "Key", r)
http.DefaultClient.Do(r)
}
func Example_customSigning() {
signer := httpsignatures.NewSigner(
httpsignatures.AlgorithmHmacSha256,
httpsignatures.RequestTarget, "date", "content-length",
)
r, _ := http.NewRequest("GET", "http://example.com/some-api", nil)
signer.SignRequest("KeyId", "Key", r)
http.DefaultClient.Do(r)
}
func Example_verification() {
_ = func(w http.ResponseWriter, r *http.Request) {
sig, err := httpsignatures.FromRequest(r)
if err != nil {
// Probably a malformed header
http.Error(w, "Bad Request", http.StatusBadRequest)
panic(err)
}
// if you have headers that must be signed check
// that they are in sig.Headers
var key string // = lookup using sig.KeyID
if !sig.IsValid(key, r) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
// request was signed correctly.
}
}