Authorization - config, interface, and default implementation (#33)

* add config yaml for enable_auth

* zion_space_manager_localhost.go

* Placeholders for authorization

* rename func and type

* re-run go mod tidy

Co-authored-by: Tak Wai Wong <tak@hntlabs.com>
This commit is contained in:
Tak Wai Wong 2022-09-26 16:46:52 -07:00 committed by GitHub
parent acf0742b40
commit 65ee181de4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 144 additions and 3 deletions

View file

@ -0,0 +1,35 @@
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package authorization
import "github.com/matrix-org/dendrite/setup/config"
type AuthorizationArgs struct {
RoomId string
UserId string
Permission string
}
type Authorization interface {
IsAllowed(args AuthorizationArgs) (bool, error)
}
func NewClientApiAuthorization(cfg *config.ClientAPI) Authorization {
// Load authorization manager for Zion
//if cfg.PublicKeyAuthentication.Ethereum.EnableAuthz {
//}
return &DefaultAuthorization{}
}

View file

@ -0,0 +1,23 @@
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package authorization
type DefaultAuthorization struct {
}
func (azm *DefaultAuthorization) IsAllowed(args AuthorizationArgs) (bool, error) {
// Default. No authorization logic.
return true, nil
}

View file

@ -21,6 +21,7 @@ import (
"github.com/gorilla/mux"
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
"github.com/matrix-org/dendrite/authorization"
"github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/clientapi/auth"
clientutil "github.com/matrix-org/dendrite/clientapi/httputil"
@ -73,6 +74,8 @@ func Setup(
rateLimits := httputil.NewRateLimits(&cfg.RateLimiting)
userInteractiveAuth := auth.NewUserInteractive(userAPI, userAPI, cfg)
authorization := authorization.NewClientApiAuthorization(cfg)
_ = authorization // todo: use this in httputil.MakeAuthAPI
unstableFeatures := map[string]bool{
"org.matrix.e2e_cross_signing": true,

View file

@ -21,9 +21,10 @@ func (p EthereumAuthParams) GetParams() interface{} {
}
type EthereumAuthConfig struct {
Enabled bool `yaml:"enabled"`
Version uint `yaml:"version"`
ChainIDs []int `yaml:"chain_ids"`
Enabled bool `yaml:"enabled"`
Version uint `yaml:"version"`
ChainIDs []int `yaml:"chain_ids"`
EnableAuthz bool `yaml:"enable_authz"` // Flag to enable / disable authorization during development
}
type PublicKeyAuthentication struct {

65
web3/account.go Normal file
View file

@ -0,0 +1,65 @@
package web3
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
type CreateTransactionSignerArgs struct {
PrivateKey string
ChainId int64
Client *ethclient.Client
GasValue int64 // in wei
GasLimit int64 // in units
}
func CreateTransactionSigner(args CreateTransactionSignerArgs) (*bind.TransactOpts, error) {
privateKey, err := crypto.HexToECDSA(args.PrivateKey)
if err != nil {
return nil, err
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, errors.New("cannot create public key ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := args.Client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return nil, err
}
gasPrice, err := args.Client.SuggestGasPrice((context.Background()))
if err != nil {
return nil, err
}
signer, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(args.ChainId))
if err != nil {
return nil, err
}
signer.Nonce = big.NewInt(int64(nonce))
signer.Value = big.NewInt(args.GasValue)
signer.GasLimit = uint64(args.GasLimit)
signer.GasPrice = gasPrice
fmt.Printf("{ nonce: %d, value: %d, gasLimit: %d, gasPrice: %d }\n",
signer.Nonce,
signer.Value,
signer.GasLimit,
signer.GasPrice,
)
return signer, nil
}

14
web3/client.go Normal file
View file

@ -0,0 +1,14 @@
package web3
import (
"github.com/ethereum/go-ethereum/ethclient"
)
func GetEthClient(web3ProviderUrl string) (*ethclient.Client, error) {
client, err := ethclient.Dial(web3ProviderUrl)
if err != nil {
return nil, err
}
return client, nil
}