增加密码修改、会话退出和登录记录接口
This commit is contained in:
1 parent
225ce96844
commit
e05c930a9d
9 files changed
+319
-2
No files matched your search
@@ -0,0 +1,110 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var ErrPassword = errors.New("incorrect current password")
|
||||
|
||||
func lockSession(ctx context.Context, tx pgx.Tx, user, token string) error {
|
||||
if _, err := tx.Exec(ctx, "SELECT id FROM users WHERE id=$1 FOR UPDATE", user); err != nil {
|
||||
return err
|
||||
}
|
||||
digest, err := SessionDigest(token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var device string
|
||||
err = tx.QueryRow(ctx, `SELECT d.id FROM sessions s JOIN devices d ON d.id=s.device_id
|
||||
WHERE s.token_hash=$1 AND d.user_id=$2 AND d.revoked_at IS NULL AND s.expires_at>now()
|
||||
FOR UPDATE OF d,s`, digest[:], user).Scan(&device)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrCredentials
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) ChangePassword(ctx context.Context, user, token, current, next string) error {
|
||||
var previousHash string
|
||||
if err := s.db.QueryRow(ctx, "SELECT password_hash FROM users WHERE id=$1", user).Scan(&previousHash); err != nil {
|
||||
return err
|
||||
}
|
||||
if !VerifyPassword(current, previousHash) {
|
||||
return ErrPassword
|
||||
}
|
||||
nextHash := PasswordHash(next)
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = lockSession(ctx, tx, user, token); err != nil {
|
||||
return err
|
||||
}
|
||||
tag, err := tx.Exec(ctx, "UPDATE users SET password_hash=$1 WHERE id=$2 AND password_hash=$3", nextHash, user, previousHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() != 1 {
|
||||
return ErrPassword
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM sessions WHERE device_id IN (SELECT id FROM devices WHERE user_id=$1)", user); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) LogoutOthers(ctx context.Context, user, token string) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = lockSession(ctx, tx, user, token); err != nil {
|
||||
return err
|
||||
}
|
||||
digest, _ := SessionDigest(token)
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM sessions WHERE token_hash<>$2 AND device_id IN
|
||||
(SELECT id FROM devices WHERE user_id=$1)`, user, digest[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) RenameDevice(ctx context.Context, user, device, name string) error {
|
||||
tag, err := s.db.Exec(ctx, "UPDATE devices SET name=$3,name_custom=true WHERE user_id=$1 AND id=$2 AND revoked_at IS NULL", user, device, name)
|
||||
if err == nil && tag.RowsAffected() != 1 {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type LoginRecord struct {
|
||||
ID string `json:"id"`
|
||||
DeviceName string `json:"deviceName"`
|
||||
Platform string `json:"platform"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
CurrentDevice bool `json:"currentDevice"`
|
||||
}
|
||||
|
||||
func (s *Store) LoginHistory(ctx context.Context, user, device string) ([]LoginRecord, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text,device_name,platform,created_at,device_id=$2
|
||||
FROM login_history WHERE user_id=$1 ORDER BY id DESC LIMIT 50`, user, device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := make([]LoginRecord, 0)
|
||||
for rows.Next() {
|
||||
var item LoginRecord
|
||||
if err := rows.Scan(&item.ID, &item.DeviceName, &item.Platform, &item.CreatedAt, &item.CurrentDevice); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return items, rows.Err()
|
||||
}
|
||||
@@ -87,12 +87,19 @@ func (s *Store) Open(ctx context.Context, register bool, username, password, nam
|
||||
return Auth{}, err
|
||||
}
|
||||
}
|
||||
var currentHash string
|
||||
if err = tx.QueryRow(ctx, "SELECT password_hash FROM users WHERE id=$1 FOR UPDATE", result.User.ID).Scan(¤tHash); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
if currentHash != hash {
|
||||
return Auth{}, ErrCredentials
|
||||
}
|
||||
|
||||
row := tx.QueryRow(ctx, `
|
||||
INSERT INTO devices(user_id, public_key, name, platform)
|
||||
VALUES ($1,$2,$3,$4)
|
||||
ON CONFLICT (user_id, public_key) DO UPDATE
|
||||
SET last_active_at=now(), name=EXCLUDED.name
|
||||
SET last_active_at=now(), name=CASE WHEN devices.name_custom THEN devices.name ELSE EXCLUDED.name END
|
||||
WHERE devices.revoked_at IS NULL AND devices.platform=EXCLUDED.platform
|
||||
RETURNING id, name, platform, created_at, last_active_at, revoked_at
|
||||
`, result.User.ID, publicKey, name, platform)
|
||||
@@ -107,6 +114,14 @@ func (s *Store) Open(ctx context.Context, register bool, username, password, nam
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO sessions VALUES ($1,$2,$3)", digest[:], result.Device.ID, time.Now().Add(s.ttl)); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO login_history(user_id,device_id,device_name,platform) VALUES ($1,$2,$3,$4)`,
|
||||
result.User.ID, result.Device.ID, result.Device.Name, result.Device.Platform); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM login_history WHERE user_id=$1 AND id NOT IN
|
||||
(SELECT id FROM login_history WHERE user_id=$1 ORDER BY id DESC LIMIT 50)`, result.User.ID); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
result.Device.SignedIn = true
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user