Skip to content

Commit 9182242

Browse files
committed
doc: add ExampleMessage_Verify
1 parent c165542 commit 9182242

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

message_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package httpsign_test
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"fmt"
7+
"net/http"
8+
"strings"
9+
10+
"github.com/yaronf/httpsign"
11+
)
12+
13+
func ExampleMessage_Verify() {
14+
config := httpsign.NewVerifyConfig().SetKeyID("my-shared-secret").SetVerifyCreated(false) // for testing only
15+
verifier, _ := httpsign.NewHMACSHA256Verifier(bytes.Repeat([]byte{0x77}, 64), config,
16+
httpsign.Headers("@authority", "Date", "@method"))
17+
reqStr := `GET /foo HTTP/1.1
18+
Host: example.org
19+
Date: Tue, 20 Apr 2021 02:07:55 GMT
20+
Cache-Control: max-age=60
21+
Signature-Input: sig77=("@authority" "date" "@method");alg="hmac-sha256";keyid="my-shared-secret"
22+
Signature: sig77=:3e9KqLP62NHfHY5OMG4036+U6tvBowZF35ALzTjpsf0=:
23+
24+
`
25+
req, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(reqStr)))
26+
27+
// Using WithRequest
28+
msgWithRequest, _ := httpsign.NewMessage(httpsign.NewMessageConfig().WithRequest(req))
29+
_, err1 := msgWithRequest.Verify("sig77", *verifier)
30+
31+
// Using constituent parts
32+
msgWithConstituents, _ := httpsign.NewMessage(httpsign.NewMessageConfig().
33+
WithMethod(req.Method).
34+
WithURL(req.URL).
35+
WithHeaders(req.Header).
36+
WithTrailers(req.Trailer).
37+
WithBody(&req.Body).
38+
WithAuthority(req.Host).
39+
WithScheme(req.URL.Scheme))
40+
41+
_, err2 := msgWithConstituents.Verify("sig77", *verifier)
42+
43+
fmt.Printf("WithRequest: %t\n", err1 == nil)
44+
fmt.Printf("Constituents: %t", err2 == nil)
45+
// Output:
46+
// WithRequest: true
47+
// Constituents: true
48+
}

0 commit comments

Comments
 (0)