Hi,
I have a quick and dirty hack used as a proof of concept to achieve this, that is a slightly different way of achieving this.
It essentially is embedding a mux.Router into a new defined struct:
- the struct allows a list of paths to be defined as secured urls.
- the ServeHTTP method is then overwritten to perform pre-processing (check if auth for request url required, check for authenticated user, redirect, etc)
- finally, if there is no redirection (url does not require auth, or user is already authenticated) call the ServeHTTP of the mux router
func (a *AuthManager) ServeHTTP(w http.ResponseWriter, req *http.Request) {
session, _ := a.Store.Get(req, a.sessionName)
authPrincipal := session.Values[AUTHENTICATED_PRINCIPAL]
if a.isAuthRequired(req) && authPrincipal == nil {
// omitted, some pre-processing
}
http.Redirect(w, req, "/", http.StatusFound)
}
a.Router.ServeHTTP(w, req)
}
I had some ideas of using this to create http request filters like functionality and implementing the logic as a filter. So I guess my main question is whether this is a recommended approach and how clean is it?