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)
CreateRefreshToken(refreshUuid string, expires time.Time) *http.Cookie
Parse(token string) (string, error)
RefreshExpires() time.Duration
}

View file

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

View file

@ -3,6 +3,7 @@ package auth
import (
"errors"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"merch-parser-api/internal/interfaces"
"merch-parser-api/internal/shared"
@ -12,7 +13,6 @@ import (
type Service struct {
repo Repository
jwtProvider interfaces.JWTProvider
refreshExpiry time.Duration
}
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) {
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) {
log.WithFields(log.Fields{
"user uuid": userUuid,
"current session uuid": sessionUuid,
}).Debug("Auth provider | Refresh session")
return s.createSession(userUuid, sessionUuid)
}
@ -69,7 +78,7 @@ func (s *Service) createSession(userUuid, sessionUuid string) (shared.AuthData,
}
refreshUuid := uuid.NewString()
expires := time.Now().UTC().Add(s.refreshExpiry)
expires := time.Now().UTC().Add(s.jwtProvider.RefreshExpires())
refreshCookie := s.jwtProvider.CreateRefreshToken(refreshUuid, expires)
err = s.repo.CreateRefreshToken(&Session{

View file

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