-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverify.go
160 lines (127 loc) · 3.4 KB
/
verify.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package signature_header
import (
"crypto"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"net/http"
"strings"
jsonld_helper "github.com/cloudmatelabs/go-jsonld-helper"
"github.com/go-fed/httpsig"
)
type Verifier struct {
Method string
URL string
Headers map[string]string
request *http.Request
algorithm httpsig.Algorithm
verifier httpsig.Verifier
}
func (v *Verifier) VerifyWithPublicKey(publicKey crypto.PublicKey) error {
if err := v.init(); err != nil {
return err
}
return v.verifier.Verify(publicKey, v.algorithm)
}
func (v *Verifier) VerifyWithPublicKeyStr(publicKeyStr string) error {
publicKey, err := publicKeyFromStr(publicKeyStr)
if err != nil {
return err
}
return v.VerifyWithPublicKey(publicKey)
}
func (v *Verifier) VerifyWithActor(actor string) error {
publicKey, err := publicKeyFromActor(actor)
if err != nil {
return err
}
return v.VerifyWithPublicKey(publicKey)
}
func (v *Verifier) VerifyWithBody(body []byte) error {
actor, err := getActor(body)
if err != nil {
return err
}
publicKey, err := publicKeyFromActor(actor)
if err != nil {
return err
}
return v.VerifyWithPublicKey(publicKey)
}
func (v *Verifier) init() (err error) {
if v.Headers == nil {
return fmt.Errorf("headers is required")
}
v.request, _ = http.NewRequest(v.Method, v.URL, nil)
for key, value := range v.Headers {
v.request.Header.Set(key, value)
}
v.algorithm, err = algorithmFromHeaders(v.request)
if err != nil {
return err
}
v.verifier, _ = httpsig.NewVerifier(v.request)
return
}
func getActor(body []byte) (string, error) {
jsonld, err := jsonld_helper.ParseJsonLD(body, nil)
if err != nil {
return "", err
}
return jsonld.ReadKey("actor").StringOrThrow(nil)
}
func publicKeyFromActor(actor string) (crypto.PublicKey, error) {
publicKeyStr, err := fetchPublicKey(actor)
if err != nil {
return nil, err
}
return publicKeyFromStr(publicKeyStr)
}
func publicKeyFromStr(publicKeyStr string) (crypto.PublicKey, error) {
block, _ := pem.Decode([]byte(publicKeyStr))
return x509.ParsePKIXPublicKey(block.Bytes)
}
func fetchPublicKey(actor string) (string, error) {
request, err := http.NewRequest("GET", actor, nil)
if err != nil {
return "", err
}
request.Header.Add("Accept", "application/ld+json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
body, _ := io.ReadAll(response.Body)
jsonld, err := jsonld_helper.ParseJsonLD(body, nil)
if err != nil {
return "", err
}
publicKey, err := jsonld.ReadKey("publicKey").ReadKey("publicKeyPem").StringOrThrow(nil)
return publicKey, err
}
func algorithmFromHeaders(r *http.Request) (httpsig.Algorithm, error) {
signature := r.Header.Get("Signature")
if signature != "" {
return algorithmFromSignature(string(signature))
}
authorization := r.Header.Get("Authorization")
if authorization != "" {
return algorithmFromSignature(strings.Replace(string(authorization), "Signature ", "", 1))
}
return httpsig.RSA_SHA256, fmt.Errorf("algorithm not found")
}
func algorithmFromSignature(signature string) (httpsig.Algorithm, error) {
for _, field := range strings.Split(signature, ",") {
if strings.HasPrefix(field, "algorithm=") {
return httpsig.Algorithm(strings.ReplaceAll(
strings.Replace(field, "algorithm=", "", -1),
`"`,
"",
)), nil
}
}
return httpsig.RSA_SHA256, fmt.Errorf("algorithm not found")
}