create account, create email

This commit is contained in:
2024-09-03 15:56:34 +02:00
parent fd8fdc899c
commit 80289f1929
20 changed files with 409 additions and 158 deletions

View File

@ -1,8 +1,60 @@
package service
type CreateAccountInput struct{}
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"`
}
// CreateAccount creates an account, returns nil if successful or error if not
func (s *Service) CreateAccount(in *CreateAccountInput) error {
// 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 {
panic("implement me")
}

View File

@ -1 +1,25 @@
package service
import (
"code.icod.de/auth/accountserver/ent"
"context"
"errors"
)
type CreateEmailInput struct {
Account *ent.Account
Primary bool
Email string
}
func (s *Service) CreateEmail(in *CreateEmailInput) error {
create := s.client.Email.Create()
create.SetEmail(in.Email)
create.SetPrimary(in.Primary)
create.SetAccount(in.Account)
_, e := create.Save(context.Background())
if e != nil {
return errors.New("create email: failed to save to database: " + e.Error())
}
return nil
}