-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmessage_test.go
More file actions
48 lines (40 loc) · 1.38 KB
/
message_test.go
File metadata and controls
48 lines (40 loc) · 1.38 KB
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
package httpsign_test
import (
"bufio"
"bytes"
"fmt"
"net/http"
"strings"
"github.com/yaronf/httpsign"
)
func ExampleMessage_Verify() {
config := httpsign.NewVerifyConfig().SetKeyID("my-shared-secret").SetVerifyCreated(false) // for testing only
verifier, _ := httpsign.NewHMACSHA256Verifier(bytes.Repeat([]byte{0x77}, 64), config,
httpsign.Headers("@authority", "Date", "@method"))
reqStr := `GET /foo HTTP/1.1
Host: example.org
Date: Tue, 20 Apr 2021 02:07:55 GMT
Cache-Control: max-age=60
Signature-Input: sig77=("@authority" "date" "@method");alg="hmac-sha256";keyid="my-shared-secret"
Signature: sig77=:3e9KqLP62NHfHY5OMG4036+U6tvBowZF35ALzTjpsf0=:
`
req, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(reqStr)))
// Using WithRequest
msgWithRequest, _ := httpsign.NewMessage(httpsign.NewMessageConfig().WithRequest(req))
_, err1 := msgWithRequest.Verify("sig77", *verifier)
// Using constituent parts
msgWithConstituents, _ := httpsign.NewMessage(httpsign.NewMessageConfig().
WithMethod(req.Method).
WithURL(req.URL).
WithHeaders(req.Header).
WithTrailers(req.Trailer).
WithBody(&req.Body).
WithAuthority(req.Host).
WithScheme(req.URL.Scheme))
_, err2 := msgWithConstituents.Verify("sig77", *verifier)
fmt.Printf("WithRequest: %t\n", err1 == nil)
fmt.Printf("Constituents: %t", err2 == nil)
// Output:
// WithRequest: true
// Constituents: true
}