Skip to content

Commit

Permalink
Rename PeerIDAuth to ServerPeerIDAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jul 9, 2024
1 parent 7f42943 commit c2d12e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions p2p/http/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func TestMutualAuth(t *testing.T) {

type serverTestCase struct {
name string
serverGen func(t *testing.T) (*httptest.Server, *PeerIDAuth)
serverGen func(t *testing.T) (*httptest.Server, *ServerPeerIDAuth)
}

serverTestCases := []serverTestCase{
{
name: "no TLS",
serverGen: func(t *testing.T) (*httptest.Server, *PeerIDAuth) {
serverGen: func(t *testing.T) (*httptest.Server, *ServerPeerIDAuth) {
t.Helper()
auth := PeerIDAuth{
auth := ServerPeerIDAuth{
PrivKey: serverKey,
ValidHostnames: map[string]struct{}{"example.com": {}},
TokenTTL: time.Hour,
Expand All @@ -78,9 +78,9 @@ func TestMutualAuth(t *testing.T) {
},
{
name: "TLS",
serverGen: func(t *testing.T) (*httptest.Server, *PeerIDAuth) {
serverGen: func(t *testing.T) (*httptest.Server, *ServerPeerIDAuth) {
t.Helper()
auth := PeerIDAuth{
auth := ServerPeerIDAuth{
PrivKey: serverKey,
ValidHostnames: map[string]struct{}{"example.com": {}},
TokenTTL: time.Hour,
Expand Down Expand Up @@ -222,7 +222,7 @@ func FuzzServeHTTP(f *testing.F) {
zeroBytes := make([]byte, 64)
serverKey, _, err := crypto.GenerateEd25519Key(bytes.NewReader(zeroBytes))
require.NoError(f, err)
auth := PeerIDAuth{
auth := ServerPeerIDAuth{
PrivKey: serverKey,
ValidHostnames: map[string]struct{}{"example.com": {}},
TokenTTL: time.Hour,
Expand Down Expand Up @@ -251,7 +251,7 @@ func BenchmarkAuths(b *testing.B) {
zeroBytes := make([]byte, 64)
serverKey, _, err := crypto.GenerateEd25519Key(bytes.NewReader(zeroBytes))
require.NoError(b, err)
auth := PeerIDAuth{
auth := ServerPeerIDAuth{
PrivKey: serverKey,
ValidHostnames: map[string]struct{}{"example.com": {}},
TokenTTL: time.Hour,
Expand Down
14 changes: 7 additions & 7 deletions p2p/http/auth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const maxAuthHeaderSize = 8192

const challengeTTL = 5 * time.Minute

type PeerIDAuth struct {
type ServerPeerIDAuth struct {
PrivKey crypto.PrivKey
ValidHostnames map[string]struct{}
TokenTTL time.Duration
Expand All @@ -35,7 +35,7 @@ var errMissingAuthHeader = errors.New("missing header")
// attempt to authenticate the request using using the libp2p peer ID auth
// scheme. If a Next handler is set, it will be called on authenticated
// requests.
func (a *PeerIDAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (a *ServerPeerIDAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hostname := r.Host
if !a.InsecureNoTLS {
if r.TLS == nil {
Expand Down Expand Up @@ -142,7 +142,7 @@ func (a *PeerIDAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.Next.ServeHTTP(w, r)
}

func (a *PeerIDAuth) signChallengeServer(challengeServerB64 string, client peer.ID, hostname string) ([]byte, error) {
func (a *ServerPeerIDAuth) signChallengeServer(challengeServerB64 string, client peer.ID, hostname string) ([]byte, error) {
if len(challengeServerB64) == 0 {
return nil, errors.New("missing challenge")
}
Expand All @@ -158,7 +158,7 @@ func (a *PeerIDAuth) signChallengeServer(challengeServerB64 string, client peer.
return sig, nil
}

func (a *PeerIDAuth) authenticate(f authFields) (peer.ID, error) {
func (a *ServerPeerIDAuth) authenticate(f authFields) (peer.ID, error) {
partsToVerify := make([]string, 0, 2)
o, err := getChallengeFromOpaque(a.PrivKey, []byte(f.opaque))
if err != nil {
Expand All @@ -180,7 +180,7 @@ func (a *PeerIDAuth) authenticate(f authFields) (peer.ID, error) {
return peer.IDFromPublicKey(f.pubKey)
}

func (a *PeerIDAuth) UnwrapBearerToken(r *http.Request, expectedHostname string) (peer.ID, error) {
func (a *ServerPeerIDAuth) UnwrapBearerToken(r *http.Request, expectedHostname string) (peer.ID, error) {
if !strings.Contains(r.Header.Get("Authorization"), BearerAuthScheme) {
return "", errors.New("missing bearer auth scheme")
}
Expand All @@ -195,7 +195,7 @@ func (a *PeerIDAuth) UnwrapBearerToken(r *http.Request, expectedHostname string)
return a.unwrapBearerToken(expectedHostname, bearerScheme)
}

func (a *PeerIDAuth) unwrapBearerToken(expectedHostname string, s authScheme) (peer.ID, error) {
func (a *ServerPeerIDAuth) unwrapBearerToken(expectedHostname string, s authScheme) (peer.ID, error) {
buf := pool.Get(4096)
defer pool.Put(buf)
buf, err := b64AppendDecode(buf[:0], []byte(s.bearerToken))
Expand Down Expand Up @@ -404,7 +404,7 @@ func genOpaqueFromChallenge(buf []byte, now time.Time, privKey crypto.PrivKey, c
return buf, nil
}

func (a *PeerIDAuth) serveAuthReq(w http.ResponseWriter) {
func (a *ServerPeerIDAuth) serveAuthReq(w http.ResponseWriter) {
var challenge [challengeLen]byte
_, err := rand.Read(challenge[:])
if err != nil {
Expand Down

0 comments on commit c2d12e7

Please sign in to comment.