From c50c3f32e2e170cc970b223201d61d703ebe89ed Mon Sep 17 00:00:00 2001 From: Sebastian Mancke Date: Sun, 30 Apr 2017 00:58:47 +0200 Subject: [PATCH] dynamic provider listing on login form --- login/handler_test.go | 10 ++---- login/login_form.go | 78 +++++++++++++++++++++++++--------------- login/login_form_test.go | 12 +++++++ 3 files changed, 64 insertions(+), 36 deletions(-) create mode 100644 login/login_form_test.go diff --git a/login/handler_test.go b/login/handler_test.go index cd722a8..0ef2721 100644 --- a/login/handler_test.go +++ b/login/handler_test.go @@ -69,9 +69,7 @@ func TestHandler_NewFromConfig(t *testing.T) { func TestHandler_LoginForm(t *testing.T) { recorder := call(req("GET", "/context/login", "")) assert.Equal(t, recorder.Code, 200) - assert.Contains(t, recorder.Body.String(), "form") - assert.Contains(t, recorder.Body.String(), `method="POST"`) - assert.Contains(t, recorder.Body.String(), `action="/context/login"`) + assert.Contains(t, recorder.Body.String(), `class="container`) assert.Equal(t, "no-cache, no-store, must-revalidate", recorder.Header().Get("Cache-Control")) } @@ -116,9 +114,7 @@ func TestHandler_LoginWeb(t *testing.T) { // show the login form again after authentication failed recorder = call(req("POST", "/context/login", "username=bob&password=FOOBAR", TypeForm, AcceptHtml)) assert.Equal(t, 403, recorder.Code) - assert.Contains(t, recorder.Body.String(), "form") - assert.Contains(t, recorder.Body.String(), `method="POST"`) - assert.Contains(t, recorder.Body.String(), `action="/context/login"`) + assert.Contains(t, recorder.Body.String(), `class="container"`) assert.Equal(t, recorder.Header().Get("Set-Cookie"), "") } @@ -160,7 +156,7 @@ func TestHandler_LoginError(t *testing.T) { assert.Equal(t, 500, recorder.Code) assert.Contains(t, recorder.Header().Get("Content-Type"), "text/html") - assert.Contains(t, recorder.Body.String(), "form") + assert.Contains(t, recorder.Body.String(), `class="container"`) assert.Contains(t, recorder.Body.String(), "Internal Error") } diff --git a/login/login_form.go b/login/login_form.go index 9f07c1f..12e9210 100644 --- a/login/login_form.go +++ b/login/login_form.go @@ -6,6 +6,7 @@ import ( "github.com/tarent/loginsrv/model" "html/template" "net/http" + "strings" ) const loginForm = ` @@ -69,35 +70,43 @@ const loginForm = ` Logout {{end}} {{else}} - - Sign in with Github - -
- - -
-
-
- -
-

Sign in

- {{ if .Failure}}{{end}} -
+ + {{ range $index, $oauth := .Config.Oauth }} + + Sign in with {{ $oauth.provider | ucfirst }} + + {{end}} + + {{if and (not (eq (len .Config.Backends) 0)) (not (eq (len .Config.Oauth) 0))}} + + {{end}} + + {{if not (eq (len .Config.Backends) 0) }} +
+
+
+

Sign in

+ {{ if .Failure}}{{end}} +
+
+
+
+
+
+ +
+
+ +
+ +
+
+
-
-
-
-
- -
-
- -
- -
-
-
-
+ {{end}} {{end}}
@@ -116,7 +125,10 @@ type loginFormData struct { } func writeLoginForm(w http.ResponseWriter, params loginFormData) { - t := template.Must(template.New("loginForm").Parse(loginForm)) + funcMap := template.FuncMap{ + "ucfirst": ucfirst, + } + t := template.Must(template.New("loginForm").Funcs(funcMap).Parse(loginForm)) b := bytes.NewBuffer(nil) err := t.Execute(b, params) if err != nil { @@ -134,3 +146,11 @@ func writeLoginForm(w http.ResponseWriter, params loginFormData) { w.Write(b.Bytes()) } + +func ucfirst(in string) string { + if in == "" { + return "" + } + + return strings.ToUpper(in[0:1]) + in[1:] +} diff --git a/login/login_form_test.go b/login/login_form_test.go new file mode 100644 index 0000000..a0c9875 --- /dev/null +++ b/login/login_form_test.go @@ -0,0 +1,12 @@ +package login + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_ucfirst(t *testing.T) { + assert.Equal(t, "", ucfirst("")) + assert.Equal(t, "A", ucfirst("a")) + assert.Equal(t, "Abc def", ucfirst("abc def")) +}