feat(auth): refactor authentication handling by removing redundant checks and introducing dynamic auth method determination

This commit is contained in:
Avinash Sridhar 2025-04-30 13:00:11 -04:00 • committed by Toby Padilla
commit f843bd4243
4 changed files with 12 additions and 19 deletions

View file

@ -74,18 +74,6 @@ func main() {
// Initialize authentication services
authService := auth.NewAuthService(cfg)
if cfg.RequireAuth {
log.Println("Authentication is required for package publishing")
if cfg.GithubClientID == "" || cfg.GithubClientSecret == "" {
log.Println("Warning: GitHub OAuth credentials not configured but authentication is required")
} else {
log.Println("GitHub OAuth authentication is configured")
}
} else {
log.Println("Authentication is optional for package publishing")
}
// Initialize HTTP server
server := api.NewServer(cfg, registryService, authService)

View file

@ -65,9 +65,20 @@ func PublishHandler(registry service.RegistryService, authService auth.Service)
token = authHeader[7:]
}
// Determine authentication method based on server name prefix
var authMethod model.AuthMethod
switch {
case strings.HasPrefix(serverDetail.Name, "io.github"):
authMethod = model.AuthMethodGitHub
// Additional cases can be added here for other prefixes
default:
// Keep the default auth method as AuthMethodNone
authMethod = model.AuthMethodNone
}
// Setup authentication info
a := model.Authentication{
Method: model.AuthMethodGitHub,
Method: authMethod,
Token: token,
RepoRef: serverDetail.Name,
}

View file

@ -39,11 +39,6 @@ func (s *AuthServiceImpl) CheckAuthStatus(ctx context.Context, statusToken strin
// ValidateAuth validates authentication credentials
func (s *AuthServiceImpl) ValidateAuth(ctx context.Context, auth model.Authentication) (bool, error) {
// If authentication is not required, allow access
if !s.config.RequireAuth {
return true, nil
}
// If authentication is required but not provided
if auth.Method == "" || auth.Method == model.AuthMethodNone {
return false, ErrAuthRequired

View file

@ -17,7 +17,6 @@ type Config struct {
// GitHub OAuth settings
GithubClientID string `env:"GITHUB_CLIENT_ID" envDefault:""`
GithubClientSecret string `env:"GITHUB_CLIENT_SECRET" envDefault:""`
RequireAuth bool `env:"REQUIRE_AUTH" envDefault:"false"` // Whether to require authentication for publishing
}
// NewConfig creates a new configuration with default values