mirror of
https://github.com/yusing/godoxy.git
synced 2026-02-22 18:37:46 +01:00
- These changes makes the API incombatible with previous versions - Added new types for error handling, success responses, and health checks. - Updated health check logic to utilize the new types for better clarity and structure. - Refactored existing handlers to improve response consistency and error handling. - Updated Makefile to include a new target for generating API types from Swagger. - Updated "new agent" API to respond an encrypted cert pair
61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/yusing/go-proxy/internal/common"
|
|
)
|
|
|
|
var defaultAuth Provider
|
|
|
|
// Initialize sets up authentication providers.
|
|
func Initialize() error {
|
|
if !IsEnabled() {
|
|
return nil
|
|
}
|
|
|
|
var err error
|
|
// Initialize OIDC if configured.
|
|
if common.OIDCIssuerURL != "" {
|
|
defaultAuth, err = NewOIDCProviderFromEnv()
|
|
} else {
|
|
defaultAuth, err = NewUserPassAuthFromEnv()
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func GetDefaultAuth() Provider {
|
|
return defaultAuth
|
|
}
|
|
|
|
func IsEnabled() bool {
|
|
return !common.DebugDisableAuth && (common.APIJWTSecret != nil || IsOIDCEnabled())
|
|
}
|
|
|
|
func IsOIDCEnabled() bool {
|
|
return common.OIDCIssuerURL != ""
|
|
}
|
|
|
|
type nextHandler struct{}
|
|
|
|
var nextHandlerContextKey = nextHandler{}
|
|
|
|
func ProceedNext(w http.ResponseWriter, r *http.Request) {
|
|
next, ok := r.Context().Value(nextHandlerContextKey).(http.HandlerFunc)
|
|
if ok {
|
|
next(w, r)
|
|
} else {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
func AuthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
|
err := defaultAuth.CheckToken(r)
|
|
if err != nil {
|
|
defaultAuth.LoginHandler(w, r)
|
|
} else {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
}
|