mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-11-25 18:24:42 +01:00
refactor: partial revert of dd93f9a2c4
This commit is contained in:
@@ -28,8 +28,6 @@ type AuthAgent struct {
|
||||
Database *db.Database
|
||||
LoginRedirectionHandler func(http.ResponseWriter, *http.Request)
|
||||
Logger *logger.Logger
|
||||
//Plugin related
|
||||
PluginAuthMiddleware *PluginAuthMiddleware //Plugin authentication middleware
|
||||
}
|
||||
|
||||
type AuthEndpoints struct {
|
||||
@@ -41,7 +39,7 @@ type AuthEndpoints struct {
|
||||
}
|
||||
|
||||
// Constructor
|
||||
func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, systemLogger *logger.Logger, loginRedirectionHandler func(http.ResponseWriter, *http.Request), apiKeyManager *APIKeyManager) *AuthAgent {
|
||||
func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, systemLogger *logger.Logger, loginRedirectionHandler func(http.ResponseWriter, *http.Request)) *AuthAgent {
|
||||
store := sessions.NewCookieStore(key)
|
||||
err := sysdb.NewTable("auth")
|
||||
if err != nil {
|
||||
@@ -49,9 +47,6 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//Initialize the plugin authentication middleware
|
||||
pluginAuthMiddleware := NewPluginAuthMiddleware(apiKeyManager)
|
||||
|
||||
//Create a new AuthAgent object
|
||||
newAuthAgent := AuthAgent{
|
||||
SessionName: sessionName,
|
||||
@@ -59,7 +54,6 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
||||
Database: sysdb,
|
||||
LoginRedirectionHandler: loginRedirectionHandler,
|
||||
Logger: systemLogger,
|
||||
PluginAuthMiddleware: pluginAuthMiddleware,
|
||||
}
|
||||
|
||||
//Return the authAgent
|
||||
|
||||
@@ -25,7 +25,7 @@ func NewManagedHTTPRouter(option RouterOption) *RouterDef {
|
||||
}
|
||||
}
|
||||
|
||||
func (router *RouterDef) HandleFunc(endpoint string, handler func(http.ResponseWriter, *http.Request), pluginAccessible bool) error {
|
||||
func (router *RouterDef) HandleFunc(endpoint string, handler func(http.ResponseWriter, *http.Request)) error {
|
||||
//Check if the endpoint already registered
|
||||
if _, exist := router.endpoints[endpoint]; exist {
|
||||
fmt.Println("WARNING! Duplicated registering of web endpoint: " + endpoint)
|
||||
@@ -34,28 +34,31 @@ func (router *RouterDef) HandleFunc(endpoint string, handler func(http.ResponseW
|
||||
|
||||
authAgent := router.option.AuthAgent
|
||||
|
||||
authWrapper := func(w http.ResponseWriter, r *http.Request) {
|
||||
//Check authentication of the user
|
||||
X_Plugin_Auth := r.Header.Get("X-Zoraxy-Plugin-Auth")
|
||||
if router.option.RequireAuth && !(pluginAccessible && X_Plugin_Auth == "true") {
|
||||
authAgent.HandleCheckAuth(w, r, func(w http.ResponseWriter, r *http.Request) {
|
||||
handler(w, r)
|
||||
})
|
||||
} else {
|
||||
handler(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// if the endpoint is supposed to be plugin accessible, wrap it with plugin authentication middleware
|
||||
if pluginAccessible {
|
||||
authWrapper = router.option.AuthAgent.PluginAuthMiddleware.WrapHandler(endpoint, authWrapper)
|
||||
}
|
||||
|
||||
//OK. Register handler
|
||||
if router.option.TargetMux == nil {
|
||||
http.HandleFunc(endpoint, authWrapper)
|
||||
http.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
|
||||
//Check authentication of the user
|
||||
if router.option.RequireAuth {
|
||||
authAgent.HandleCheckAuth(w, r, func(w http.ResponseWriter, r *http.Request) {
|
||||
handler(w, r)
|
||||
})
|
||||
} else {
|
||||
handler(w, r)
|
||||
}
|
||||
|
||||
})
|
||||
} else {
|
||||
router.option.TargetMux.HandleFunc(endpoint, authWrapper)
|
||||
router.option.TargetMux.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) {
|
||||
//Check authentication of the user
|
||||
if router.option.RequireAuth {
|
||||
authAgent.HandleCheckAuth(w, r, func(w http.ResponseWriter, r *http.Request) {
|
||||
handler(w, r)
|
||||
})
|
||||
} else {
|
||||
handler(w, r)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
router.endpoints[endpoint] = handler
|
||||
|
||||
Reference in New Issue
Block a user