Skip to content

Commit

Permalink
Si 2272 web3 connector unit tests (#179)
Browse files Browse the repository at this point in the history
* WIP: Write test for web3 connector

* Write tests for EOA login verification

* Add a new test and remove rpcUrl function
  • Loading branch information
0xdev22 authored Jan 17, 2024
1 parent e3e7e5d commit 839b51f
Show file tree
Hide file tree
Showing 5 changed files with 556 additions and 42 deletions.
15 changes: 8 additions & 7 deletions connector/web3/web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package web3

import (
"fmt"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

type Config struct {
InfuraID string `json:"infuraId"`
RPCURL string `json:"rpcUrl"`
}

func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
return &web3Connector{infuraID: c.InfuraID}, nil
return &web3Connector{infuraID: c.InfuraID, rpcURL: c.RPCURL}, nil
}

type web3Connector struct {
infuraID string
rpcURL string
}

func (c *web3Connector) InfuraID() string {
Expand All @@ -43,6 +44,7 @@ func (c *web3Connector) Verify(address, msg, signedMsg string) (identity connect
signb[64] -= 27
} else if signb[64] != 0 && signb[64] != 1 {
// We allow 0 or 1 because some non-conformant devices like Ledger use it.
// TODO - Verify using ERC-1271
return identity, fmt.Errorf("byte at index 64 of signed message should be 27 or 28: %s", signedMsg)
}

Expand All @@ -63,6 +65,5 @@ func (c *web3Connector) Verify(address, msg, signedMsg string) (identity connect
}

func signHash(data []byte) []byte {
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
return crypto.Keccak256([]byte(msg))
return accounts.TextHash(data)
}
163 changes: 163 additions & 0 deletions connector/web3/web3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package web3

import (
"crypto/ecdsa"
"errors"
"fmt"
"github.com/dexidp/dex/connector"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"testing"
)

func newConnector(t *testing.T) *web3Connector {
log := logrus.New()

testConfig := Config{
InfuraID: "mockInfuraID",
}

conn, err := testConfig.Open("id", log)
if err != nil {
t.Fatal(err)
}

web3Conn, ok := conn.(*web3Connector)
if !ok {
t.Fatal(err)
}

return web3Conn
}

func generateWallet() (*ecdsa.PrivateKey, *common.Address, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
return nil, nil, err
}

userAddr := crypto.PubkeyToAddress(privateKey.PublicKey)

return privateKey, &userAddr, nil
}

func signMessage(msg string, pk *ecdsa.PrivateKey) ([]byte, []byte, error) {
data := []byte(msg)
hash := accounts.TextHash(data)

signature, err := crypto.Sign(hash, pk)
if err != nil {
return nil, nil, err
}

return signature, hash, nil
}

func TestEOALogin(t *testing.T) {
conn := newConnector(t)

type testCase struct {
address string
msg string
signedMessage string
shouldErr bool
err error
identity connector.Identity
}

pk, addr, err := generateWallet()
assert.NoError(t, err)

rawMsg := "Mock Signable Message"

testCases := map[string]func() testCase{
"decode_error_signed_message": func() testCase {
return testCase{
address: addr.Hex(),
msg: "",
signedMessage: "",
shouldErr: true,
err: errors.New("could not decode hex string of signed nonce: empty hex string"),
}
},
"v_parameter_error_signed_message": func() testCase {
sigWithInvalidVParam, hash, err := signMessage(rawMsg, pk)
assert.NoError(t, err)
sigWithInvalidVParam[64] = 100

return testCase{
address: addr.Hex(),
msg: hexutil.Encode(hash),
signedMessage: hexutil.Encode(sigWithInvalidVParam),
shouldErr: true,
err: fmt.Errorf("byte at index 64 of signed message should be 27 or 28: %s", hexutil.Encode(sigWithInvalidVParam)),
}
},
"error_mismatch_address": func() testCase {
pk2, _, err2 := generateWallet()
assert.NoError(t, err2)

sigMsg2, hash, err := signMessage(rawMsg, pk2)
assert.NoError(t, err)

return testCase{
address: addr.Hex(),
msg: hexutil.Encode(hash),
signedMessage: hexutil.Encode(sigMsg2),
shouldErr: true,
err: fmt.Errorf("given address and address recovered from signed nonce do not match"),
}
},
"success_verify_signature": func() testCase {
sigMsg, hash, err := signMessage(rawMsg, pk)
assert.NoError(t, err)
return testCase{
address: addr.Hex(),
msg: hexutil.Encode(hash),
signedMessage: hexutil.Encode(sigMsg),
shouldErr: false,
err: nil,
identity: connector.Identity{
UserID: addr.Hex(),
Username: addr.Hex(),
},
}
},
"success_verify_signature_v_value_27Or28": func() testCase {
sigMsg, hash, err := signMessage(rawMsg, pk)
assert.NoError(t, err)

sigMsg[64] += 27

return testCase{
address: addr.Hex(),
msg: hexutil.Encode(hash),
signedMessage: hexutil.Encode(sigMsg),
shouldErr: false,
err: nil,
identity: connector.Identity{
UserID: addr.Hex(),
Username: addr.Hex(),
},
}
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
tc := testCase()
res, err := conn.Verify(tc.address, rawMsg, tc.signedMessage)
if tc.shouldErr {
assert.ErrorContains(t, err, tc.err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, tc.identity, res)
}
})
}
}
4 changes: 2 additions & 2 deletions examples/config-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ connectors:
- type: web3
id: web3
name: Web3
# config:
# infuraId: $INFURA_ID
config:
infuraId: $INFURA_ID
# - type: google
# id: google
# name: Google
Expand Down
68 changes: 57 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/coreos/go-oidc/v3 v3.5.0
github.com/dexidp/dex/api/v2 v2.1.0
github.com/ethereum/go-ethereum v1.12.0
github.com/ethereum/go-ethereum v1.13.8
github.com/felixge/httpsnoop v1.0.3
github.com/ghodss/yaml v1.0.0
github.com/go-ldap/ldap/v3 v3.4.4
Expand All @@ -30,12 +30,12 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spruceid/siwe-go v0.2.0
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
go.etcd.io/etcd/client/pkg/v3 v3.5.7
go.etcd.io/etcd/client/v3 v3.5.7
golang.org/x/crypto v0.10.0
golang.org/x/exp v0.0.0-20230206171751-46f607a40771
golang.org/x/net v0.10.0
golang.org/x/crypto v0.17.0
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
golang.org/x/net v0.18.0
golang.org/x/oauth2 v0.6.0
google.golang.org/api v0.114.0
google.golang.org/grpc v1.53.0
Expand All @@ -48,60 +48,106 @@ require (
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e // indirect
github.com/DataDog/zstd v1.4.5 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.8.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593 // indirect
github.com/cockroachdb/redact v1.0.8 // indirect
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dchest/uniuri v1.2.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/holiman/uint256 v1.2.2 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/relvacode/iso8601 v1.3.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.7 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/dexidp/dex/api/v2 => ./api/v2
Loading

0 comments on commit 839b51f

Please sign in to comment.