-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibp2p_authenticator.go
98 lines (80 loc) · 2.23 KB
/
libp2p_authenticator.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
package main
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"net/http"
p2pcrypto "github.com/libp2p/go-libp2p-crypto"
)
var (
ErrMissingProxyIDHeader = errors.New("missing Proxy-ID request header")
ErrFailedToReadRequestBody = errors.New("failed to read request body")
ErrMissingSignatureFromRequest = errors.New("missing Proxy-Signature request header")
ErrFailedToDecodeSignature = errors.New("failed to decode signature")
)
type ErrIDNotWhitelisted struct {
ID string
}
func (e ErrIDNotWhitelisted) Error() string {
return fmt.Sprintf("provided Proxy-ID is not whitelisted: %s", e.ID)
}
type ErrSignatureInvalid struct{}
func (e ErrSignatureInvalid) Error() string {
return "Request signature invalid"
}
type P2PAuthenticator struct {
pubKeyMap map[string]p2pcrypto.PubKey
}
func NewP2PAuthenticator(allowedPubKeys []string) (*P2PAuthenticator, error) {
pubKeys := map[string]p2pcrypto.PubKey{}
for _, encodedPubKey := range allowedPubKeys {
pubBytes, err := hex.DecodeString(encodedPubKey)
if err != nil {
return nil, err
}
pubKey, err := p2pcrypto.UnmarshalSecp256k1PublicKey(pubBytes)
if err != nil {
return nil, err
}
pubKeys[encodedPubKey] = pubKey
}
return &P2PAuthenticator{
pubKeyMap: pubKeys,
}, nil
}
// AuthenticateRequest checks the id of the forwarder and validates the
// signature
func (p *P2PAuthenticator) AuthenticateRequest(r *http.Request) error {
id := r.Header.Get(ProxyIDHeader)
if id == "" {
return ErrMissingProxyIDHeader
}
pubKey, ok := p.pubKeyMap[id]
if !ok {
return &ErrIDNotWhitelisted{id}
}
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return ErrFailedToReadRequestBody
}
// NOTE: this enables us to reread Body later if necessary
r.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
signature := r.Header.Get(ProxySignatureHeader)
if signature == "" {
return ErrMissingSignatureFromRequest
}
decodedSignature, err := hex.DecodeString(signature)
if err != nil {
return ErrFailedToDecodeSignature
}
isValidSignature, err := pubKey.Verify(bodyBytes, decodedSignature)
if err != nil {
return fmt.Errorf("Signature verification failed: %s", err)
}
if !isValidSignature {
return &ErrSignatureInvalid{}
}
return nil
}