dynamic provider listing on login form

This commit is contained in:
Sebastian Mancke
2017-04-30 00:58:47 +02:00
parent 66af9505e2
commit c50c3f32e2
3 changed files with 64 additions and 36 deletions
+3 -7
View File
@@ -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")
}
+49 -29
View File
@@ -6,6 +6,7 @@ import (
"github.com/tarent/loginsrv/model"
"html/template"
"net/http"
"strings"
)
const loginForm = `<!DOCTYPE html>
@@ -69,35 +70,43 @@ const loginForm = `<!DOCTYPE html>
<a class="btn btn-md btn-primary" href="login?logout=true">Logout</a>
{{end}}
{{else}}
<a class="btn btn-block btn-lg btn-social btn-github" href="login/github">
<span class="fa fa-github"></span> Sign in with Github
</a>
<div class="login-or-container">
<hr class="login-or-hr">
<div class="login-or lead">or</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h4>Sign in</h4>
{{ if .Failure}}<div class="alert alert-warning" role="alert">Invalid credentials</div>{{end}}
</div>
{{ range $index, $oauth := .Config.Oauth }}
<a class="btn btn-block btn-lg btn-social btn-{{ $oauth.provider }}" href="login/{{ $oauth.provider }}">
<span class="fa fa-{{ $oauth.provider }}"></span> Sign in with {{ $oauth.provider | ucfirst }}
</a>
{{end}}
{{if and (not (eq (len .Config.Backends) 0)) (not (eq (len .Config.Oauth) 0))}}
<div class="login-or-container">
<hr class="login-or-hr">
<div class="login-or lead">or</div>
</div>
{{end}}
{{if not (eq (len .Config.Backends) 0) }}
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h4>Sign in</h4>
{{ if .Failure}}<div class="alert alert-warning" role="alert">Invalid credentials</div>{{end}}
</div>
</div>
<div class="panel-body">
<form accept-charset="UTF-8" role="form" method="POST" action="{{.Path}}">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="username" value="{{.UserInfo.Sub}}" type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Login">
</fieldset>
</form>
</div>
</div>
<div class="panel-body">
<form accept-charset="UTF-8" role="form" method="POST" action="{{.Path}}">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="username" value="{{.UserInfo.Sub}}" type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Login">
</fieldset>
</form>
</div>
</div>
{{end}}
{{end}}
</div>
</div>
@@ -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:]
}
+12
View File
@@ -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"))
}