feat: redirect to requested page after login, solves #281

This commit is contained in:
Christoph Kluge 2024-11-26 16:21:16 +01:00
parent e1be6c7138
commit 38ce40ae7d
5 changed files with 18 additions and 15 deletions

View File

@ -110,9 +110,7 @@ func serverInit() {
if !config.Keys.DisableAuthentication { if !config.Keys.DisableAuthentication {
router.Handle("/login", authHandle.Login( router.Handle("/login", authHandle.Login(
// On success: // On success: Handled within Login()
http.RedirectHandler("/", http.StatusTemporaryRedirect),
// On failure: // On failure:
func(rw http.ResponseWriter, r *http.Request, err error) { func(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Add("Content-Type", "text/html; charset=utf-8") rw.Header().Add("Content-Type", "text/html; charset=utf-8")
@ -127,9 +125,7 @@ func serverInit() {
})).Methods(http.MethodPost) })).Methods(http.MethodPost)
router.Handle("/jwt-login", authHandle.Login( router.Handle("/jwt-login", authHandle.Login(
// On success: // On success: Handled within Login()
http.RedirectHandler("/", http.StatusTemporaryRedirect),
// On failure: // On failure:
func(rw http.ResponseWriter, r *http.Request, err error) { func(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Add("Content-Type", "text/html; charset=utf-8") rw.Header().Add("Content-Type", "text/html; charset=utf-8")
@ -170,6 +166,7 @@ func serverInit() {
Message: err.Error(), Message: err.Error(),
Build: buildInfo, Build: buildInfo,
Infos: info, Infos: info,
Redirect: r.RequestURI,
}) })
}) })
}) })

View File

@ -201,7 +201,6 @@ func (auth *Authentication) SaveSession(rw http.ResponseWriter, r *http.Request,
} }
func (auth *Authentication) Login( func (auth *Authentication) Login(
onsuccess http.Handler,
onfailure func(rw http.ResponseWriter, r *http.Request, loginErr error), onfailure func(rw http.ResponseWriter, r *http.Request, loginErr error),
) http.Handler { ) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
@ -238,7 +237,12 @@ func (auth *Authentication) Login(
log.Infof("login successfull: user: %#v (roles: %v, projects: %v)", user.Username, user.Roles, user.Projects) log.Infof("login successfull: user: %#v (roles: %v, projects: %v)", user.Username, user.Roles, user.Projects)
ctx := context.WithValue(r.Context(), repository.ContextUserKey, user) ctx := context.WithValue(r.Context(), repository.ContextUserKey, user)
onsuccess.ServeHTTP(rw, r.WithContext(ctx))
if r.FormValue("redirect") != "" {
http.RedirectHandler(r.FormValue("redirect"), http.StatusFound).ServeHTTP(rw, r.WithContext(ctx))
} else {
http.RedirectHandler(r.FormValue("/"), http.StatusFound).ServeHTTP(rw, r.WithContext(ctx))
}
return return
} }

View File

@ -43,9 +43,9 @@ func RegisterFootprintWorker() {
if err != nil { if err != nil {
continue continue
} }
// NOTE: Additional Subcluster Loop Could Allow For Limited List Of (Energy)Footprint-Metrics Only. // NOTE: Additional Subcluster Loop Could Allow For Limited List Of Footprint-Metrics Only.
// - Chunk-Size Would Then Be 'SubCluster' (Running Jobs, Transactions) as Lists Can Change Within SCs // - Chunk-Size Would Then Be 'SubCluster' (Running Jobs, Transactions) as Lists Can Change Within SCs
// - Would Require Review of 'updateFootprint' And 'updateEnergy' Usage // - Would Require Review of 'updateFootprint' Usage (Logic Could Possibly Be Included Here Completely)
allMetrics := make([]string, 0) allMetrics := make([]string, 0)
metricConfigs := archive.GetCluster(cluster.Name).MetricConfig metricConfigs := archive.GetCluster(cluster.Name).MetricConfig
for _, mc := range metricConfigs { for _, mc := range metricConfigs {

View File

@ -41,6 +41,7 @@
{{- if .Infos.hasOpenIDConnect}} {{- if .Infos.hasOpenIDConnect}}
<a class="btn btn-primary" href="/oidc-login">OpenID Connect Login</a> <a class="btn btn-primary" href="/oidc-login">OpenID Connect Login</a>
{{end}} {{end}}
<input type="hidden" id="redirect" name="redirect" value="{{ .Redirect }}" />
</form> </form>
</div> </div>
</div> </div>

View File

@ -99,6 +99,7 @@ type Page struct {
Infos map[string]interface{} // For generic use (e.g. username for /monitoring/user/<id>, job id for /monitoring/job/<id>) Infos map[string]interface{} // For generic use (e.g. username for /monitoring/user/<id>, job id for /monitoring/job/<id>)
Config map[string]interface{} // UI settings for the currently logged in user (e.g. line width, ...) Config map[string]interface{} // UI settings for the currently logged in user (e.g. line width, ...)
Resampling *schema.ResampleConfig // If not nil, defines resampling trigger and resolutions Resampling *schema.ResampleConfig // If not nil, defines resampling trigger and resolutions
Redirect string // The originally requested URL, for intermediate login handling
} }
func RenderTemplate(rw http.ResponseWriter, file string, page *Page) { func RenderTemplate(rw http.ResponseWriter, file string, page *Page) {