-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add registration server for user to register
- Loading branch information
Showing
14 changed files
with
607 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] | ||
} | ||
) | ||
``` |
Oops, something went wrong.