fixed bugs

This commit is contained in:
2025-03-19 16:36:21 +02:00
parent ba47810398
commit 1488d7db16
4 changed files with 19 additions and 29 deletions

View File

@@ -7,6 +7,7 @@ import (
"git.acooldomain.co/server-manager/backend/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type ServerPermissions struct {
@@ -65,9 +66,12 @@ func (self *ServersAuthorizationDbHandler) AddPermissions(ctx context.Context, u
"server_id": serverId,
},
bson.M{"$set": bson.M{
"username": username,
"server_id": serverId,
"permissions": newPermissions,
},
},
options.Update().SetUpsert(true),
)
return err

View File

@@ -2,24 +2,22 @@ package mongo
import (
"context"
"fmt"
"time"
"git.acooldomain.co/server-manager/backend/dbhandler"
"git.acooldomain.co/server-manager/backend/models"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/crypto/bcrypt"
)
type AuthUser struct {
Username string `json:"username"`
Nickname string `json:"nickname"`
HashedPassword string `json:"hashed_password"`
Permissions models.Permission `json:"permissions"`
MaxOwnedSevers uint `json:"max_owned_severs"`
Email string `json:"email"`
Username string `bson:"username"`
Nickname string `bson:"nickname"`
HashedPassword string `bson:"hashed_password"`
Permissions models.Permission `bson:"permissions"`
MaxOwnedSevers uint `bson:"max_owned_severs"`
Email string `bson:"email"`
}
type UserPassAuthenticationDbHandler struct {
@@ -60,16 +58,12 @@ func (self *UserPassAuthenticationDbHandler) AuthenticateUser(ctx context.Contex
return nil, err
}
hashedPassword, err := dbhandler.HashPassword(password)
err = bcrypt.CompareHashAndPassword([]byte(user.HashedPassword), []byte(password))
if err != nil {
return nil, err
}
if user.HashedPassword != hashedPassword {
return nil, fmt.Errorf("Incorrect Password")
}
return &models.User{
Username: user.Username,
Nickname: user.Nickname,
@@ -136,22 +130,13 @@ func (self *UserPassAuthenticationDbHandler) SetPermissions(
}
func NewUserPassAuthHandler(config models.MongoDBConfig) (*UserPassAuthenticationDbHandler, error) {
clientOptions := options.Client().ApplyURI(config.Url).SetAuth(options.Credential{
Username: config.Username,
Password: config.Password,
})
ctx, cancel := context.WithTimeoutCause(context.Background(), 30*time.Second, fmt.Errorf("Timeout"))
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
collection, err := getMongoCollection(config)
if err != nil {
return nil, err
}
return &UserPassAuthenticationDbHandler{
collection: client.Database(config.Database).Collection(config.Collection),
collection: collection,
}, nil
}