Refactor codebase for clarity and consistency.

This commit is contained in:
2024-07-24 23:46:27 +01:00
parent 6de1ccbd17
commit 88c566ee9a
5 changed files with 456 additions and 481 deletions
+61 -5
View File
@@ -1,10 +1,13 @@
package traefikoidc
import "os"
import (
"fmt"
"net/http"
"os"
)
// constants
const (
cookie_name = "_raczylo_oidc"
cookieName = "_raczylo_oidc"
)
type Config struct {
@@ -19,6 +22,59 @@ type Config struct {
}
func CreateConfig() *Config {
infoLogger.SetOutput(os.Stdout)
return &Config{}
return &Config{
Scopes: []string{"openid", "profile", "email"},
LogLevel: "info",
}
}
func (c *Config) Validate() error {
if c.ProviderURL == "" {
return fmt.Errorf("providerURL is required")
}
if c.CallbackURL == "" {
return fmt.Errorf("callbackURL is required")
}
if c.ClientID == "" {
return fmt.Errorf("clientID is required")
}
if c.ClientSecret == "" {
return fmt.Errorf("clientSecret is required")
}
if c.SessionEncryptionKey == "" {
return fmt.Errorf("sessionEncryptionKey is required")
}
return nil
}
type defaultLogger struct {
level string
}
func NewLogger(level string) Logger {
return &defaultLogger{level: level}
}
func (l *defaultLogger) Infof(format string, args ...interface{}) {
if l.level == "info" || l.level == "debug" {
fmt.Printf("INFO: "+format+"\n", args...)
}
}
func (l *defaultLogger) Errorf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "ERROR: "+format+"\n", args...)
}
type HTTPClient interface {
Get(url string) (*http.Response, error)
Do(req *http.Request) (*http.Response, error)
}
type Logger interface {
Infof(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
func handleError(w http.ResponseWriter, message string, code int) {
http.Error(w, message, code)
}