Fix conditional rendering of OIDC button in login

This commit is contained in:
Jan Eitzinger 2024-03-28 13:18:25 +01:00
parent c3d2508693
commit 50401e0030
3 changed files with 12 additions and 11 deletions

View File

@ -345,16 +345,17 @@ func main() {
buildInfo := web.Build{Version: version, Hash: commit, Buildtime: date}
info := map[string]interface{}{}
info["hasOpenIDConnect"] = "false"
info["hasOpenIDConnect"] = false
if config.Keys.OpenIDProvider != "" {
openIDConnect := auth.NewOIDC(authentication)
openIDConnect.RegisterEndpoints(r)
info["hasOpenIDConnect"] = "true"
info["hasOpenIDConnect"] = true
}
r.HandleFunc("/login", func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("Content-Type", "text/html; charset=utf-8")
log.Debugf("##%v##", info)
web.RenderTemplate(rw, "login.tmpl", &web.Page{Title: "Login", Build: buildInfo, Infos: info})
}).Methods(http.MethodGet)
r.HandleFunc("/imprint", func(rw http.ResponseWriter, r *http.Request) {
@ -382,6 +383,7 @@ func main() {
MsgType: "alert-warning",
Message: err.Error(),
Build: buildInfo,
Infos: info,
})
})).Methods(http.MethodPost)
@ -398,6 +400,7 @@ func main() {
MsgType: "alert-warning",
Message: err.Error(),
Build: buildInfo,
Infos: info,
})
}))
@ -410,6 +413,7 @@ func main() {
MsgType: "alert-info",
Message: "Logout successful",
Build: buildInfo,
Infos: info,
})
}))).Methods(http.MethodPost)
@ -426,6 +430,7 @@ func main() {
MsgType: "alert-danger",
Message: err.Error(),
Build: buildInfo,
Infos: info,
})
})
})

View File

@ -61,12 +61,12 @@ func NewOIDC(a *Authentication) *OIDC {
if clientSecret == "" {
log.Warn("environment variable 'OID_CLIENT_SECRET' not set (Open ID connect auth will not work)")
}
redirectURL := "oidc-callback"
client := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: redirectURL,
RedirectURL: "oidc-callback",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}

View File

@ -38,14 +38,10 @@
<input class="form-control" type="password" id="password" name="password" required/>
</div>
<button type="submit" class="btn btn-success">Submit</button>
{{- if .Infos.hasOpenIDConnect}}
<a class="btn btn-primary" href="/oidc-login">OpenID Connect Login</a>
{{end}}
</form>
{{ range $key, $value := .Infos }}
<strong>{{ $key }}</strong>: {{ $value }},
{{ end }}
{{if .Infos.hasOpenIDConnect }}
<a class="btn btn-primary" href="/oidc-login">OpenID Connect Login</a>
{{end}}
</div>
</div>
</div>