Skip to content

Commit

Permalink
add registration server for user to register
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Dec 21, 2023
1 parent e8c4a15 commit 14c3342
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 28 deletions.
2 changes: 1 addition & 1 deletion server/blockchain/calypso/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"golang.org/x/xerrors"
)

// miniController is a CLI initializer to register the value contract
// miniController is a CLI initializer to registration the value contract
//
// - implements node.Initializer
type miniController struct {
Expand Down
46 changes: 36 additions & 10 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,74 @@ module go.dedis.ch/hbt/server
go 1.21

require (
github.com/stretchr/testify v1.8.1
github.com/gorilla/mux v1.8.1
github.com/spf13/viper v1.18.1
github.com/steinfletcher/apitest v1.5.15
github.com/stretchr/testify v1.8.4
go.dedis.ch/dela v0.0.0-20230814162536-4bcfa7981c82
go.dedis.ch/kyber/v3 v3.1.0
go.mongodb.org/mongo-driver v1.13.1
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dedis/debugtools v0.0.0-20221206213939-0bc3bacd3042 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.dedis.ch/fixbuf v1.0.3 // indirect
go.dedis.ch/protobuf v1.0.11 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect
google.golang.org/grpc v1.58.3 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
118 changes: 101 additions & 17 deletions server/go.sum

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions server/registration/api.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@host = localhost:3000

// Create document
POST http://{{host}}/document HTTP/1.1
content-type: application/json

{
"name": "charles-henry",
"passport": "AB12CDEFGH",
"role": 1,
"picture": "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
"registered": false,
}

###

// Get Product By ID
GET http://{{host}}/register/document/1 HTTP/1.1
content-type: application/json

###

// Update Product
PUT http://{{host}}/register/document/1 HTTP/1.1
content-type: application/json

{
"name": "updated-product",
"description": "random-description-updated",
"price": 100.00
}

###

// Delete Product
DELETE http://{{host}}/register/document/1 HTTP/1.1
content-type: application/json
34 changes: 34 additions & 0 deletions server/registration/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package config

import (
"log"

"github.com/spf13/viper"
)

type Config struct {
MongoDbUri string `mapstructure:"mongodb_uri"`
UserName string `mapstructure:"user_name"`
UserPassword string `mapstructure:"user_password"`
AdminName string `mapstructure:"admin_name"`
AdminPassword string `mapstructure:"admin_name"`
UserServerPort string `mapstructure:"user_port"`
AdminServerPort string `mapstructure:"admin_port"`
}

var AppConfig Config

func LoadAppConfig() {
log.Println("Loading Server Configurations...")
viper.AddConfigPath("/home/jean/")
viper.SetConfigName("config")
viper.SetConfigType("json")
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
err = viper.Unmarshal(&AppConfig)
if err != nil {
log.Fatal(err)
}
}
9 changes: 9 additions & 0 deletions server/registration/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"mongodb_uri": "mongodb://localhost:27017",
"user_name": "user",
"user_password": "user",
"admin_name": "admin",
"admin_password": "admin",
"user_port": 3000,
"admin_port": 4000
}
25 changes: 25 additions & 0 deletions server/registration/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package database

import "go.dedis.ch/hbt/server/registration/registry"

// Database defines a generic CRUD interface to the database
type Database interface {
// Create creates a new document in the database
Create(registry.RegistrationData) (registry.DocId, error)

// Read retrieves a document from the database
// it takes the document ID as an argument
// and returns the document
Read(registry.DocId) (*registry.RegistrationData, error)

// Update updates a document in the database
// it takes the document ID and the updated document as an argument
Update(registry.DocId, *registry.RegistrationData) error

// DeleteDocument deletes a document from the database
// it takes the document ID as an argument
Delete(registry.DocId) error

// Disconnect disconnects from the database
Disconnect() error
}
130 changes: 130 additions & 0 deletions server/registration/database/mongodb/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package mongodb

import (
"context"
"errors"

"go.dedis.ch/hbt/server/registration/config"
"go.dedis.ch/hbt/server/registration/database"
"go.dedis.ch/hbt/server/registration/registry"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

type UserDbAccess struct {
client *mongo.Client
}

type AdminDbAccess struct {
client *mongo.Client
}

// NewUserDbAccess creates a new user access to the DB
func NewUserDbAccess() database.Database {
// Initialize userDb DB access
userCredentials := options.Credential{
Username: config.AppConfig.UserName,
Password: config.AppConfig.UserPassword,
}
userOpts := options.Client().ApplyURI(config.AppConfig.MongoDbUri).SetAuth(userCredentials)
userDb, err := mongo.Connect(context.TODO(), userOpts)
if err != nil {

return nil
}

return UserDbAccess{
client: userDb,
}
}

// Create creates a new document in the DB
func (u UserDbAccess) Create(doc registry.RegistrationData) (registry.DocId, error) {
result, err := u.client.Database("registration").Collection("documents").InsertOne(context.Background(),
doc)

if err != nil {
return nil, err
}

if oid, ok := result.InsertedID.(primitive.ObjectID); ok {
return oid.MarshalJSON()
}

return nil, errors.New("could not marshal object id")
}

// Read reads a document from the DB
func (u UserDbAccess) Read(docId registry.DocId) (*registry.RegistrationData, error) {
return nil, nil
}

// Update updates a document in the DB
func (u UserDbAccess) Update(docId registry.DocId, reg *registry.RegistrationData) error {
return nil
}

// Delete updates a document in the DB
func (u UserDbAccess) Delete(docId registry.DocId) error {
return nil
}

// Disconnect disconnects the user from the DB
func (u UserDbAccess) Disconnect() error {
err := u.client.Disconnect(context.Background())
if err != nil {
panic(err)
}

return nil
}

// NewAdminDbAccess creates a new admin access to the DB
func NewAdminDbAccess() *AdminDbAccess {
// Initialize adminDb DB access
adminCredentials := options.Credential{
Username: config.AppConfig.UserName,
Password: config.AppConfig.UserPassword,
}
adminOpts := options.Client().ApplyURI(config.AppConfig.MongoDbUri).SetAuth(adminCredentials)
adminDb, err := mongo.Connect(context.TODO(), adminOpts)
if err != nil {

return nil
}

return &AdminDbAccess{
client: adminDb,
}
}

// Create creates a new document in the DB
func (a AdminDbAccess) Create(doc registry.RegistrationData) (registry.DocId, error) {
return nil, errors.New("admin cannot create user documents")
}

// Read reads a document from the DB
func (a AdminDbAccess) Read(docId registry.DocId) (*registry.RegistrationData, error) {
return nil, nil
}

// Update updates a document in the DB
func (a AdminDbAccess) Update(docId registry.DocId, reg *registry.RegistrationData) error {
return nil
}

// Delete updates a document in the DB
func (a AdminDbAccess) Delete(docId registry.DocId) error {
return nil
}

// Disconnect disconnects the admin from the DB
func (a *AdminDbAccess) Disconnect() error {
err := a.client.Disconnect(context.Background())
if err != nil {
panic(err)
}

return nil
}
25 changes: 25 additions & 0 deletions server/registration/howto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Use the compass app from mongoDB to create a new database called registry, with a collection called documents.

Create a user with readWrite access to the registry database.
```mongosh
use registry
db.createUser(
{
user: "user",
pwd: "user",
roles: [ "readWrite" ]
}
)
```

Create a superuser with readWrite and dbAdmin access to the registry database.
```mongosh
use registry
db.createUser(
{
user: "superuser",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ "readWrite", "dbAdmin" ]
}
)
```
Loading

0 comments on commit 14c3342

Please sign in to comment.