This commit is contained in:
Giovanni Rezcjikov
2026-03-09 18:09:02 +03:00
parent be36c6f0c4
commit f188150c9f
25 changed files with 694 additions and 53 deletions
+10 -1
View File
@@ -3,6 +3,7 @@ package services
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
@@ -100,7 +101,12 @@ func (s *Service) SessionLogin(ctx context.Context, token, email, password strin
}
user, err := s.users.GetUserByEmail(ctx, email)
if err != nil || bcryptCompare(user.PasswordHash, password) != nil {
compareErr := error(nil)
if err == nil {
compareErr = bcryptCompare(user.PasswordHash, password)
}
if err != nil || compareErr != nil {
return "", ErrAccessDenied
}
@@ -321,6 +327,9 @@ func (s *Service) issuePair(ctx context.Context, user *models.User, clientID uui
return nil, models.ErrInternal
}
// Track issued access tokens per user and client to support revocation.
_ = s.redis.SAdd(ctx, fmt.Sprintf("user_client_tokens:%s:%s", user.ID, clientID), accessToken).Err()
_, err = s.oauth.CreateRefreshToken(ctx, models.RefreshTokenRow{
TokenHash: sha256Hex(rawRefresh),
UserID: user.ID,
@@ -0,0 +1,35 @@
package services
import (
"context"
"github.com/dusiburg/kotosoft-id/internal/models"
"github.com/google/uuid"
)
const adminPositionCode = "PRESIDENT"
func (s *Service) GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error) {
if locale == "" {
locale = "en"
}
return s.positions.GetUserPositions(ctx, userID, locale)
}
func (s *Service) GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error) {
if locale == "" {
locale = "en"
}
return s.positions.GetAllPositions(ctx, locale)
}
func (s *Service) UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error {
if locale == "" {
locale = "en"
}
return s.positions.UpsertPositionForLocale(ctx, code, locale, name, description)
}
func (s *Service) UserIsPositionsAdmin(ctx context.Context, userID uuid.UUID) (bool, error) {
return s.positions.UserHasPosition(ctx, userID, adminPositionCode)
}
@@ -57,10 +57,7 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
}
count := int(incrCmd.Val())
remaining := cfg.Max - count
if remaining < 0 {
remaining = 0
}
remaining := max(cfg.Max-count, 0)
if count > cfg.Max {
if cfg.BlockFor > 0 {
@@ -45,6 +45,13 @@ type OAuthRepo interface {
RevokeConsent(ctx context.Context, userID, clientID uuid.UUID, revokedBy *uuid.UUID) error
}
type PositionRepo interface {
GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error)
GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error)
UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error
UserHasPosition(ctx context.Context, userID uuid.UUID, code string) (bool, error)
}
type TokenConfig struct {
AccessTTL time.Duration
RefreshTTL time.Duration
@@ -56,23 +63,26 @@ type ClientAuth struct {
}
type Service struct {
users UserRepo
oauth OAuthRepo
redis *redis.Client
tokens TokenConfig
users UserRepo
oauth OAuthRepo
positions PositionRepo
redis *redis.Client
tokens TokenConfig
}
func New(
users UserRepo,
oauth OAuthRepo,
positions PositionRepo,
rdb *redis.Client,
tokenCfg TokenConfig,
) *Service {
return &Service{
users: users,
oauth: oauth,
redis: rdb,
tokens: tokenCfg,
users: users,
oauth: oauth,
positions: positions,
redis: rdb,
tokens: tokenCfg,
}
}