mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 18:43:10 -06:00
Dendrite should only support 1 chain id (#47)
* Support 1 blockchain only * auth check based on single chain * use config to set the blockchain provider URL * fix config to read from env variable * handle 31337 in addition to 1337
This commit is contained in:
parent
36039c2efc
commit
db9758464d
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
)
|
)
|
||||||
|
|
@ -35,11 +35,9 @@ type loginContext struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createLoginContext(_ *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,
|
||||||
|
|
@ -47,7 +45,7 @@ func createLoginContext(_ *testing.T) *loginContext {
|
||||||
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,
|
||||||
|
|
|
||||||
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
func NewAuthorization(cfg *config.ClientAPI, rsAPI roomserver.ClientRoomserverAPI) authorization.Authorization {
|
func NewAuthorization(cfg *config.ClientAPI, rsAPI roomserver.ClientRoomserverAPI) authorization.Authorization {
|
||||||
// Load authorization manager for Zion
|
// Load authorization manager for Zion
|
||||||
if cfg.PublicKeyAuthentication.Ethereum.EnableAuthz {
|
if cfg.PublicKeyAuthentication.Ethereum.EnableAuthz {
|
||||||
auth, err := zion.NewZionAuthorization(rsAPI)
|
auth, err := zion.NewZionAuthorization(cfg, rsAPI)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln("Failed to initialise Zion authorization manager. Using default.", err)
|
log.Errorln("Failed to initialise Zion authorization manager. Using default.", err)
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -41,11 +40,9 @@ type registerContext struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRegisterContext(_ *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,
|
||||||
|
|
@ -53,7 +50,7 @@ func createRegisterContext(_ *testing.T) *registerContext {
|
||||||
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,
|
||||||
_ *config.ClientAPI,
|
|
||||||
_ *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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,8 @@ client_api:
|
||||||
ethereum:
|
ethereum:
|
||||||
enabled: false
|
enabled: false
|
||||||
version: 1
|
version: 1
|
||||||
chain_ids: []
|
chain_id: 31337
|
||||||
|
networkUrl: "http://127.0.0.1:8545"
|
||||||
|
|
||||||
# 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
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,8 @@ client_api:
|
||||||
ethereum:
|
ethereum:
|
||||||
enabled: false
|
enabled: false
|
||||||
version: 1
|
version: 1
|
||||||
chain_ids: []
|
chain_id: 31337
|
||||||
|
networkUrl: "http://127.0.0.1:8545"
|
||||||
|
|
||||||
# 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
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
"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/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
|
@ -587,11 +588,15 @@ Replace selected config with environment variables
|
||||||
|
|
||||||
func (config *Dendrite) replaceWithEnvVariables() {
|
func (config *Dendrite) replaceWithEnvVariables() {
|
||||||
// Replace selected fields with env variables
|
// Replace selected fields with env variables
|
||||||
|
err := godotenv.Load(".env")
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorln("error loading .env file", err)
|
||||||
|
}
|
||||||
|
|
||||||
config.Global.ServerName = gomatrixserverlib.ServerName(
|
config.Global.ServerName = gomatrixserverlib.ServerName(
|
||||||
replaceWithEnvVariables(string(config.Global.ServerName)),
|
replaceWithEnvVariables(string(config.Global.ServerName)),
|
||||||
)
|
)
|
||||||
logrus.Infof("Matrix ServerName=%s\n", config.Global.ServerName)
|
logrus.Infof("Matrix ServerName=%s", config.Global.ServerName)
|
||||||
|
|
||||||
config.Global.DatabaseOptions.ConnectionString = DataSource(
|
config.Global.DatabaseOptions.ConnectionString = DataSource(
|
||||||
replaceWithEnvVariables(
|
replaceWithEnvVariables(
|
||||||
|
|
@ -602,19 +607,21 @@ func (config *Dendrite) replaceWithEnvVariables() {
|
||||||
// If env variable is set, convert the deployment chain IDs from the env
|
// If env variable is set, convert the deployment chain IDs from the env
|
||||||
// variable into []int and replace the ChainIDs field.
|
// variable into []int and replace the ChainIDs field.
|
||||||
if config.ClientAPI.PublicKeyAuthentication.Ethereum.Enabled {
|
if config.ClientAPI.PublicKeyAuthentication.Ethereum.Enabled {
|
||||||
deploymentChainIDs := replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.DeploymentChainIDs)
|
strChainId := replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.DeploymentChainID)
|
||||||
chainIds := strings.Split(deploymentChainIDs, ",")
|
if strChainId != "" {
|
||||||
if len(chainIds) > 0 && chainIds[0] != "" {
|
id, err := strconv.Atoi(strings.TrimSpace(strChainId))
|
||||||
var ids []int
|
|
||||||
for _, id := range chainIds {
|
|
||||||
id, err := strconv.Atoi(strings.TrimSpace(id))
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ids = append(ids, id)
|
config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainID = id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainIDs = ids
|
|
||||||
}
|
config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl = replaceWithEnvVariables(config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl)
|
||||||
logrus.Infof("Supported Ethereum chain IDs=%d\n", config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainIDs)
|
|
||||||
|
logrus.Infof(
|
||||||
|
"Supported Ethereum chain ID=%d, network URL=%s",
|
||||||
|
config.ClientAPI.PublicKeyAuthentication.Ethereum.ChainID,
|
||||||
|
config.ClientAPI.PublicKeyAuthentication.Ethereum.NetworkUrl,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,21 +10,19 @@ 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"`
|
||||||
DeploymentChainIDs string `yaml:"deployment_chain_ids"` // For deployment: use env variable strings to override the chain IDs.
|
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
|
EnableAuthz bool `yaml:"enable_authz"` // Flag to enable / disable authorization during development
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,7 +48,7 @@ func (pk *PublicKeyAuthentication) GetPublicKeyRegistrationParams() map[string]i
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,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 {
|
||||||
|
|
@ -68,7 +68,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,
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,16 @@ package zion
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"github.com/matrix-org/dendrite/authorization"
|
"github.com/matrix-org/dendrite/authorization"
|
||||||
roomserver "github.com/matrix-org/dendrite/roomserver/api"
|
roomserver "github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
zion_goerli "github.com/matrix-org/dendrite/zion/contracts/goerli/zion_goerli"
|
zion_goerli "github.com/matrix-org/dendrite/zion/contracts/goerli/zion_goerli"
|
||||||
zion_localhost "github.com/matrix-org/dendrite/zion/contracts/localhost/zion_localhost"
|
zion_localhost "github.com/matrix-org/dendrite/zion/contracts/localhost/zion_localhost"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
localhostEndpointUrl = "LOCALHOST_ENDPOINT" // .env
|
|
||||||
goerliEndpointUrl = "GOERLI_ENDPOINT" // .env
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:embed contracts/localhost/addresses/space-manager.json
|
//go:embed contracts/localhost/addresses/space-manager.json
|
||||||
var localhostJson []byte
|
var localhostJson []byte
|
||||||
|
|
||||||
|
|
@ -28,30 +22,42 @@ type ZionAuthorization struct {
|
||||||
store Store
|
store Store
|
||||||
spaceManagerLocalhost *zion_localhost.ZionSpaceManagerLocalhost
|
spaceManagerLocalhost *zion_localhost.ZionSpaceManagerLocalhost
|
||||||
spaceManagerGoerli *zion_goerli.ZionSpaceManagerGoerli
|
spaceManagerGoerli *zion_goerli.ZionSpaceManagerGoerli
|
||||||
|
chainId int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewZionAuthorization(rsAPI roomserver.ClientRoomserverAPI) (authorization.Authorization, error) {
|
func NewZionAuthorization(
|
||||||
err := godotenv.Load(".env")
|
cfg *config.ClientAPI,
|
||||||
if err != nil {
|
rsAPI roomserver.ClientRoomserverAPI,
|
||||||
log.Errorln("error loading .env file", err)
|
) (authorization.Authorization, error) {
|
||||||
|
if cfg.PublicKeyAuthentication.Ethereum.NetworkUrl == "" {
|
||||||
|
log.Errorf("No blockchain network url specified in config\n")
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var auth ZionAuthorization
|
var auth ZionAuthorization
|
||||||
|
|
||||||
|
auth.chainId = cfg.PublicKeyAuthentication.Ethereum.ChainID
|
||||||
auth.store = NewStore(rsAPI)
|
auth.store = NewStore(rsAPI)
|
||||||
|
|
||||||
localhost, err := newZionSpaceManagerLocalhost(os.Getenv(localhostEndpointUrl))
|
switch auth.chainId {
|
||||||
|
case 1337, 31337:
|
||||||
|
localhost, err := newZionSpaceManagerLocalhost(cfg.PublicKeyAuthentication.Ethereum.NetworkUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln("error instantiating ZionSpaceManagerLocalhost", err)
|
log.Errorln("error instantiating ZionSpaceManagerLocalhost", err)
|
||||||
}
|
}
|
||||||
auth.spaceManagerLocalhost = localhost
|
auth.spaceManagerLocalhost = localhost
|
||||||
|
|
||||||
goerli, err := newZionSpaceManagerGoerli(os.Getenv(goerliEndpointUrl))
|
case 5:
|
||||||
|
goerli, err := newZionSpaceManagerGoerli(cfg.PublicKeyAuthentication.Ethereum.NetworkUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln("error instantiating ZionSpaceManagerGoerli", err)
|
log.Errorln("error instantiating ZionSpaceManagerGoerli", err)
|
||||||
}
|
}
|
||||||
auth.spaceManagerGoerli = goerli
|
auth.spaceManagerGoerli = goerli
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.Errorf("Unsupported chain id: %d\n", auth.chainId)
|
||||||
|
}
|
||||||
|
|
||||||
return &auth, nil
|
return &auth, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,13 +72,13 @@ func (za *ZionAuthorization) IsAllowed(args authorization.AuthorizationArgs) (bo
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch userIdentifier.ChainId {
|
switch za.chainId {
|
||||||
case 1337, 31337:
|
case 1337, 31337:
|
||||||
return za.isAllowedLocalhost(roomInfo, userIdentifier.AccountAddress, args.Permission)
|
return za.isAllowedLocalhost(roomInfo, userIdentifier.AccountAddress, args.Permission)
|
||||||
case 5:
|
case 5:
|
||||||
return za.isAllowedGoerli(roomInfo, userIdentifier.AccountAddress, args.Permission)
|
return za.isAllowedGoerli(roomInfo, userIdentifier.AccountAddress, args.Permission)
|
||||||
default:
|
default:
|
||||||
log.Errorf("Unsupported chain id: %d\n", userIdentifier.ChainId)
|
log.Errorf("Unsupported chain id: %d", userIdentifier.ChainId)
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return false, nil
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue