diff --git a/clientapi/auth/login_publickey_ethereum.go b/clientapi/auth/login_publickey_ethereum.go index abf1d2032..1ce41d886 100644 --- a/clientapi/auth/login_publickey_ethereum.go +++ b/clientapi/auth/login_publickey_ethereum.go @@ -129,7 +129,7 @@ func (pk LoginPublicKeyEthereum) ValidateLoginResponse() (bool, *jsonerror.Matri } // Error if the chainId is not supported by the server. - if pk.config.PublicKeyAuthentication.Ethereum.ChainID != message.GetChainID() { + if pk.config.PublicKeyAuthentication.Ethereum.GetChainID() != message.GetChainID() { return false, jsonerror.Forbidden("chainId") } diff --git a/clientapi/auth/login_publickey_ethereum_test.go b/clientapi/auth/login_publickey_ethereum_test.go index a8d3a710a..cd7db05b2 100644 --- a/clientapi/auth/login_publickey_ethereum_test.go +++ b/clientapi/auth/login_publickey_ethereum_test.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "net/http" + "strconv" "strings" "testing" @@ -43,9 +44,9 @@ func createLoginContext(_ *testing.T) *loginContext { PasswordAuthenticationDisabled: true, PublicKeyAuthentication: config.PublicKeyAuthentication{ Ethereum: config.EthereumAuthConfig{ - Enabled: true, - Version: 1, - ChainID: testutil.EthereumTestNetworkId, + Enabled: true, + Version: 1, + ConfigChainID: strconv.Itoa(testutil.EthereumTestNetworkId), }, }, } diff --git a/clientapi/authorization/authorization.go b/clientapi/authorization/authorization.go index 3efb37e39..f81513d76 100644 --- a/clientapi/authorization/authorization.go +++ b/clientapi/authorization/authorization.go @@ -10,7 +10,7 @@ import ( func NewAuthorization(cfg *config.ClientAPI, rsAPI roomserver.ClientRoomserverAPI) authorization.Authorization { // Load authorization manager for Zion - if cfg.PublicKeyAuthentication.Ethereum.EnableAuthz { + if cfg.PublicKeyAuthentication.Ethereum.GetEnableAuthZ() { auth, err := zion.NewZionAuthorization(cfg, rsAPI) if err != nil { diff --git a/clientapi/routing/register_publickey_test.go b/clientapi/routing/register_publickey_test.go index e1a225cbd..961873b89 100644 --- a/clientapi/routing/register_publickey_test.go +++ b/clientapi/routing/register_publickey_test.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" "strings" "testing" @@ -48,9 +49,9 @@ func createRegisterContext(_ *testing.T) *registerContext { PasswordAuthenticationDisabled: true, PublicKeyAuthentication: config.PublicKeyAuthentication{ Ethereum: config.EthereumAuthConfig{ - Enabled: true, - Version: 1, - ChainID: testutil.EthereumTestNetworkId, + Enabled: true, + Version: 1, + ConfigChainID: strconv.Itoa(testutil.EthereumTestNetworkId), }, }, } diff --git a/dendrite-sample.monolith.yaml b/dendrite-sample.monolith.yaml index 715534528..cbe930c51 100644 --- a/dendrite-sample.monolith.yaml +++ b/dendrite-sample.monolith.yaml @@ -179,7 +179,7 @@ client_api: enabled: false version: 1 chain_id: 31337 - networkUrl: "http://127.0.0.1:8545" + network_url: "http://127.0.0.1:8545" # 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 diff --git a/dendrite-sample.polylith.yaml b/dendrite-sample.polylith.yaml index 6475fb398..89e272518 100644 --- a/dendrite-sample.polylith.yaml +++ b/dendrite-sample.polylith.yaml @@ -175,7 +175,7 @@ client_api: enabled: false version: 1 chain_id: 31337 - networkUrl: "http://127.0.0.1:8545" + network_url: "http://127.0.0.1:8545" # 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 diff --git a/dendrite-zion.yaml b/dendrite-zion.yaml index eda51f4d1..da999baa5 100644 --- a/dendrite-zion.yaml +++ b/dendrite-zion.yaml @@ -186,9 +186,8 @@ client_api: ethereum: enabled: true version: 1 - chain_id: 5 - deployment_chain_id: ${CHAIN_ID} - networkUrl: ${BLOCKCHAIN_PROVIDER_URL} + chain_id: ${CHAIN_ID} + network_url: ${BLOCKCHAIN_PROVIDER_URL} enable_authz: ${ENABLE_AUTHZ} # Whether to require reCAPTCHA for registration. diff --git a/setup/config/config.go b/setup/config/config.go index d30dc4107..dedb5b031 100644 --- a/setup/config/config.go +++ b/setup/config/config.go @@ -23,7 +23,6 @@ import ( "os" "path/filepath" "regexp" - "strconv" "strings" "github.com/joho/godotenv" @@ -587,7 +586,9 @@ Replace selected config with environment variables */ func (config *Dendrite) replaceWithEnvVariables() { - // Replace selected fields with env variables + // If env variable is set, get the value from the env + // variable and replace it in each supported field. + err := godotenv.Load(".env") if err != nil { logrus.Errorln("error loading .env file", err) @@ -604,23 +605,21 @@ func (config *Dendrite) replaceWithEnvVariables() { ), ) - // If env variable is set, convert the deployment chain IDs from the env - // variable into []int and replace the ChainIDs field. if config.ClientAPI.PublicKeyAuthentication.Ethereum.Enabled { - strChainId := replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.DeploymentChainID) - if strChainId != "" { - id, err := strconv.Atoi(strings.TrimSpace(strChainId)) - if err == nil { - config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainID = id - } - } + config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigChainID = + replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigChainID) - config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl = replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl) + config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl = + replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl) + + config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigEnableAuthz = + replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigEnableAuthz) logrus.Infof( - "Supported Ethereum chain ID=%d, network URL=%s", - config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainID, + "Supported Ethereum chain_id=%v, network_url=%v, enable_authz=%v", + config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigChainID, config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl, + config.ClientAPI.PublicKeyAuthentication.Ethereum.ConfigEnableAuthz, ) } } diff --git a/setup/config/config_publickey.go b/setup/config/config_publickey.go index 647b648c1..6d73717da 100644 --- a/setup/config/config_publickey.go +++ b/setup/config/config_publickey.go @@ -1,6 +1,9 @@ package config import ( + "strconv" + "strings" + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" ) @@ -20,10 +23,37 @@ func (p EthereumAuthParams) GetParams() interface{} { type EthereumAuthConfig struct { Enabled bool `yaml:"enabled"` Version uint `yaml:"version"` - ChainID int `yaml:"chain_id"` - DeploymentChainID string `yaml:"deployment_chain_id"` // For deployment: use env variable string to override the chain ID. - NetworkUrl string `yaml:"networkUrl"` // Blockchain network provider URL - EnableAuthz bool `yaml:"enable_authz"` // Flag to enable / disable authorization during development + NetworkUrl string `yaml:"network_url"` // Blockchain network provider URL + ConfigChainID string `yaml:"chain_id"` // Blockchain chain ID. Env variable can replace this property. + ConfigEnableAuthz string `yaml:"enable_authz"` // Enable / disable authorization during development. Will be removed when feature is done. + chainID int + enableAuthz bool +} + +func (c *EthereumAuthConfig) GetChainID() int { + if c.ConfigChainID != "" { + v := strings.TrimSpace(c.ConfigChainID) + id, err := strconv.Atoi(v) + if err == nil { + c.chainID = id + } + // No need to do this again. + c.ConfigChainID = "" + } + return c.chainID +} + +func (c *EthereumAuthConfig) GetEnableAuthZ() bool { + if c.ConfigEnableAuthz != "" { + v := strings.TrimSpace(c.ConfigEnableAuthz) + boolValue, err := strconv.ParseBool(v) + if err == nil { + c.enableAuthz = boolValue + } + // No need to do this again. + c.ConfigEnableAuthz = "" + } + return c.enableAuthz } type PublicKeyAuthentication struct { @@ -48,7 +78,7 @@ func (pk *PublicKeyAuthentication) GetPublicKeyRegistrationParams() map[string]i if pk.Ethereum.Enabled { p := EthereumAuthParams{ Version: pk.Ethereum.Version, - ChainID: pk.Ethereum.ChainID, + ChainID: pk.Ethereum.GetChainID(), } params[authtypes.LoginTypePublicKeyEthereum] = p } diff --git a/test/publickey_utils.go b/test/publickey_utils.go index 497cb7527..0b045c954 100644 --- a/test/publickey_utils.go +++ b/test/publickey_utils.go @@ -27,7 +27,7 @@ import ( "github.com/spruceid/siwe-go" ) -const EthereumTestNetworkId = 1337 // Localhost chain ID +const EthereumTestNetworkId int = 31337 // Localhost chain ID const TestServerName = "localhost" type EthereumTestWallet struct { diff --git a/zion/zion_authorization.go b/zion/zion_authorization.go index d009fde94..fbb991a31 100644 --- a/zion/zion_authorization.go +++ b/zion/zion_authorization.go @@ -36,7 +36,7 @@ func NewZionAuthorization( var auth ZionAuthorization - auth.chainId = cfg.PublicKeyAuthentication.Ethereum.ChainID + auth.chainId = cfg.PublicKeyAuthentication.Ethereum.GetChainID() auth.store = NewStore(rsAPI) switch auth.chainId {