Skip to content

Commit

Permalink
can save a user registration in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Jan 8, 2024
1 parent f951143 commit 1b8b94d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
30 changes: 16 additions & 14 deletions server/registration/database/mongodb/userdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ type UserDbAccess struct {
}

// NewUserDbAccess creates a new user access to the DB
func NewUserDbAccess() database.Database {
func NewUserDbAccess() (database.Database, error) {
// 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)
credentials := options.Credential{
AuthSource: "admin",
AuthMechanism: "SCRAM-SHA-1",
Username: config.AppConfig.UserName,
Password: config.AppConfig.UserPassword,
}
userOpts := options.Client().ApplyURI(config.AppConfig.MongoDbUri).SetAuth(credentials)
client, err := mongo.Connect(context.TODO(), userOpts)
if err != nil {

return nil
return UserDbAccess{nil}, err
}

return UserDbAccess{
client: userDb,
}
return UserDbAccess{client}, nil
}

// Create creates a new document in the DB
Expand All @@ -47,8 +46,11 @@ func (u UserDbAccess) Create(data *registry.RegistrationData) (*registry.Registr
Registered: false,
}

result, err := u.client.Database("registration").Collection("documents").InsertOne(context.Background(),
doc)
db := u.client.Database("registry")
c := db.Collection("documents")
result, err := c.InsertOne(context.Background(), doc)

// result, err := u.client.Database("registry").Collection("documents").InsertOne(context.Background(), doc)

if err != nil {
return nil, err
Expand Down
63 changes: 63 additions & 0 deletions server/registration/docs/howto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Create users in DB:
test> use admin
switched to db admin
admin> db.getUsers()
{ users: [], ok: 1 }
admin> db.createUser({"user":"user", "pwd":"user", "roles": []})
{ ok: 1 }
admin> db.createUser({"user":"admin", "pwd":"admin", "roles": []})
{ ok: 1 }
admin> db.getUsers()
{
users: [
{
_id: 'admin.admin',
userId: UUID('d74ec7d0-6e9e-4869-bafb-3a865fa88b5d'),
user: 'admin',
db: 'admin',
roles: [],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
},
{
_id: 'admin.user',
userId: UUID('2510fbf3-71a4-4397-8f6d-3e80098c947f'),
user: 'user',
db: 'admin',
roles: [],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
],
ok: 1
}
admin> use registry
switched to db registry
registry> db.dropAllUsers()
{ n: 2, ok: 1 }
registry> db.getUsers()
{ users: [], ok: 1 }
registry> use admin
switched to db admin
admin> db.getUsers()
{
users: [
{
_id: 'admin.admin',
userId: UUID('d74ec7d0-6e9e-4869-bafb-3a865fa88b5d'),
user: 'admin',
db: 'admin',
roles: [],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
},
{
_id: 'admin.user',
userId: UUID('2510fbf3-71a4-4397-8f6d-3e80098c947f'),
user: 'user',
db: 'admin',
roles: [],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
],
ok: 1
}
admin> exit

7 changes: 5 additions & 2 deletions server/registration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.dedis.ch/hbt/server/registration/registry/user"
)

// curl -F "name='John Doe'" -F "passport=12XY456789" -F "role=0" -F "image=@./picture.png" -F "registered=false" localhost:80/document
// curl -F "name='John Doe'" -F "passport=12XY456789" -F "role=0" -F "image=@test/passport.jpg" -F "registered=false" localhost:3000/document

// POST /document HTTP/1.1
// Host: localhost:3000
Expand Down Expand Up @@ -86,7 +86,10 @@ func newApp() *application {
adminRouter.HandleFunc("/admin/document", admin.UpdateDocument).Methods("PUT")
adminRouter.HandleFunc("/admin/document", admin.DeleteDocument).Methods("DELETE")

userDb := mongodb.NewUserDbAccess()
userDb, err := mongodb.NewUserDbAccess()
if err != nil {
log.Fatal(err)
}
user.RegisterDb(userDb)

adminDb := mongodb.NewAdminDbAccess()
Expand Down
Binary file added server/registration/test/passport.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added server/registration/test/passport.jpg.zip
Binary file not shown.

0 comments on commit 1b8b94d

Please sign in to comment.