get expiry time from provider

This commit is contained in:
nquidox 2025-09-14 14:13:35 +03:00
parent e051c1d17f
commit 1cb033d526
4 changed files with 22 additions and 9 deletions

View file

@ -9,4 +9,5 @@ type JWTProvider interface {
CreateAccessToken(userUuid, sessionUuid string) (string, error) CreateAccessToken(userUuid, sessionUuid string) (string, error)
CreateRefreshToken(refreshUuid string, expires time.Time) *http.Cookie CreateRefreshToken(refreshUuid string, expires time.Time) *http.Cookie
Parse(token string) (string, error) Parse(token string) (string, error)
RefreshExpires() time.Duration
} }

View file

@ -7,10 +7,9 @@ import (
type Handler struct { type Handler struct {
*Service *Service
repo *repo repo *repo
jwtProvider interfaces.JWTProvider jwtProvider interfaces.JWTProvider
utils interfaces.Utils utils interfaces.Utils
RefreshTokenExpTime int64
} }
type Deps struct { type Deps struct {

View file

@ -3,6 +3,7 @@ package auth
import ( import (
"errors" "errors"
"github.com/google/uuid" "github.com/google/uuid"
log "github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
"merch-parser-api/internal/interfaces" "merch-parser-api/internal/interfaces"
"merch-parser-api/internal/shared" "merch-parser-api/internal/shared"
@ -10,9 +11,8 @@ import (
) )
type Service struct { type Service struct {
repo Repository repo Repository
jwtProvider interfaces.JWTProvider jwtProvider interfaces.JWTProvider
refreshExpiry time.Duration
} }
func newService(repo Repository, jwtProvider interfaces.JWTProvider) *Service { func newService(repo Repository, jwtProvider interfaces.JWTProvider) *Service {
@ -55,10 +55,19 @@ func (s *Service) Logout(refreshUuid string) error {
} }
func (s *Service) newSession(userUuid string) (shared.AuthData, error) { func (s *Service) newSession(userUuid string) (shared.AuthData, error) {
return s.createSession(userUuid, uuid.NewString()) newSession := uuid.NewString()
log.WithFields(log.Fields{
"user uuid": userUuid,
"new session uuid": newSession,
}).Debug("Auth provider | New session")
return s.createSession(userUuid, newSession)
} }
func (s *Service) updateSession(userUuid, sessionUuid string) (shared.AuthData, error) { func (s *Service) updateSession(userUuid, sessionUuid string) (shared.AuthData, error) {
log.WithFields(log.Fields{
"user uuid": userUuid,
"current session uuid": sessionUuid,
}).Debug("Auth provider | Refresh session")
return s.createSession(userUuid, sessionUuid) return s.createSession(userUuid, sessionUuid)
} }
@ -69,7 +78,7 @@ func (s *Service) createSession(userUuid, sessionUuid string) (shared.AuthData,
} }
refreshUuid := uuid.NewString() refreshUuid := uuid.NewString()
expires := time.Now().UTC().Add(s.refreshExpiry) expires := time.Now().UTC().Add(s.jwtProvider.RefreshExpires())
refreshCookie := s.jwtProvider.CreateRefreshToken(refreshUuid, expires) refreshCookie := s.jwtProvider.CreateRefreshToken(refreshUuid, expires)
err = s.repo.CreateRefreshToken(&Session{ err = s.repo.CreateRefreshToken(&Session{

View file

@ -90,6 +90,10 @@ func (j *JWT) Parse(token string) (string, error) {
return "", fmt.Errorf("invalid token") return "", fmt.Errorf("invalid token")
} }
func (j *JWT) RefreshExpires() time.Duration {
return j.RefreshExpire
}
func duration(minutes string) time.Duration { func duration(minutes string) time.Duration {
dur, err := strconv.Atoi(minutes) dur, err := strconv.Atoi(minutes)
if err != nil { if err != nil {