From dd677540f6202ee1cf1d01d7cf5e12d90865d738 Mon Sep 17 00:00:00 2001
From: Stephan Renatus <srenatus@chef.io>
Date: Thu, 9 Nov 2017 16:20:20 +0100
Subject: [PATCH] show "back" link for password connectors

This way, the user who has selected, say, "Log in with Email" can make up
their mind, and select a different connector instead.

However, if there's only one connector set up, none of this makes sense -- and
the link will thus not be displayed.

Signed-off-by: Stephan Renatus <srenatus@chef.io>
---
 server/handlers.go           | 5 +++--
 server/templates.go          | 5 +++--
 web/templates/password.html  | 5 +++++
 web/themes/coreos/styles.css | 4 ++++
 4 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/server/handlers.go b/server/handlers.go
index c265e0b1..d9700f30 100644
--- a/server/handlers.go
+++ b/server/handlers.go
@@ -223,6 +223,7 @@ func (s *Server) handleConnectorLogin(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	scopes := parseScopes(authReq.Scopes)
+	showBacklink := len(s.connectors) > 1
 
 	switch r.Method {
 	case "GET":
@@ -250,7 +251,7 @@ func (s *Server) handleConnectorLogin(w http.ResponseWriter, r *http.Request) {
 			}
 			http.Redirect(w, r, callbackURL, http.StatusFound)
 		case connector.PasswordConnector:
-			if err := s.templates.password(w, r.URL.String(), "", usernamePrompt(conn), false); err != nil {
+			if err := s.templates.password(w, r.URL.String(), "", usernamePrompt(conn), false, showBacklink); err != nil {
 				s.logger.Errorf("Server template error: %v", err)
 			}
 		case connector.SAMLConnector:
@@ -298,7 +299,7 @@ func (s *Server) handleConnectorLogin(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 		if !ok {
-			if err := s.templates.password(w, r.URL.String(), username, usernamePrompt(passwordConnector), true); err != nil {
+			if err := s.templates.password(w, r.URL.String(), username, usernamePrompt(passwordConnector), true, showBacklink); err != nil {
 				s.logger.Errorf("Server template error: %v", err)
 			}
 			return
diff --git a/server/templates.go b/server/templates.go
index aff4568c..5cadefa3 100644
--- a/server/templates.go
+++ b/server/templates.go
@@ -190,13 +190,14 @@ func (t *templates) login(w http.ResponseWriter, connectors []connectorInfo) err
 	return renderTemplate(w, t.loginTmpl, data)
 }
 
-func (t *templates) password(w http.ResponseWriter, postURL, lastUsername, usernamePrompt string, lastWasInvalid bool) error {
+func (t *templates) password(w http.ResponseWriter, postURL, lastUsername, usernamePrompt string, lastWasInvalid, showBacklink bool) error {
 	data := struct {
 		PostURL        string
+		BackLink       bool
 		Username       string
 		UsernamePrompt string
 		Invalid        bool
-	}{postURL, lastUsername, usernamePrompt, lastWasInvalid}
+	}{postURL, showBacklink, lastUsername, usernamePrompt, lastWasInvalid}
 	return renderTemplate(w, t.passwordTmpl, data)
 }
 
diff --git a/web/templates/password.html b/web/templates/password.html
index bd2e954d..5b585b4e 100644
--- a/web/templates/password.html
+++ b/web/templates/password.html
@@ -25,6 +25,11 @@
     <button tabindex="3" id="submit-login" type="submit" class="dex-btn theme-btn--primary">Login</button>
 
   </form>
+  {{ if .BackLink }}
+  <div class="theme-link-back">
+    <a class="dex-subtle-text" href="javascript:history.back()">Select another login method.</a>
+  </div>
+  {{ end }}
 </div>
 
 {{ template "footer.html" . }}
diff --git a/web/themes/coreos/styles.css b/web/themes/coreos/styles.css
index 517f06a7..2d920571 100644
--- a/web/themes/coreos/styles.css
+++ b/web/themes/coreos/styles.css
@@ -107,3 +107,7 @@
   text-align: left;
   width: 250px;
 }
+
+.theme-link-back {
+  margin-top: 4px;
+}
-- 
GitLab