dendrite/setup/config/config_publickey.go
Tak Wai Wong 751030a658 Add a config flag on dendrite to switch between zion contracts v1 / v2 (#1151)
Temporary flag to implement v2 smart contract integration. Once v2 is
done, will remove this flag.
2022-12-28 18:56:40 -08:00

89 lines
2.3 KiB
Go

package config
import (
"strconv"
"strings"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
)
type AuthParams interface {
GetParams() interface{}
}
type EthereumAuthParams struct {
Version uint `json:"version"`
ChainID int `json:"chain_id"`
}
func (p EthereumAuthParams) GetParams() interface{} {
return p
}
type EthereumAuthConfig struct {
Enabled bool `yaml:"enabled"`
Version uint `yaml:"version"`
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. todo: remove this flag when feature is done.
ContractVersion string // todo: remove this setting when v2 migration is done.
chainID int
enableAuthz bool // todo: remove this flag when feature is done.
}
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 {
Ethereum EthereumAuthConfig `yaml:"ethereum"`
}
func (pk *PublicKeyAuthentication) Enabled() bool {
return pk.Ethereum.Enabled
}
func (pk *PublicKeyAuthentication) GetPublicKeyRegistrationFlows() []authtypes.Flow {
var flows []authtypes.Flow
if pk.Ethereum.Enabled {
flows = append(flows, authtypes.Flow{Stages: []authtypes.LoginType{authtypes.LoginTypePublicKeyEthereum}})
}
return flows
}
func (pk *PublicKeyAuthentication) GetPublicKeyRegistrationParams() map[string]interface{} {
params := make(map[string]interface{})
if pk.Ethereum.Enabled {
p := EthereumAuthParams{
Version: pk.Ethereum.Version,
ChainID: pk.Ethereum.GetChainID(),
}
params[authtypes.LoginTypePublicKeyEthereum] = p
}
return params
}