get expiry time from provider
This commit is contained in:
parent
e051c1d17f
commit
1cb033d526
4 changed files with 22 additions and 9 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,9 @@ import (
|
|||
|
||||
type Handler struct {
|
||||
*Service
|
||||
repo *repo
|
||||
jwtProvider interfaces.JWTProvider
|
||||
utils interfaces.Utils
|
||||
RefreshTokenExpTime int64
|
||||
repo *repo
|
||||
jwtProvider interfaces.JWTProvider
|
||||
utils interfaces.Utils
|
||||
}
|
||||
|
||||
type Deps struct {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -10,9 +11,8 @@ import (
|
|||
)
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
jwtProvider interfaces.JWTProvider
|
||||
refreshExpiry time.Duration
|
||||
repo Repository
|
||||
jwtProvider interfaces.JWTProvider
|
||||
}
|
||||
|
||||
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{
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue