-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlerex_test.go
93 lines (80 loc) · 3.42 KB
/
handlerex_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package httpsign_test
import (
"bufio"
"bytes"
"fmt"
"github.com/yaronf/httpsign"
"io"
"log"
"net/http"
"net/http/httptest"
"strings"
)
func ExampleWrapHandler_clientSigns() {
// Note: client/server examples may fail in the Go Playground, https://github.com/golang/go/issues/45855
// Callback to let the server locate its verifying key and configuration
fetchVerifier := func(r *http.Request) (string, *httpsign.Verifier) {
sigName := "sig1"
verifier, _ := httpsign.NewHMACSHA256Verifier(bytes.Repeat([]byte{0x99}, 64), httpsign.NewVerifyConfig().SetKeyID("key1"),
httpsign.Headers("@method"))
return sigName, verifier
}
// The basic handler (HTTP server) that gets wrapped
simpleHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("bar", "baz")
fmt.Fprintln(w, "Hey client, your message verified just fine")
}
// Configure the wrapper and set it up
config := httpsign.NewHandlerConfig().SetFetchVerifier(fetchVerifier)
ts := httptest.NewServer(httpsign.WrapHandler(http.HandlerFunc(simpleHandler), *config))
defer ts.Close()
// HTTP client code, with a signer
signer, _ := httpsign.NewHMACSHA256Signer(bytes.Repeat([]byte{0x99}, 64), httpsign.NewSignConfig().SetKeyID("key1"),
*httpsign.NewFields().AddHeader("content-type").AddQueryParam("pet").AddHeader("@method"))
client := httpsign.NewDefaultClient(httpsign.NewClientConfig().SetSignatureName("sig1").SetSigner(signer))
body := `{"hello": "world"}`
host := ts.URL // test server
path := "/foo?param=value&pet=dog"
res, _ := client.Post(host+path, "application/json", bufio.NewReader(strings.NewReader(body)))
serverText, _ := io.ReadAll(res.Body)
res.Body.Close()
fmt.Println("Status: ", res.Status)
fmt.Println("Server sent: ", string(serverText))
// output: Status: 200 OK
//Server sent: Hey client, your message verified just fine
}
func ExampleWrapHandler_serverSigns() {
// Note: client/server examples may fail in the Go Playground, https://github.com/golang/go/issues/45855
// Callback to let the server locate its signing key and configuration
fetchSigner := func(res http.Response, r *http.Request) (string, *httpsign.Signer) {
sigName := "sig1"
signer, _ := httpsign.NewHMACSHA256Signer(bytes.Repeat([]byte{0}, 64), httpsign.NewSignConfig().SetKeyID("key"),
httpsign.Headers("@status", "bar", "date", "content-type"))
return sigName, signer
}
simpleHandler := func(w http.ResponseWriter, r *http.Request) { // this handler gets wrapped
w.WriteHeader(200)
w.Header().Set("bar", "some text here") // note: a single word in the header value would be interpreted is a trivial dictionary!
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Hello, client")
}
// Configure the wrapper and set it up
config := httpsign.NewHandlerConfig().SetFetchSigner(fetchSigner)
ts := httptest.NewServer(httpsign.WrapHandler(http.HandlerFunc(simpleHandler), *config))
defer ts.Close()
// HTTP client code
verifier, _ := httpsign.NewHMACSHA256Verifier(bytes.Repeat([]byte{0}, 64), httpsign.NewVerifyConfig().SetKeyID("key"), *httpsign.NewFields())
client := httpsign.NewDefaultClient(httpsign.NewClientConfig().SetSignatureName("sig1").SetVerifier(verifier))
res, err := client.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
serverText, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
res.Body.Close()
fmt.Println("Server sent: ", string(serverText))
// output: Server sent: Hello, client
}