Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: limit token creation endpoint #106

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Census3APIConf struct {
DataDir string
GroupKey string
Web3Providers map[uint64]string
AdminToken string
}

type census3API struct {
Expand Down Expand Up @@ -48,6 +49,8 @@ func Init(db *db.DB, conf Census3APIConf) error {
if newAPI.endpoint, err = api.NewAPI(&r, "/api"); err != nil {
return err
}
// set the admin token
newAPI.endpoint.SetAdminToken(conf.AdminToken)
// init the census DB
if newAPI.censusDB, err = census.NewCensusDB(conf.DataDir, conf.GroupKey); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion api/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (capi *census3API) initTokenHandlers() error {
return err
}
if err := capi.endpoint.RegisterMethod("/tokens", "POST",
api.MethodAccessTypePublic, capi.createToken); err != nil {
api.MethodAccessTypeAdmin, capi.createToken); err != nil {
return err
}
if err := capi.endpoint.RegisterMethod("/tokens/{tokenID}", "GET",
Expand Down
18 changes: 18 additions & 0 deletions cmd/census3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"
"time"

"github.com/google/uuid"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/vocdoni/census3/api"
Expand All @@ -21,6 +22,7 @@ type Census3Config struct {
dataDir, logLevel, connectKey string
listOfWeb3Providers []string
port int
adminToken string
}

func main() {
Expand All @@ -37,6 +39,7 @@ func main() {
flag.StringVar(&config.logLevel, "logLevel", "info", "log level (debug, info, warn, error)")
flag.IntVar(&config.port, "port", 7788, "HTTP port for the API")
flag.StringVar(&config.connectKey, "connectKey", "", "connect group key for IPFS connect")
flag.StringVar(&config.adminToken, "adminToken", "", "the admin UUID token for the API")
var strWeb3Providers string
flag.StringVar(&strWeb3Providers, "web3Providers", "", "the list of URL's of available web3 providers")
flag.Parse()
Expand Down Expand Up @@ -68,6 +71,10 @@ func main() {
panic(err)
}
config.connectKey = pviper.GetString("connectKey")
if err := pviper.BindPFlag("adminToken", flag.Lookup("adminToken")); err != nil {
panic(err)
}
config.adminToken = pviper.GetString("adminToken")
if err := pviper.BindPFlag("web3Providers", flag.Lookup("web3Providers")); err != nil {
panic(err)
}
Expand All @@ -93,13 +100,24 @@ func main() {
if err != nil {
log.Fatal(err)
}
// if the admin token is not defined, generate a random one

if config.adminToken != "" {
if _, err := uuid.Parse(config.adminToken); err != nil {
log.Fatal("bad admin token format, it must be a valid UUID")
}
} else {
config.adminToken = uuid.New().String()
log.Infof("no admin token defined, using a random one: %s", config.adminToken)
}
// start the API
err = api.Init(database, api.Census3APIConf{
Hostname: "0.0.0.0",
Port: config.port,
DataDir: config.dataDir,
Web3Providers: w3p,
GroupKey: config.connectKey,
AdminToken: config.adminToken,
})
if err != nil {
log.Fatal(err)
Expand Down