2026-04-05 21:01:29 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-06 09:49:03 +00:00
|
|
|
"context"
|
2026-04-06 22:03:35 +00:00
|
|
|
"errors"
|
2026-04-05 21:01:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
2026-04-06 09:49:03 +00:00
|
|
|
"time"
|
2026-04-05 21:01:29 +00:00
|
|
|
|
|
|
|
|
"github.com/owncord/server/db"
|
2026-04-06 09:49:03 +00:00
|
|
|
"github.com/owncord/server/telemetry"
|
2026-04-05 21:01:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// UserService handles user profile and session operations.
|
|
|
|
|
type UserService struct {
|
2026-07-19 16:33:58 +00:00
|
|
|
st Store
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewUserService creates a UserService.
|
2026-07-19 16:33:58 +00:00
|
|
|
func NewUserService(st Store) *UserService {
|
2026-04-05 21:01:29 +00:00
|
|
|
return &UserService{st: st}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateProfile updates a user's username and/or avatar.
|
|
|
|
|
// Returns the updated user for response building.
|
2026-04-07 09:10:14 +02:00
|
|
|
func (s *UserService) UpdateProfile(ctx context.Context, userID int64, username string, avatar *string) (*db.User, error) {
|
|
|
|
|
ctx, span := telemetry.GlobalTracer("service/user").Start(ctx, "UserService.UpdateProfile",
|
2026-04-06 09:49:03 +00:00
|
|
|
telemetry.Int64("user_id", userID),
|
|
|
|
|
)
|
|
|
|
|
start := time.Now()
|
|
|
|
|
defer func() {
|
2026-04-06 13:48:41 +00:00
|
|
|
telemetry.TimeSince(ctx, telemetry.NewAppMetrics().ServiceCallDurationSec, start,
|
2026-04-06 09:49:03 +00:00
|
|
|
telemetry.String("method", "UpdateProfile"))
|
|
|
|
|
span.End()
|
|
|
|
|
}()
|
|
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
if err := s.st.UpdateUserProfile(userID, username, avatar); err != nil {
|
2026-04-05 21:31:35 +00:00
|
|
|
if db.IsUniqueConstraintError(err) {
|
|
|
|
|
return nil, fmt.Errorf("%w: username is already taken", ErrConflict)
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("%w: failed to update profile", ErrInternal)
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
user, err := s.st.GetUserByID(userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("%w: failed to fetch updated user", ErrInternal)
|
|
|
|
|
}
|
2026-07-20 10:48:05 +02:00
|
|
|
db.WriteAudit(s.st, userID, "profile_update", "user", userID,
|
2026-04-05 21:01:29 +00:00
|
|
|
fmt.Sprintf("username=%s", username))
|
|
|
|
|
slog.Info("profile updated", "user_id", userID, "username", username)
|
|
|
|
|
return user, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 08:50:35 +02:00
|
|
|
// ChangePasswordResult reports a completed password change. RevokeFailed is
|
|
|
|
|
// set when the password committed but other sessions could not be revoked —
|
|
|
|
|
// a partial success the caller must surface as a warning, never as a 5xx:
|
|
|
|
|
// the old password is already unusable, so telling the user the change
|
|
|
|
|
// "failed" walks them into retrying with a dead password and tripping the
|
|
|
|
|
// password-confirm lockout.
|
|
|
|
|
type ChangePasswordResult struct {
|
|
|
|
|
SessionsRevoked int64
|
|
|
|
|
RevokeFailed bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 22:03:35 +00:00
|
|
|
// ChangePassword updates the user's password and revokes other sessions.
|
2026-07-19 08:50:35 +02:00
|
|
|
func (s *UserService) ChangePassword(userID int64, newPasswordHash string, keepSessionID int64) (ChangePasswordResult, error) {
|
2026-04-05 21:01:29 +00:00
|
|
|
if err := s.st.UpdateUserPassword(userID, newPasswordHash); err != nil {
|
2026-07-19 08:50:35 +02:00
|
|
|
return ChangePasswordResult{}, fmt.Errorf("%w: failed to update password", ErrInternal)
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
2026-07-19 08:50:35 +02:00
|
|
|
|
|
|
|
|
// The password is committed from here on: every path below reports
|
|
|
|
|
// success and writes the audit row.
|
|
|
|
|
var res ChangePasswordResult
|
2026-04-05 21:01:29 +00:00
|
|
|
revoked, err := s.st.DeleteOtherSessions(userID, keepSessionID)
|
2026-07-19 08:50:35 +02:00
|
|
|
res.SessionsRevoked = revoked
|
2026-04-05 21:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("UserService.ChangePassword DeleteOtherSessions", "err", err, "user_id", userID)
|
2026-07-19 08:50:35 +02:00
|
|
|
// One bounded compensating retry: revocation is the security tail of
|
|
|
|
|
// the change and a single immediate retry covers transient write-lock
|
|
|
|
|
// contention. ponytail: one retry, add backoff only if logs show it.
|
|
|
|
|
if revokedRetry, retryErr := s.st.DeleteOtherSessions(userID, keepSessionID); retryErr == nil {
|
|
|
|
|
res.SessionsRevoked += revokedRetry
|
|
|
|
|
} else {
|
|
|
|
|
res.RevokeFailed = true
|
|
|
|
|
}
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
2026-07-20 10:48:05 +02:00
|
|
|
db.WriteAudit(s.st, userID, "password_change", "user", userID, "password changed")
|
2026-07-19 08:50:35 +02:00
|
|
|
slog.Info("password changed", "user_id", userID,
|
|
|
|
|
"sessions_revoked", res.SessionsRevoked, "revoke_failed", res.RevokeFailed)
|
|
|
|
|
return res, nil
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListSessions returns all active sessions for a user.
|
|
|
|
|
func (s *UserService) ListSessions(userID int64) ([]db.Session, error) {
|
|
|
|
|
sessions, err := s.st.ListUserSessions(userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("%w: failed to list sessions", ErrInternal)
|
|
|
|
|
}
|
|
|
|
|
return sessions, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RevokeSession deletes a specific session owned by the user.
|
|
|
|
|
func (s *UserService) RevokeSession(userID, sessionID int64) error {
|
|
|
|
|
if err := s.st.DeleteSessionByID(sessionID, userID); err != nil {
|
2026-04-06 22:03:35 +00:00
|
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
|
|
|
return fmt.Errorf("%w: session not found", ErrNotFound)
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("%w: failed to revoke session", ErrInternal)
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
2026-07-20 10:48:05 +02:00
|
|
|
db.WriteAudit(s.st, userID, "session_revoke", "session", sessionID, "session revoked")
|
2026-04-05 21:01:29 +00:00
|
|
|
slog.Info("session revoked", "user_id", userID, "session_id", sessionID)
|
|
|
|
|
return nil
|
|
|
|
|
}
|