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

Registration #62

Merged
merged 8 commits into from
Jan 8, 2024
Merged
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: 2 additions & 1 deletion .github/workflows/server_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ jobs:
test:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
# platform: [ubuntu-latest, macos-latest, windows-latest]
platform: [ubuntu-latest]
runs-on: ${{matrix.platform}}
defaults:
run:
Expand Down
42 changes: 34 additions & 8 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.17.0 // indirect
golang.org/x/net v0.16.0 // 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-20231009173412-8bfb1ae86b6c // indirect
google.golang.org/grpc v1.58.3 // 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
)
110 changes: 97 additions & 13 deletions server/go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions server/registration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registration
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
}
29 changes: 29 additions & 0 deletions server/registration/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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
// it takes the document as an argument
// and returns the document ID or an error
Create(*registry.RegistrationData) (*registry.RegistrationId, error)

// Read retrieves a document from the database
// it takes the document ID and the hash as arguments
// and returns the document or an error
Read(registry.RegistrationId, []byte) (*registry.RegistrationData, error)

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

// Delete deletes a document from the database
// it takes the document ID and the hash as arguments
// and returns nil or an error
Delete(registry.RegistrationId, []byte) error

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

import (
"context"
"errors"

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

type AdminDbAccess struct {
client *mongo.Client
}

// 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.RegistrationId, error) {
return nil, errors.New("admin cannot create user documents")
}

// Read reads a document from the DB
func (a AdminDbAccess) Read(docId registry.RegistrationId, hash []byte) (
*registry.RegistrationData,
error,
) {
return nil, nil
}

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

// Delete updates a document in the DB
func (a AdminDbAccess) Delete(docId registry.RegistrationId, hash []byte) 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
}
11 changes: 11 additions & 0 deletions server/registration/database/mongodb/dbtypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mongodb

// Document is a database struct for the registration service
type Document struct {
Name string `bson:"name"`
Passport string `bson:"passport"`
Role uint `bson:"role"`
Picture []byte `bson:"picture"`
Hash []byte `bson:"hash"`
Registered bool `bson:"registered"`
}
Loading
Loading