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

View File

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

View File

@ -38,14 +38,10 @@
<input class="form-control" type="password" id="password" name="password" required/> <input class="form-control" type="password" id="password" name="password" required/>
</div> </div>
<button type="submit" class="btn btn-success">Submit</button> <button type="submit" class="btn btn-success">Submit</button>
<a class="btn btn-primary" href="/oidc-login">OpenID Connect Login</a> {{- if .Infos.hasOpenIDConnect}}
</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> <a class="btn btn-primary" href="/oidc-login">OpenID Connect Login</a>
{{end}} {{end}}
</form>
</div> </div>
</div> </div>
</div> </div>