accountserver/service/account.go

61 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-08-19 13:54:46 +02:00
package service
2024-08-19 14:15:44 +02:00
2024-09-03 15:56:34 +02:00
import (
"code.icod.de/auth/accountserver/util"
"context"
"errors"
"github.com/matthewhartstonge/argon2"
)
type CreateAccountInput struct {
Name string `json:"name,omitempty"`
Nickname string `json:"nickname,omitempty"`
Email string `json:"email"`
Secret string `json:"secret"`
}
2024-08-19 14:15:44 +02:00
// CreateAccount creates an account, returns nil if successful or error if not
func (s *Service) CreateAccount(in *CreateAccountInput) error {
2024-09-03 15:56:34 +02:00
// hash the secret with argon2
argon := argon2.DefaultConfig()
encoded, e := argon.HashEncoded([]byte(in.Secret))
if e != nil {
return errors.New("create account: could not encode secret: " + e.Error())
}
// generate aes key
k := util.GenerateRandomKey(32)
create := s.client.Account.Create()
create = create.SetSecret(encoded)
create = create.SetAes(k)
if in.Name != "" {
create = create.SetName(in.Name)
}
if in.Nickname != "" {
create = create.SetNickname(in.Nickname)
}
a, e := create.Save(context.Background())
if e != nil {
return errors.New("create account: could not save account to database" + e.Error())
}
cei := new(CreateEmailInput)
cei.Account = a
cei.Email = in.Email
cei.Primary = true
return s.CreateEmail(cei)
}
type ReplaceAccountInput struct{}
// ReplaceAccount updates, replaces an account.
func (s *Service) ReplaceAccount(input *ReplaceAccountInput) error {
panic("implement me")
}
type DeleteAccountInput struct{}
func (s *Service) DeleteAccount(input *DeleteAccountInput) error {
2024-08-19 14:15:44 +02:00
panic("implement me")
}