增加密码修改、会话退出和登录记录接口
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()
|
||||
}
|
||||
Reference in new issue
Block a user