mobility-accounts/handlers/accounts.go

135 lines
3.3 KiB
Go

package handlers
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/mobility-accounts/storage"
"github.com/google/uuid"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/spf13/viper"
"golang.org/x/crypto/bcrypt"
)
type MobilityAccountsHandler struct {
storage storage.Storage
config *viper.Viper
}
func NewHandler(cfg *viper.Viper, storage storage.Storage) MobilityAccountsHandler {
return MobilityAccountsHandler{
storage: storage,
config: cfg,
}
}
func (h MobilityAccountsHandler) Login(username string, password string, namespace string) (*storage.Account, error) {
if password == "" {
return nil, errors.New("empty password not allowed")
}
account, err := h.storage.DB.LocalAuthentication(namespace, username, "", "")
if err != nil {
return nil, err
}
if err = bcrypt.CompareHashAndPassword([]byte(account.Authentication.Local.Password), []byte(password)); err != nil {
return nil, err
}
return account, nil
}
func (h MobilityAccountsHandler) Register(account storage.Account) (*storage.Account, error) {
if account.ID != "" {
return nil, errors.New("id should be empty")
}
// Generate new UUID
account.ID = uuid.NewString()
// If a password was sent, hash the password
if account.Authentication.Local.Password != "" {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(account.Authentication.Local.Password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
account.Authentication.Local.Password = string(hashedPassword)
}
// Validate data schemas
dataschemas := h.config.GetStringMap("data_schemas")
for k, v := range account.Data {
if !h.config.GetBool("allow_any_data") && dataschemas[k] == nil {
return nil, errors.New("data scope not allowed")
}
if dataschemas[k] != nil {
s := dataschemas[k].(map[string]string)
sch, err := jsonschema.Compile(s["schema"])
if err != nil {
return nil, err
}
if err = sch.Validate(v); err != nil {
return nil, err
}
}
}
// Set the "created" metadata
if account.Metadata == nil {
account.Metadata = map[string]any{}
}
account.Metadata["created"] = time.Now()
// Store the account
if err := h.storage.DB.CreateAccount(account); err != nil {
return nil, err
}
return &account, nil
}
func (h MobilityAccountsHandler) UpdateData(accountid string, datas map[string]any) (*storage.Account, error) {
account, err := h.storage.DB.GetAccount(accountid)
if err != nil {
return nil, err
}
// Validate data schemas and update account
dataschemas := h.config.GetStringMap("data_schemas")
for k, v := range datas {
if !h.config.GetBool("allow_any_data") && dataschemas[k] == nil {
return nil, errors.New("data scope not allowed")
}
if dataschemas[k] != nil {
s := dataschemas[k].(map[string]string)
sch, err := jsonschema.Compile(s["schema"])
if err != nil {
return nil, err
}
if err = sch.Validate(v); err != nil {
return nil, err
}
account.Data[k] = v
}
}
if err = h.storage.DB.UpdateAccount(*account); err != nil {
return nil, err
}
return account, nil
}
func (h MobilityAccountsHandler) GetAccount(id string) (account *storage.Account, err error) {
account, err = h.storage.DB.GetAccount(id)
return
}
func (h MobilityAccountsHandler) GetAccounts(namespaces []string) (accounts []storage.Account, err error) {
accounts, err = h.storage.DB.GetAccounts(namespaces)
return
}