Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

fix: signup API is disabled when auth server is disabled, users and auth requests in mongo cannot be deleted #132

Merged
merged 3 commits into from
Mar 31, 2022
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
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo {
api.GET("/published/:name", PublishedMetadata())
api.GET("/published_data/:name", PublishedData())
api.GET("/layers/:param", ExportLayer(), AuthRequiredMiddleware())
api.POST("/signup", Signup())

if !cfg.Config.AuthSrv.Disabled {
api.POST("/signup", Signup())
api.POST("/signup/verify", StartSignupVerify())
api.POST("/signup/verify/:code", SignupVerify())
api.POST("/password-reset", PasswordReset())
Expand Down
13 changes: 5 additions & 8 deletions internal/infrastructure/mongo/auth_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,15 @@ func (r *authRequestRepo) init() {
}

func (r *authRequestRepo) FindByID(ctx context.Context, id2 id.AuthRequestID) (*auth.Request, error) {
filter := bson.D{{Key: "id", Value: id2.String()}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{"id": id2.String()})
}

func (r *authRequestRepo) FindByCode(ctx context.Context, s string) (*auth.Request, error) {
filter := bson.D{{Key: "code", Value: s}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{"code": s})
}

func (r *authRequestRepo) FindBySubject(ctx context.Context, s string) (*auth.Request, error) {
filter := bson.D{{Key: "subject", Value: s}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{"subject": s})
}

func (r *authRequestRepo) Save(ctx context.Context, request *auth.Request) error {
Expand All @@ -49,10 +46,10 @@ func (r *authRequestRepo) Save(ctx context.Context, request *auth.Request) error
}

func (r *authRequestRepo) Remove(ctx context.Context, requestID id.AuthRequestID) error {
return r.client.RemoveOne(ctx, requestID.String())
return r.client.RemoveOne(ctx, bson.M{"id": requestID.String()})
}

func (r *authRequestRepo) findOne(ctx context.Context, filter bson.D) (*auth.Request, error) {
func (r *authRequestRepo) findOne(ctx context.Context, filter interface{}) (*auth.Request, error) {
dst := make([]*auth.Request, 0, 1)
c := mongodoc.AuthRequestConsumer{
Rows: dst,
Expand Down
7 changes: 3 additions & 4 deletions internal/infrastructure/mongo/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ func (r *propertyRepo) RemoveByScene(ctx context.Context, sceneID id.SceneID) er
if !r.f.CanWrite(sceneID) {
return nil
}
filter := bson.D{
{Key: "scene", Value: sceneID.String()},
}
_, err := r.client.Collection().DeleteMany(ctx, filter)
_, err := r.client.Collection().DeleteMany(ctx, bson.M{
"scene": sceneID.String(),
})
if err != nil {
return rerror.ErrInternalBy(err)
}
Expand Down
19 changes: 4 additions & 15 deletions internal/infrastructure/mongo/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,17 @@ func (r *sceneRepo) FindByID(ctx context.Context, id id.SceneID) (*scene.Scene,
}

func (r *sceneRepo) FindByIDs(ctx context.Context, ids []id.SceneID) (scene.List, error) {
filter := bson.M{
return r.find(ctx, make(scene.List, 0, len(ids)), bson.M{
"id": bson.M{
"$in": id.SceneIDsToStrings(ids),
},
}
dst := make(scene.List, 0, len(ids))
res, err := r.find(ctx, dst, filter)
if err != nil {
return nil, err
}
return filterScenes(ids, res), nil
})
}

func (r *sceneRepo) FindByProject(ctx context.Context, id id.ProjectID) (*scene.Scene, error) {
filter := bson.M{
return r.findOne(ctx, bson.M{
"project": id.String(),
}
return r.findOne(ctx, filter)
})
}

func (r *sceneRepo) FindByTeam(ctx context.Context, teams ...id.TeamID) (scene.List, error) {
Expand Down Expand Up @@ -111,10 +104,6 @@ func (r *sceneRepo) findOne(ctx context.Context, filter interface{}) (*scene.Sce
return c.Rows[0], nil
}

func filterScenes(ids []id.SceneID, rows scene.List) scene.List {
return rows.FilterByID(ids...)
}

func (r *sceneRepo) readFilter(filter interface{}) interface{} {
return applyTeamFilter(filter, r.f.Readable)
}
Expand Down
7 changes: 3 additions & 4 deletions internal/infrastructure/mongo/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,9 @@ func (r *tagRepo) RemoveAll(ctx context.Context, ids []id.TagID) error {
}

func (r *tagRepo) RemoveByScene(ctx context.Context, sceneID id.SceneID) error {
filter := bson.D{
{Key: "scene", Value: sceneID.String()},
}
_, err := r.client.Collection().DeleteMany(ctx, filter)
_, err := r.client.Collection().DeleteMany(ctx, bson.M{
"scene": sceneID.String(),
})
if err != nil {
return rerror.ErrInternalBy(err)
}
Expand Down
29 changes: 11 additions & 18 deletions internal/infrastructure/mongo/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,26 @@ func (r *teamRepo) init() {
}

func (r *teamRepo) FindByUser(ctx context.Context, id id.UserID) (user.TeamList, error) {
filter := bson.D{
{Key: "members." + strings.Replace(id.String(), ".", "", -1), Value: bson.D{
{Key: "$exists", Value: true},
}},
}
return r.find(ctx, nil, filter)
return r.find(ctx, nil, bson.M{
"members." + strings.Replace(id.String(), ".", "", -1): bson.M{
"$exists": true,
},
})
}

func (r *teamRepo) FindByIDs(ctx context.Context, ids []id.TeamID) (user.TeamList, error) {
filter := bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.TeamIDsToStrings(ids)},
}},
}
dst := make([]*user.Team, 0, len(ids))
res, err := r.find(ctx, dst, filter)
res, err := r.find(ctx, dst, bson.M{
"id": bson.M{"$in": id.TeamIDsToStrings(ids)},
})
if err != nil {
return nil, err
}
return filterTeams(ids, res), nil
}

func (r *teamRepo) FindByID(ctx context.Context, id id.TeamID) (*user.Team, error) {
filter := bson.D{
{Key: "id", Value: id.String()},
}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{"id": id.String()})
}

func (r *teamRepo) Save(ctx context.Context, team *user.Team) error {
Expand Down Expand Up @@ -89,7 +82,7 @@ func (r *teamRepo) RemoveAll(ctx context.Context, ids []id.TeamID) error {
})
}

func (r *teamRepo) find(ctx context.Context, dst []*user.Team, filter bson.D) (user.TeamList, error) {
func (r *teamRepo) find(ctx context.Context, dst []*user.Team, filter interface{}) (user.TeamList, error) {
c := mongodoc.TeamConsumer{
Rows: dst,
}
Expand All @@ -99,7 +92,7 @@ func (r *teamRepo) find(ctx context.Context, dst []*user.Team, filter bson.D) (u
return c.Rows, nil
}

func (r *teamRepo) findOne(ctx context.Context, filter bson.D) (*user.Team, error) {
func (r *teamRepo) findOne(ctx context.Context, filter interface{}) (*user.Team, error) {
dst := make([]*user.Team, 0, 1)
c := mongodoc.TeamConsumer{
Rows: dst,
Expand Down
71 changes: 34 additions & 37 deletions internal/infrastructure/mongo/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,64 +30,62 @@ func (r *userRepo) init() {
}

func (r *userRepo) FindByIDs(ctx context.Context, ids []id.UserID) ([]*user.User, error) {
filter := bson.D{{Key: "id", Value: bson.D{
{Key: "$in", Value: id.UserIDsToStrings(ids)},
}}}
dst := make([]*user.User, 0, len(ids))
res, err := r.find(ctx, dst, filter)
res, err := r.find(ctx, dst, bson.M{
"id": bson.M{"$in": id.UserIDsToStrings(ids)},
})
if err != nil {
return nil, err
}
return filterUsers(ids, res), nil
}

func (r *userRepo) FindByID(ctx context.Context, id2 id.UserID) (*user.User, error) {
filter := bson.D{{Key: "id", Value: id.ID(id2).String()}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{"id": id2.String()})
}

func (r *userRepo) FindByAuth0Sub(ctx context.Context, auth0sub string) (*user.User, error) {
filter := bson.D{
{Key: "$or", Value: []bson.D{
{{Key: "auth0sub", Value: auth0sub}},
{{Key: "auth0sublist", Value: bson.D{
{Key: "$elemMatch", Value: bson.D{
{Key: "$eq", Value: auth0sub},
}},
}}},
}},
}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{
"$or": []bson.M{
{"auth0sub": auth0sub},
{
"auth0sublist": bson.M{
"$elemMatch": bson.M{
"$eq": auth0sub,
},
},
},
},
})
}

func (r *userRepo) FindByEmail(ctx context.Context, email string) (*user.User, error) {
filter := bson.D{{Key: "email", Value: email}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{"email": email})
}

func (r *userRepo) FindByName(ctx context.Context, name string) (*user.User, error) {
filter := bson.D{{Key: "name", Value: name}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{"name": name})
}

func (r *userRepo) FindByNameOrEmail(ctx context.Context, nameOrEmail string) (*user.User, error) {
filter := bson.D{{Key: "$or", Value: []bson.D{
{{Key: "email", Value: nameOrEmail}},
{{Key: "name", Value: nameOrEmail}},
}}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{
"$or": []bson.M{
{"email": nameOrEmail},
{"name": nameOrEmail},
},
})
}

func (r *userRepo) FindByVerification(ctx context.Context, code string) (*user.User, error) {
filter := bson.D{{Key: "verification.code", Value: code}}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{
"verification.code": code,
})
}

func (r *userRepo) FindByPasswordResetRequest(ctx context.Context, pwdResetToken string) (*user.User, error) {
filter := bson.D{
{Key: "passwordreset.token", Value: pwdResetToken},
}
return r.findOne(ctx, filter)
return r.findOne(ctx, bson.M{
"passwordreset.token": pwdResetToken,
})
}

func (r *userRepo) Save(ctx context.Context, user *user.User) error {
Expand All @@ -96,10 +94,10 @@ func (r *userRepo) Save(ctx context.Context, user *user.User) error {
}

func (r *userRepo) Remove(ctx context.Context, user id.UserID) error {
return r.client.RemoveOne(ctx, user.String())
return r.client.RemoveOne(ctx, bson.M{"id": user.String()})
}

func (r *userRepo) find(ctx context.Context, dst []*user.User, filter bson.D) ([]*user.User, error) {
func (r *userRepo) find(ctx context.Context, dst []*user.User, filter interface{}) ([]*user.User, error) {
c := mongodoc.UserConsumer{
Rows: dst,
}
Expand All @@ -109,10 +107,9 @@ func (r *userRepo) find(ctx context.Context, dst []*user.User, filter bson.D) ([
return c.Rows, nil
}

func (r *userRepo) findOne(ctx context.Context, filter bson.D) (*user.User, error) {
dst := make([]*user.User, 0, 1)
func (r *userRepo) findOne(ctx context.Context, filter interface{}) (*user.User, error) {
c := mongodoc.UserConsumer{
Rows: dst,
Rows: make([]*user.User, 0, 1),
}
if err := r.client.FindOne(ctx, filter, &c); err != nil {
return nil, err
Expand Down