Hallucinate a whole lot of shit...
Too much shit...
This commit is contained in:
65
esi/sso.go
65
esi/sso.go
@@ -16,12 +16,13 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go-eve-pi/repositories"
|
||||
"go-eve-pi/types"
|
||||
"gorm.io/gorm"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
"github.com/fasthttp/router"
|
||||
"github.com/valyala/fasthttp"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -29,22 +30,18 @@ const (
|
||||
issuerTokenURL = "https://login.eveonline.com/v2/oauth/token"
|
||||
)
|
||||
|
||||
// DB interface for database operations
|
||||
type DB interface {
|
||||
GetCharacterByName(characterName string) (*types.Character, error)
|
||||
SaveCharacter(character *types.Character) error
|
||||
AutoMigrate(dst ...interface{}) error
|
||||
}
|
||||
// CharacterRepositoryInterface defines the interface for character operations
|
||||
type CharacterRepositoryInterface = repositories.CharacterRepositoryInterface
|
||||
|
||||
type SSO struct {
|
||||
clientID string
|
||||
redirectURI string
|
||||
scopes []string
|
||||
db DB
|
||||
mu sync.Mutex
|
||||
router *router.Router
|
||||
state string
|
||||
callbackChan chan struct {
|
||||
clientID string
|
||||
redirectURI string
|
||||
scopes []string
|
||||
characterRepo CharacterRepositoryInterface
|
||||
mu sync.Mutex
|
||||
router *router.Router
|
||||
state string
|
||||
callbackChan chan struct {
|
||||
code string
|
||||
state string
|
||||
err error
|
||||
@@ -52,19 +49,14 @@ type SSO struct {
|
||||
}
|
||||
|
||||
// NewSSO creates a new SSO instance
|
||||
func NewSSO(clientID, redirectURI string, scopes []string, db DB) (*SSO, error) {
|
||||
func NewSSO(clientID, redirectURI string, scopes []string, characterRepo CharacterRepositoryInterface) (*SSO, error) {
|
||||
logger.Info("Creating new SSO instance for clientID %s with redirectURI %s and scopes %v", clientID, redirectURI, scopes)
|
||||
|
||||
s := &SSO{
|
||||
clientID: clientID,
|
||||
redirectURI: redirectURI,
|
||||
scopes: scopes,
|
||||
db: db,
|
||||
}
|
||||
|
||||
if err := s.initDB(); err != nil {
|
||||
logger.Error("Failed to initialize SSO database %v", err)
|
||||
return nil, err
|
||||
clientID: clientID,
|
||||
redirectURI: redirectURI,
|
||||
scopes: scopes,
|
||||
characterRepo: characterRepo,
|
||||
}
|
||||
|
||||
logger.Info("SSO instance created successfully")
|
||||
@@ -78,16 +70,7 @@ func (s *SSO) SetRouter(r *router.Router) {
|
||||
s.setupCallbackHandler()
|
||||
}
|
||||
|
||||
func (s *SSO) initDB() error {
|
||||
logger.Debug("Initializing SSO database schema")
|
||||
err := s.db.AutoMigrate(&types.Character{})
|
||||
if err != nil {
|
||||
logger.Error("Failed to migrate Token table %v", err)
|
||||
return err
|
||||
}
|
||||
logger.Debug("SSO database schema initialized successfully")
|
||||
return nil
|
||||
}
|
||||
// initDB is no longer needed as migrations are handled by the repository
|
||||
|
||||
// GetCharacter returns a valid character object for the given character name
|
||||
// If no token exists, it will start the OAuth flow
|
||||
@@ -98,7 +81,7 @@ func (s *SSO) GetCharacter(ctx context.Context, characterName string) (types.Cha
|
||||
defer s.mu.Unlock()
|
||||
|
||||
// Try to get existing token from DB
|
||||
char, err := s.db.GetCharacterByName(characterName)
|
||||
char, err := s.characterRepo.GetCharacterByName(characterName)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Info("No existing token found for character %s, starting authentication flow", characterName)
|
||||
@@ -107,7 +90,7 @@ func (s *SSO) GetCharacter(ctx context.Context, characterName string) (types.Cha
|
||||
return types.Character{}, err
|
||||
}
|
||||
// After authentication, fetch the token from DB
|
||||
char, err = s.db.GetCharacterByName(characterName)
|
||||
char, err = s.characterRepo.GetCharacterByName(characterName)
|
||||
if err != nil {
|
||||
return types.Character{}, err
|
||||
}
|
||||
@@ -124,7 +107,7 @@ func (s *SSO) GetCharacter(ctx context.Context, characterName string) (types.Cha
|
||||
if eveCharID > 0 {
|
||||
char.ID = eveCharID
|
||||
logger.Debug("Updating character %s with ID: %d", characterName, eveCharID)
|
||||
if err := s.db.SaveCharacter(char); err != nil {
|
||||
if err := s.characterRepo.SaveCharacter(char); err != nil {
|
||||
logger.Warning("Failed to update character %s with ID: %v", characterName, err)
|
||||
}
|
||||
} else {
|
||||
@@ -143,7 +126,7 @@ func (s *SSO) GetCharacter(ctx context.Context, characterName string) (types.Cha
|
||||
return types.Character{}, err
|
||||
}
|
||||
// After re-authentication, fetch the token from DB
|
||||
char, err = s.db.GetCharacterByName(characterName)
|
||||
char, err = s.characterRepo.GetCharacterByName(characterName)
|
||||
if err != nil {
|
||||
return types.Character{}, err
|
||||
}
|
||||
@@ -213,7 +196,7 @@ func (s *SSO) startAuthFlow(ctx context.Context, characterName string) error {
|
||||
_, eveCharID := parseTokenCharacter(char.AccessToken)
|
||||
char.ID = eveCharID
|
||||
logger.Debug("Saving token to database for character %s (EVE ID: %d)", characterName, eveCharID)
|
||||
if err := s.db.SaveCharacter(char); err != nil {
|
||||
if err := s.characterRepo.SaveCharacter(char); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -397,7 +380,7 @@ func (s *SSO) refreshToken(ctx context.Context, char *types.Character) error {
|
||||
}
|
||||
|
||||
logger.Debug("Saving refreshed token to database for character %s", char.CharacterName)
|
||||
if err := s.db.SaveCharacter(char); err != nil {
|
||||
if err := s.characterRepo.SaveCharacter(char); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user