98 lines
3.2 KiB
Go
98 lines
3.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
)
|
|
|
|
func (s *Store) OpenAccountSession(ctx context.Context, expires time.Time) (identity.AccountSession, error) {
|
|
p, ok := access.PrincipalFromContext(ctx)
|
|
if !ok {
|
|
return identity.AccountSession{}, identity.ErrCredentialRejected
|
|
}
|
|
row, err := s.queries().GetIdentity(ctx, string(p.IdentityID))
|
|
if err != nil {
|
|
return identity.AccountSession{}, err
|
|
}
|
|
value := identityFromRow(row)
|
|
if value.Kind != identity.KindUser || !value.CanAct() {
|
|
return identity.AccountSession{}, identity.ErrCredentialRejected
|
|
}
|
|
result := identity.AccountSession{Identity: value, Token: rand.Text(), Expires: time.Now().UTC().Add(identity.AccountSessionLifetime)}
|
|
if expires.IsZero() || !expires.After(time.Now()) {
|
|
return identity.AccountSession{}, identity.ErrCredentialRejected
|
|
}
|
|
if expires.Before(result.Expires) {
|
|
result.Expires = expires
|
|
}
|
|
_, err = s.pool.Exec(ctx, `INSERT INTO account_sessions (token_hash,identity_id,expires_at) VALUES ($1,$2,$3)`, sessionHash(result.Token), string(p.IdentityID), result.Expires)
|
|
return result, err
|
|
}
|
|
|
|
func (s *Store) RequireFreshAccountSession(ctx context.Context, token string) error {
|
|
p, ok := access.PrincipalFromContext(ctx)
|
|
if !ok {
|
|
return identity.ErrCredentialRejected
|
|
}
|
|
var fresh bool
|
|
err := s.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM account_sessions WHERE token_hash=$1 AND identity_id=$2 AND expires_at>now() AND issued_at>now()-interval '10 minutes')`, sessionHash(token), string(p.IdentityID)).Scan(&fresh)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !fresh {
|
|
return identity.ErrCredentialRejected
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) CreateAccountRecovery(ctx context.Context) (string, error) {
|
|
p, ok := access.PrincipalFromContext(ctx)
|
|
if !ok {
|
|
return "", identity.ErrCredentialRejected
|
|
}
|
|
code := rand.Text() + rand.Text()
|
|
result, err := s.pool.Exec(ctx, `UPDATE accounts SET recovery_hash=$2 WHERE identity_id=$1`, string(p.IdentityID), sessionHash(code))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if result.RowsAffected() != 1 {
|
|
return "", identity.ErrNotFound
|
|
}
|
|
return code, nil
|
|
}
|
|
|
|
func (s *Store) RecoverAccount(ctx context.Context, email, code, password string) error {
|
|
if len(code) < 52 || len(code) > 128 {
|
|
return identity.ErrCredentialRejected
|
|
}
|
|
hash, err := identity.HashPassword(password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
var id string
|
|
err = tx.QueryRow(ctx, `UPDATE accounts a SET password_hash=$3,recovery_hash=NULL,failed_attempts=0,locked_until=NULL FROM identities i WHERE a.identity_id=i.id AND i.lifecycle='active' AND a.email=$1 AND a.recovery_hash=$2 RETURNING a.identity_id`, strings.ToLower(strings.TrimSpace(email)), sessionHash(code), hash).Scan(&id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return identity.ErrCredentialRejected
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err = tx.Exec(ctx, `DELETE FROM account_sessions WHERE identity_id=$1`, id); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|