Files
archie/internal/domain/identity/accounts.go
T

60 lines
2.0 KiB
Go

package identity
import (
"context"
"errors"
"net/mail"
"strings"
"time"
)
var ErrSetupComplete = errors.New("administrator has already been set up")
const AccountSessionLifetime = 7 * 24 * time.Hour
type AccountRegistration struct {
PublicURL string `json:"public_url"`
WorkspaceName string `json:"workspace_name"`
OrganisationName string `json:"organisation_name"`
Email string `json:"email"`
DisplayName string `json:"display_name"`
Password string `json:"password"`
Role string `json:"role"`
}
func (r *AccountRegistration) Validate() error {
r.Email = strings.ToLower(strings.TrimSpace(r.Email))
address, err := mail.ParseAddress(r.Email)
r.DisplayName = strings.TrimSpace(r.DisplayName)
if err != nil || address.Address != r.Email || len(r.Email) > 254 || r.DisplayName == "" || len(r.DisplayName) > 200 {
return ErrInvalid
}
return nil
}
type AccountSession struct {
Identity Identity
Token string
Expires time.Time
}
// Accounts is served by the State Store. Account creation after bootstrap is
// administrative; password changes always address the caller's own identity.
type Accounts interface {
AccountSetupRequired(context.Context) (bool, error)
RegisterAccount(context.Context, AccountRegistration, bool) (Identity, error)
SignInAccount(context.Context, string, string) (AccountSession, error)
ResolveAccountSession(context.Context, string) (Identity, error)
SignOutAccount(context.Context, string) error
ChangeAccountPassword(context.Context, string, string) error
}
// AccountSecurity guards authenticator changes with recent authentication and
// provides a one-use recovery secret without relying on outbound email.
type AccountSecurity interface {
OpenAccountSession(context.Context, time.Time) (AccountSession, error)
RequireFreshAccountSession(context.Context, string) error
CreateAccountRecovery(context.Context) (string, error)
RecoverAccount(context.Context, string, string, string) error
}