Refactor character to types

This commit is contained in:
2025-10-10 22:24:10 +02:00
parent ef02bb810e
commit d7d3a6e888
4 changed files with 39 additions and 43 deletions

View File

@@ -16,6 +16,7 @@ import (
"sync"
"time"
"go-eve-pi/types"
"gorm.io/gorm"
logger "git.site.quack-lab.dev/dave/cylogger"
@@ -28,14 +29,11 @@ const (
issuerTokenURL = "https://login.eveonline.com/v2/oauth/token"
)
type Character struct {
ID int64 `gorm:"primaryKey"` // EVE character ID from JWT token
CharacterName string `gorm:"uniqueIndex"`
AccessToken string
RefreshToken string
ExpiresAt time.Time
UpdatedAt time.Time
CreatedAt time.Time
// DB interface for database operations
type DB interface {
GetCharacterByName(characterName string) (*types.Character, error)
SaveCharacter(character *types.Character) error
AutoMigrate(dst ...interface{}) error
}
type SSO struct {
@@ -54,15 +52,9 @@ type SSO struct {
}
// NewSSO creates a new SSO instance
func NewSSO(clientID, redirectURI string, scopes []string) (*SSO, error) {
func NewSSO(clientID, redirectURI string, scopes []string, db DB) (*SSO, error) {
logger.Info("Creating new SSO instance for clientID %s with redirectURI %s and scopes %v", clientID, redirectURI, scopes)
db, err := GetDB()
if err != nil {
logger.Error("Failed to get database connection %v", err)
return nil, err
}
s := &SSO{
clientID: clientID,
redirectURI: redirectURI,
@@ -88,7 +80,7 @@ func (s *SSO) SetRouter(r *router.Router) {
func (s *SSO) initDB() error {
logger.Debug("Initializing SSO database schema")
err := s.db.AutoMigrate(&Character{})
err := s.db.AutoMigrate(&types.Character{})
if err != nil {
logger.Error("Failed to migrate Token table %v", err)
return err
@@ -100,7 +92,7 @@ func (s *SSO) initDB() error {
// GetCharacter returns a valid character object for the given character name
// If no token exists, it will start the OAuth flow
// If token is expired, it will refresh it automatically
func (s *SSO) GetCharacter(ctx context.Context, characterName string) (Character, error) {
func (s *SSO) GetCharacter(ctx context.Context, characterName string) (types.Character, error) {
logger.Debug("Getting token for character %s", characterName)
s.mu.Lock()
defer s.mu.Unlock()
@@ -112,16 +104,16 @@ func (s *SSO) GetCharacter(ctx context.Context, characterName string) (Character
logger.Info("No existing token found for character %s, starting authentication flow", characterName)
// No token exists, need to authenticate
if err := s.startAuthFlow(ctx, characterName); err != nil {
return Character{}, err
return types.Character{}, err
}
// After authentication, fetch the token from DB
char, err = s.db.GetCharacterByName(characterName)
if err != nil {
return Character{}, err
return types.Character{}, err
}
logger.Info("Successfully authenticated character %s", characterName)
} else {
return Character{}, err
return types.Character{}, err
}
} else {
logger.Debug("Found existing token for character %s, expires at %v", characterName, char.ExpiresAt)
@@ -148,12 +140,12 @@ func (s *SSO) GetCharacter(ctx context.Context, characterName string) (Character
logger.Warning("Token refresh failed for character %s, re-authenticating: %v", characterName, err)
// Refresh failed, need to re-authenticate
if err := s.startAuthFlow(ctx, characterName); err != nil {
return Character{}, err
return types.Character{}, err
}
// After re-authentication, fetch the token from DB
char, err = s.db.GetCharacterByName(characterName)
if err != nil {
return Character{}, err
return types.Character{}, err
}
logger.Info("Successfully re-authenticated character %s", characterName)
} else {
@@ -320,7 +312,7 @@ func (s *SSO) waitForCallback() (code, state string, err error) {
}
}
func (s *SSO) exchangeCodeForToken(ctx context.Context, code, verifier string) (*Character, error) {
func (s *SSO) exchangeCodeForToken(ctx context.Context, code, verifier string) (*types.Character, error) {
logger.Debug("Exchanging authorization code for access token")
form := url.Values{}
form.Set("grant_type", "authorization_code")
@@ -356,7 +348,7 @@ func (s *SSO) exchangeCodeForToken(ctx context.Context, code, verifier string) (
name, _ := parseTokenCharacter(tr.AccessToken)
logger.Info("Successfully exchanged code for token, character: %s", name)
return &Character{
return &types.Character{
CharacterName: name,
AccessToken: tr.AccessToken,
RefreshToken: tr.RefreshToken,
@@ -364,7 +356,7 @@ func (s *SSO) exchangeCodeForToken(ctx context.Context, code, verifier string) (
}, nil
}
func (s *SSO) refreshToken(ctx context.Context, char *Character) error {
func (s *SSO) refreshToken(ctx context.Context, char *types.Character) error {
logger.Debug("Refreshing token for character %s", char.CharacterName)
form := url.Values{}
form.Set("grant_type", "refresh_token")