create account + create session
This commit is contained in:
+258
-14
@@ -1,26 +1,270 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
const (
|
||||
maxFailedAttempts = 5
|
||||
lockDuration = 15 * time.Minute
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
ID ident.UUID
|
||||
Login string
|
||||
Name string
|
||||
Status string
|
||||
AccountState string
|
||||
MustChangePassword bool
|
||||
CreatedAt int64
|
||||
Groups []Group
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
ID ident.UUID
|
||||
Name string
|
||||
Role string
|
||||
}
|
||||
|
||||
type CreateUserParams struct {
|
||||
Login string
|
||||
Password string
|
||||
Email *string
|
||||
Role string
|
||||
}
|
||||
|
||||
type NewUserParams struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func NewUser(params NewUserParams) User {
|
||||
|
||||
return User{
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
func (s *Service) CreateUser(ctx context.Context, params CreateUserParams) (*User, error) {
|
||||
display, key, err := normalizeLogin(params.Login)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validatePassword(params.Password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
role := params.Role
|
||||
if role == "" {
|
||||
role = RoleMember
|
||||
}
|
||||
|
||||
groupName, err := groupNameForRole(role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
group, err := s.groupByName(ctx, groupName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
passwordHash, err := HashPassword(params.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userID, err := ident.NewV7()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
err = s.withTx(ctx, func(q *database.Queries) error {
|
||||
if _, err := q.CreatePrincipal(ctx, database.CreatePrincipalParams{
|
||||
ID: userID,
|
||||
Kind: "user",
|
||||
Name: display,
|
||||
Status: "active",
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := q.CreateAccount(ctx, database.CreateAccountParams{
|
||||
PrincipalID: userID,
|
||||
Login: display,
|
||||
LoginKey: key,
|
||||
PasswordHash: sql.NullString{
|
||||
String: passwordHash,
|
||||
Valid: true,
|
||||
},
|
||||
PasswordChangedAt: sql.NullInt64{
|
||||
Int64: now,
|
||||
Valid: true,
|
||||
},
|
||||
MustChangePassword: 0,
|
||||
State: "active",
|
||||
FailedAttempts: 0,
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := q.CreateMembership(ctx, database.CreateMembershipParams{
|
||||
GroupID: group.ID,
|
||||
MemberID: userID,
|
||||
Role: "member",
|
||||
CreatedAt: now,
|
||||
})
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrLoginTaken, display)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.loadUser(ctx, userID)
|
||||
}
|
||||
|
||||
func (u *User) Is(role string) bool {
|
||||
return role == u.Role
|
||||
func (s *Service) Authenticate(ctx context.Context, login, password string) (*User, error) {
|
||||
_, key, err := normalizeLogin(login)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
account, err := s.db.Queries.GetAccountByLoginKey(ctx, key)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrInvalidCredentials
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
principal, err := s.db.Queries.GetPrincipal(ctx, account.PrincipalID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrInvalidCredentials
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if principal.Status != "active" {
|
||||
return nil, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
switch account.State {
|
||||
case "disabled":
|
||||
return nil, ErrAccountDisabled
|
||||
case "locked":
|
||||
if !account.LockedUntil.Valid || account.LockedUntil.Int64 > now {
|
||||
return nil, ErrAccountLocked
|
||||
}
|
||||
if _, err := s.db.WriteQueries.UnlockAccount(ctx, account.PrincipalID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
account.State = "active"
|
||||
account.FailedAttempts = 0
|
||||
account.LockedUntil = sql.NullInt64{}
|
||||
}
|
||||
|
||||
if !account.PasswordHash.Valid {
|
||||
return nil, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
valid, err := VerifyPassword(password, account.PasswordHash.String)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
if !valid {
|
||||
updated, err := s.db.WriteQueries.RegisterFailedLogin(ctx, database.RegisterFailedLoginParams{
|
||||
MaxAttempts: maxFailedAttempts,
|
||||
LockedUntil: sql.NullInt64{
|
||||
Int64: now + lockDuration.Milliseconds(),
|
||||
Valid: true,
|
||||
},
|
||||
PrincipalID: account.PrincipalID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if updated.State == "locked" {
|
||||
return nil, ErrAccountLocked
|
||||
}
|
||||
return nil, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
if _, err := s.db.WriteQueries.RecordLogin(ctx, database.RecordLoginParams{
|
||||
LastLoginAt: sql.NullInt64{
|
||||
Int64: now,
|
||||
Valid: true,
|
||||
},
|
||||
PrincipalID: account.PrincipalID,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.loadUser(ctx, account.PrincipalID)
|
||||
}
|
||||
|
||||
func (s *Service) SetPassword(ctx context.Context, principalID ident.UUID, password string) error {
|
||||
if err := validatePassword(password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
passwordHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
_, err = s.db.WriteQueries.UpdatePassword(ctx, database.UpdatePasswordParams{
|
||||
PasswordHash: sql.NullString{
|
||||
String: passwordHash,
|
||||
Valid: true,
|
||||
},
|
||||
PasswordChangedAt: sql.NullInt64{
|
||||
Int64: now,
|
||||
Valid: true,
|
||||
},
|
||||
PrincipalID: principalID,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) loadUser(ctx context.Context, id ident.UUID) (*User, error) {
|
||||
principal, err := s.db.Queries.GetPrincipal(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
login := principal.Name
|
||||
accountState := "active"
|
||||
mustChangePassword := false
|
||||
|
||||
account, err := s.db.Queries.GetAccount(ctx, id)
|
||||
switch {
|
||||
case err == nil:
|
||||
login = account.Login
|
||||
accountState = account.State
|
||||
mustChangePassword = account.MustChangePassword != 0
|
||||
case !errors.Is(err, sql.ErrNoRows):
|
||||
return nil, err
|
||||
}
|
||||
|
||||
groups, err := s.ListGroups(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &User{
|
||||
ID: principal.ID,
|
||||
Login: login,
|
||||
Name: principal.Name,
|
||||
Status: principal.Status,
|
||||
AccountState: accountState,
|
||||
MustChangePassword: mustChangePassword,
|
||||
CreatedAt: principal.CreatedAt,
|
||||
Groups: groups,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user