change chainID from an array to 1 chain only

This commit is contained in:
Tak Wai Wong 2022-10-21 11:04:26 -07:00
parent ec486f9f54
commit dc240d48a4
8 changed files with 51 additions and 65 deletions

View file

@ -129,7 +129,7 @@ func (pk LoginPublicKeyEthereum) ValidateLoginResponse() (bool, *jsonerror.Matri
} }
// Error if the chainId is not supported by the server. // Error if the chainId is not supported by the server.
if !contains(pk.config.PublicKeyAuthentication.Ethereum.ChainIDs, message.GetChainID()) { if pk.config.PublicKeyAuthentication.Ethereum.ChainID != message.GetChainID() {
return false, jsonerror.Forbidden("chainId") return false, jsonerror.Forbidden("chainId")
} }
@ -156,12 +156,3 @@ func (pk LoginPublicKeyEthereum) verifyMessageUserId(message *siwe.Message) bool
// one derived from the signed message. // one derived from the signed message.
return pk.UserId == strings.ToLower(expectedUserId) return pk.UserId == strings.ToLower(expectedUserId)
} }
func contains(list []int, element int) bool {
for _, i := range list {
if i == element {
return true
}
}
return false
}

View file

@ -24,7 +24,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/internal/mapsutil" "github.com/matrix-org/dendrite/internal/mapsutil"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/test" testutil "github.com/matrix-org/dendrite/test"
uapi "github.com/matrix-org/dendrite/userapi/api" uapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -34,20 +34,18 @@ type loginContext struct {
userInteractive *UserInteractive userInteractive *UserInteractive
} }
func createLoginContext(t *testing.T) *loginContext { func createLoginContext(_ *testing.T) *loginContext {
chainIds := []int{4}
cfg := &config.ClientAPI{ cfg := &config.ClientAPI{
Matrix: &config.Global{ Matrix: &config.Global{
ServerName: test.TestServerName, ServerName: testutil.TestServerName,
}, },
Derived: &config.Derived{}, Derived: &config.Derived{},
PasswordAuthenticationDisabled: true, PasswordAuthenticationDisabled: true,
PublicKeyAuthentication: config.PublicKeyAuthentication{ PublicKeyAuthentication: config.PublicKeyAuthentication{
Ethereum: config.EthereumAuthConfig{ Ethereum: config.EthereumAuthConfig{
Enabled: true, Enabled: true,
Version: 1, Version: 1,
ChainIDs: chainIds, ChainID: testutil.EthereumTestNetworkId,
}, },
}, },
} }
@ -154,9 +152,9 @@ func TestLoginPublicKeyEthereum(t *testing.T) {
var userAPI fakePublicKeyUserApi var userAPI fakePublicKeyUserApi
ctx := context.Background() ctx := context.Background()
loginContext := createLoginContext(t) loginContext := createLoginContext(t)
wallet, _ := test.CreateTestAccount() wallet, _ := testutil.CreateTestAccount()
message, _ := test.CreateEip4361TestMessage(wallet.PublicAddress) message, _ := testutil.CreateEip4361TestMessage(wallet.PublicAddress)
signature, _ := test.SignMessage(message.String(), wallet.PrivateKey) signature, _ := testutil.SignMessage(message.String(), wallet.PrivateKey)
sessionId := publicKeyTestSession( sessionId := publicKeyTestSession(
&ctx, &ctx,
loginContext.config, loginContext.config,
@ -165,7 +163,7 @@ func TestLoginPublicKeyEthereum(t *testing.T) {
) )
// Escape \t and \n. Work around for marshalling and unmarshalling message. // Escape \t and \n. Work around for marshalling and unmarshalling message.
msgStr := test.FromEip4361MessageToString(message) msgStr := testutil.FromEip4361MessageToString(message)
body := fmt.Sprintf(`{ body := fmt.Sprintf(`{
"type": "m.login.publickey", "type": "m.login.publickey",
"auth": { "auth": {
@ -219,8 +217,8 @@ func TestLoginPublicKeyEthereumMissingSignature(t *testing.T) {
var userAPI fakePublicKeyUserApi var userAPI fakePublicKeyUserApi
ctx := context.Background() ctx := context.Background()
loginContext := createLoginContext(t) loginContext := createLoginContext(t)
wallet, _ := test.CreateTestAccount() wallet, _ := testutil.CreateTestAccount()
message, _ := test.CreateEip4361TestMessage(wallet.PublicAddress) message, _ := testutil.CreateEip4361TestMessage(wallet.PublicAddress)
sessionId := publicKeyTestSession( sessionId := publicKeyTestSession(
&ctx, &ctx,
loginContext.config, loginContext.config,
@ -229,7 +227,7 @@ func TestLoginPublicKeyEthereumMissingSignature(t *testing.T) {
) )
// Escape \t and \n. Work around for marshalling and unmarshalling message. // Escape \t and \n. Work around for marshalling and unmarshalling message.
msgStr := test.FromEip4361MessageToString(message) msgStr := testutil.FromEip4361MessageToString(message)
body := fmt.Sprintf(`{ body := fmt.Sprintf(`{
"type": "m.login.publickey", "type": "m.login.publickey",
"auth": { "auth": {
@ -280,7 +278,7 @@ func TestLoginPublicKeyEthereumEmptyMessage(t *testing.T) {
var userAPI fakePublicKeyUserApi var userAPI fakePublicKeyUserApi
ctx := context.Background() ctx := context.Background()
loginContext := createLoginContext(t) loginContext := createLoginContext(t)
wallet, _ := test.CreateTestAccount() wallet, _ := testutil.CreateTestAccount()
sessionId := publicKeyTestSession( sessionId := publicKeyTestSession(
&ctx, &ctx,
loginContext.config, loginContext.config,
@ -333,7 +331,7 @@ func TestLoginPublicKeyEthereumWrongUserId(t *testing.T) {
var userAPI fakePublicKeyUserApi var userAPI fakePublicKeyUserApi
ctx := context.Background() ctx := context.Background()
loginContext := createLoginContext(t) loginContext := createLoginContext(t)
wallet, _ := test.CreateTestAccount() wallet, _ := testutil.CreateTestAccount()
sessionId := publicKeyTestSession( sessionId := publicKeyTestSession(
&ctx, &ctx,
loginContext.config, loginContext.config,

View file

@ -22,6 +22,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
testutil "github.com/matrix-org/dendrite/test"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -72,7 +73,10 @@ func TestLoginPublicKeyNewSession(t *testing.T) {
params, params,
"[object]") "[object]")
ethParams := params.(config.EthereumAuthParams) ethParams := params.(config.EthereumAuthParams)
assert.NotEmptyf(ethParams.ChainIDs, "ChainIDs actual: empty, expected not empty") assert.Equalf(
testutil.EthereumTestNetworkId,
ethParams.ChainID,
"ChainID actual: %d, expected %d", ethParams.ChainID, testutil.EthereumTestNetworkId)
assert.NotEmptyf(ethParams.Version, "Version actual: \"\", expected: not empty") assert.NotEmptyf(ethParams.Version, "Version actual: \"\", expected: not empty")
} }

View file

@ -26,8 +26,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/mapsutil" "github.com/matrix-org/dendrite/internal/mapsutil"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/test" testutil "github.com/matrix-org/dendrite/test"
"github.com/matrix-org/dendrite/userapi/api"
uapi "github.com/matrix-org/dendrite/userapi/api" uapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -40,20 +39,18 @@ type registerContext struct {
userInteractive *auth.UserInteractive userInteractive *auth.UserInteractive
} }
func createRegisterContext(t *testing.T) *registerContext { func createRegisterContext(_ *testing.T) *registerContext {
chainIds := []int{4}
cfg := &config.ClientAPI{ cfg := &config.ClientAPI{
Matrix: &config.Global{ Matrix: &config.Global{
ServerName: test.TestServerName, ServerName: testutil.TestServerName,
}, },
Derived: &config.Derived{}, Derived: &config.Derived{},
PasswordAuthenticationDisabled: true, PasswordAuthenticationDisabled: true,
PublicKeyAuthentication: config.PublicKeyAuthentication{ PublicKeyAuthentication: config.PublicKeyAuthentication{
Ethereum: config.EthereumAuthConfig{ Ethereum: config.EthereumAuthConfig{
Enabled: true, Enabled: true,
Version: 1, Version: 1,
ChainIDs: chainIds, ChainID: testutil.EthereumTestNetworkId,
}, },
}, },
} }
@ -129,7 +126,7 @@ func (ua *fakePublicKeyUserApi) PerformDeviceCreation(
req *uapi.PerformDeviceCreationRequest, req *uapi.PerformDeviceCreationRequest,
res *uapi.PerformDeviceCreationResponse) error { res *uapi.PerformDeviceCreationResponse) error {
res.DeviceCreated = true res.DeviceCreated = true
res.Device = &api.Device{ res.Device = &uapi.Device{
ID: "device_id", ID: "device_id",
UserID: req.Localpart, UserID: req.Localpart,
AccessToken: req.AccessToken, AccessToken: req.AccessToken,
@ -142,11 +139,11 @@ func (ua *fakePublicKeyUserApi) PerformAccountCreation(
req *uapi.PerformAccountCreationRequest, req *uapi.PerformAccountCreationRequest,
res *uapi.PerformAccountCreationResponse) error { res *uapi.PerformAccountCreationResponse) error {
res.AccountCreated = true res.AccountCreated = true
res.Account = &api.Account{ res.Account = &uapi.Account{
AppServiceID: req.AppServiceID, AppServiceID: req.AppServiceID,
Localpart: req.Localpart, Localpart: req.Localpart,
ServerName: test.TestServerName, ServerName: testutil.TestServerName,
UserID: fmt.Sprintf("@%s:%s", req.Localpart, test.TestServerName), UserID: fmt.Sprintf("@%s:%s", req.Localpart, testutil.TestServerName),
AccountType: req.AccountType, AccountType: req.AccountType,
} }
return nil return nil
@ -173,8 +170,6 @@ func (*fakePublicKeyUserApi) QueryLoginToken(ctx context.Context, req *uapi.Quer
func newRegistrationSession( func newRegistrationSession(
t *testing.T, t *testing.T,
userId string, userId string,
cfg *config.ClientAPI,
userInteractive *auth.UserInteractive,
userAPI *fakePublicKeyUserApi, userAPI *fakePublicKeyUserApi,
) string { ) string {
body := fmt.Sprintf(`{ body := fmt.Sprintf(`{
@ -214,20 +209,18 @@ func newRegistrationSession(
func TestRegisterEthereum(t *testing.T) { func TestRegisterEthereum(t *testing.T) {
// Setup // Setup
var userAPI fakePublicKeyUserApi var userAPI fakePublicKeyUserApi
wallet, _ := test.CreateTestAccount() wallet, _ := testutil.CreateTestAccount()
message, _ := test.CreateEip4361TestMessage(wallet.PublicAddress) message, _ := testutil.CreateEip4361TestMessage(wallet.PublicAddress)
signature, _ := test.SignMessage(message.String(), wallet.PrivateKey) signature, _ := testutil.SignMessage(message.String(), wallet.PrivateKey)
registerContext := createRegisterContext(t) registerContext := createRegisterContext(t)
sessionId := newRegistrationSession( sessionId := newRegistrationSession(
t, t,
wallet.Eip155UserId, wallet.Eip155UserId,
registerContext.config,
registerContext.userInteractive,
&userAPI, &userAPI,
) )
// Escape \t and \n. Work around for marshalling and unmarshalling message. // Escape \t and \n. Work around for marshalling and unmarshalling message.
msgStr := test.FromEip4361MessageToString(message) msgStr := testutil.FromEip4361MessageToString(message)
body := fmt.Sprintf(`{ body := fmt.Sprintf(`{
"username": "%v", "username": "%v",
"auth": { "auth": {
@ -339,7 +332,10 @@ func TestNewRegistrationSession(t *testing.T) {
params, params,
"[object]") "[object]")
ethParams := params.(config.EthereumAuthParams) ethParams := params.(config.EthereumAuthParams)
assert.NotEmptyf(ethParams.ChainIDs, "ChainIDs actual: empty, expected not empty") assert.Equalf(
testutil.EthereumTestNetworkId,
ethParams.ChainID,
"ChainID actual: %d, expected %d", ethParams.ChainID, testutil.EthereumTestNetworkId)
assert.NotEmptyf(ethParams.Version, "Version actual: \"\", expected: not empty") assert.NotEmptyf(ethParams.Version, "Version actual: \"\", expected: not empty")
} }

View file

@ -178,7 +178,7 @@ client_api:
ethereum: ethereum:
enabled: false enabled: false
version: 1 version: 1
chain_ids: [] chain_id: 1337
# Whether to require reCAPTCHA for registration. If you have enabled registration # Whether to require reCAPTCHA for registration. If you have enabled registration
# then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used # then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used

View file

@ -174,7 +174,7 @@ client_api:
ethereum: ethereum:
enabled: false enabled: false
version: 1 version: 1
chain_ids: [] chain_id: 1337
# Whether to require reCAPTCHA for registration. If you have enabled registration # Whether to require reCAPTCHA for registration. If you have enabled registration
# then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used # then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used

View file

@ -9,21 +9,18 @@ type AuthParams interface {
} }
type EthereumAuthParams struct { type EthereumAuthParams struct {
Version uint `json:"version"` Version uint `json:"version"`
ChainIDs []int `json:"chain_ids"` ChainID int `json:"chain_id"`
} }
func (p EthereumAuthParams) GetParams() interface{} { func (p EthereumAuthParams) GetParams() interface{} {
copyP := p return p
copyP.ChainIDs = make([]int, len(p.ChainIDs))
copy(copyP.ChainIDs, p.ChainIDs)
return copyP
} }
type EthereumAuthConfig struct { type EthereumAuthConfig struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
Version uint `yaml:"version"` Version uint `yaml:"version"`
ChainIDs []int `yaml:"chain_ids"` ChainID int `yaml:"chain_id"`
} }
type PublicKeyAuthentication struct { type PublicKeyAuthentication struct {
@ -47,8 +44,8 @@ func (pk *PublicKeyAuthentication) GetPublicKeyRegistrationParams() map[string]i
params := make(map[string]interface{}) params := make(map[string]interface{})
if pk.Ethereum.Enabled { if pk.Ethereum.Enabled {
p := EthereumAuthParams{ p := EthereumAuthParams{
Version: pk.Ethereum.Version, Version: pk.Ethereum.Version,
ChainIDs: pk.Ethereum.ChainIDs, ChainID: pk.Ethereum.ChainID,
} }
params[authtypes.LoginTypePublicKeyEthereum] = p params[authtypes.LoginTypePublicKeyEthereum] = p
} }

View file

@ -25,7 +25,7 @@ import (
"github.com/spruceid/siwe-go" "github.com/spruceid/siwe-go"
) )
const EthereumTestNetworkId = 4 // Rinkeby test network ID const EthereumTestNetworkId = 1337 // Localhost chain ID
const TestServerName = "localhost" const TestServerName = "localhost"
type EthereumTestWallet struct { type EthereumTestWallet struct {
@ -66,7 +66,7 @@ func CreateEip4361TestMessage(
publicAddress string, publicAddress string,
) (*siwe.Message, error) { ) (*siwe.Message, error) {
options := make(map[string]interface{}) options := make(map[string]interface{})
options["chainId"] = 4 // Rinkeby test network options["chainId"] = EthereumTestNetworkId
options["statement"] = "This is a test statement" options["statement"] = "This is a test statement"
message, err := siwe.InitMessage( message, err := siwe.InitMessage(
TestServerName, TestServerName,