feat: add challenge to auth service
This commit is contained in:
@@ -1 +1,49 @@
|
||||
package usecase
|
||||
|
||||
import (
|
||||
"backend/internal/domain"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AuthService interface {
|
||||
GenerateChallenge(ctx context.Context, pubKey string) (*domain.Challenge, error)
|
||||
}
|
||||
|
||||
type authService struct {
|
||||
userRepo *domain.UserRepo
|
||||
sessionRepo *domain.SessionRepo
|
||||
challengeExp uint
|
||||
tokenExp uint
|
||||
refreshTokenExp uint
|
||||
}
|
||||
|
||||
func NewAuthService(
|
||||
userRepo *domain.UserRepo,
|
||||
sessionRepo *domain.SessionRepo,
|
||||
challengeExp uint,
|
||||
tokenExp uint,
|
||||
refreshTokenExp uint) AuthService {
|
||||
return &authService{
|
||||
userRepo: userRepo,
|
||||
sessionRepo: sessionRepo,
|
||||
challengeExp: challengeExp,
|
||||
tokenExp: tokenExp,
|
||||
refreshTokenExp: refreshTokenExp,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *authService) GenerateChallenge(ctx context.Context, pubKey string) (*domain.Challenge, error) {
|
||||
if !common.IsHexAddress(pubKey) {
|
||||
return nil, domain.ErrWalletInvalid
|
||||
}
|
||||
challenge := &domain.Challenge{
|
||||
Message: uuid.New(),
|
||||
TimeStamp: time.Now().UTC(),
|
||||
ExpiresAt: time.Now().Add(time.Duration(s.challengeExp) * time.Minute),
|
||||
}
|
||||
return challenge, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user