Skip to content

Commit 50044c3

Browse files
committed
refactor(p2p/http/auth): Support RoundTripper for authentication
Modify authentication methods to work with http.RoundTripper
1 parent d754491 commit 50044c3

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

p2p/http/auth/client.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@ type ClientPeerIDAuth struct {
2020
tm tokenMap
2121
}
2222

23+
type clientAsRoundTripper struct {
24+
*http.Client
25+
}
26+
27+
func (c clientAsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
28+
return c.Client.Do(req)
29+
}
30+
2331
// AuthenticatedDo is like http.Client.Do, but it does the libp2p peer ID auth
2432
// handshake if needed.
2533
//
2634
// It is recommended to pass in an http.Request with `GetBody` set, so that this
2735
// method can retry sending the request in case a previously used token has
2836
// expired.
2937
func (a *ClientPeerIDAuth) AuthenticatedDo(client *http.Client, req *http.Request) (peer.ID, *http.Response, error) {
38+
return a.AuthenticateWithRoundTripper(clientAsRoundTripper{client}, req)
39+
}
40+
41+
func (a *ClientPeerIDAuth) AuthenticateWithRoundTripper(rt http.RoundTripper, req *http.Request) (peer.ID, *http.Response, error) {
3042
hostname := req.Host
3143
ti, hasToken := a.tm.get(hostname, a.TokenTTL)
3244
handshake := handshake.PeerIDAuthHandshakeClient{
@@ -36,7 +48,7 @@ func (a *ClientPeerIDAuth) AuthenticatedDo(client *http.Client, req *http.Reques
3648

3749
if hasToken {
3850
// We have a token. Attempt to use that, but fallback to server initiated challenge if it fails.
39-
peer, resp, err := a.doWithToken(client, req, ti)
51+
peer, resp, err := a.doWithToken(rt, req, ti)
4052
switch {
4153
case err == nil:
4254
return peer, resp, nil
@@ -62,7 +74,7 @@ func (a *ClientPeerIDAuth) AuthenticatedDo(client *http.Client, req *http.Reques
6274
handshake.SetInitiateChallenge()
6375
}
6476

65-
serverPeerID, resp, err := a.runHandshake(client, req, clearBody(req), &handshake)
77+
serverPeerID, resp, err := a.runHandshake(rt, req, clearBody(req), &handshake)
6678
if err != nil {
6779
return "", nil, fmt.Errorf("failed to run handshake: %w", err)
6880
}
@@ -74,7 +86,7 @@ func (a *ClientPeerIDAuth) AuthenticatedDo(client *http.Client, req *http.Reques
7486
return serverPeerID, resp, nil
7587
}
7688

77-
func (a *ClientPeerIDAuth) runHandshake(client *http.Client, req *http.Request, b bodyMeta, hs *handshake.PeerIDAuthHandshakeClient) (peer.ID, *http.Response, error) {
89+
func (a *ClientPeerIDAuth) runHandshake(rt http.RoundTripper, req *http.Request, b bodyMeta, hs *handshake.PeerIDAuthHandshakeClient) (peer.ID, *http.Response, error) {
7890
maxSteps := 5 // Avoid infinite loops in case of buggy handshake. Shouldn't happen.
7991
var resp *http.Response
8092

@@ -92,7 +104,7 @@ func (a *ClientPeerIDAuth) runHandshake(client *http.Client, req *http.Request,
92104
b.setBody(req)
93105
}
94106

95-
resp, err = client.Do(req)
107+
resp, err = rt.RoundTrip(req)
96108
if err != nil {
97109
return "", nil, err
98110
}
@@ -119,10 +131,10 @@ func (a *ClientPeerIDAuth) runHandshake(client *http.Client, req *http.Request,
119131

120132
var errTokenRejected = errors.New("token rejected")
121133

122-
func (a *ClientPeerIDAuth) doWithToken(client *http.Client, req *http.Request, ti tokenInfo) (peer.ID, *http.Response, error) {
134+
func (a *ClientPeerIDAuth) doWithToken(rt http.RoundTripper, req *http.Request, ti tokenInfo) (peer.ID, *http.Response, error) {
123135
// Try to make the request with the token
124136
req.Header.Set("Authorization", ti.token)
125-
resp, err := client.Do(req)
137+
resp, err := rt.RoundTrip(req)
126138
if err != nil {
127139
return "", nil, err
128140
}

p2p/http/libp2phttp.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,7 @@ func (h *Host) RoundTrip(r *http.Request) (*http.Response, error) {
802802
r.Host = u.Host
803803
}
804804

805-
// thin client as ClientPeerIDAuth expects an *http.Client
806-
c := http.Client{Transport: rt}
807-
serverID, resp, err := h.ClientPeerIDAuth.AuthenticatedDo(&c, r)
805+
serverID, resp, err := h.ClientPeerIDAuth.AuthenticateWithRoundTripper(rt, r)
808806
if err != nil {
809807
return nil, err
810808
}

0 commit comments

Comments
 (0)