mirror of https://github.com/dexidp/dex.git
17 changed files with 717 additions and 126 deletions
@ -1,101 +1,196 @@
|
||||
package server |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
"io/ioutil" |
||||
"log" |
||||
"net/http" |
||||
"path/filepath" |
||||
"sort" |
||||
"text/template" |
||||
) |
||||
|
||||
"github.com/coreos/dex/storage" |
||||
const ( |
||||
tmplApproval = "approval.html" |
||||
tmplLogin = "login.html" |
||||
tmplPassword = "password.html" |
||||
) |
||||
|
||||
const coreOSLogoURL = "https://coreos.com/assets/images/brand/coreos-wordmark-135x40px.png" |
||||
|
||||
var requiredTmpls = []string{ |
||||
tmplApproval, |
||||
tmplLogin, |
||||
tmplPassword, |
||||
} |
||||
|
||||
// TemplateConfig describes.
|
||||
type TemplateConfig struct { |
||||
// Directory of the templates. If empty, these will be loaded from memory.
|
||||
Dir string `yaml:"dir"` |
||||
|
||||
// Defaults to the CoreOS logo and "dex".
|
||||
LogoURL string `yaml:"logoURL"` |
||||
Issuer string `yaml:"issuerName"` |
||||
} |
||||
|
||||
type globalData struct { |
||||
LogoURL string |
||||
Issuer string |
||||
} |
||||
|
||||
func loadTemplates(config TemplateConfig) (*templates, error) { |
||||
var tmpls *template.Template |
||||
if config.Dir != "" { |
||||
files, err := ioutil.ReadDir(config.Dir) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("read dir: %v", err) |
||||
} |
||||
filenames := []string{} |
||||
for _, file := range files { |
||||
if file.IsDir() { |
||||
continue |
||||
} |
||||
filenames = append(filenames, filepath.Join(config.Dir, file.Name())) |
||||
} |
||||
if len(filenames) == 0 { |
||||
return nil, fmt.Errorf("no files in template dir %s", config.Dir) |
||||
} |
||||
if tmpls, err = template.ParseFiles(filenames...); err != nil { |
||||
return nil, fmt.Errorf("parse files: %v", err) |
||||
} |
||||
} else { |
||||
// Load templates from memory. This code is largely copied from the standard library's
|
||||
// ParseFiles source code.
|
||||
// See: https://goo.gl/6Wm4mN
|
||||
for name, data := range defaultTemplates { |
||||
var t *template.Template |
||||
if tmpls == nil { |
||||
tmpls = template.New(name) |
||||
} |
||||
if name == tmpls.Name() { |
||||
t = tmpls |
||||
} else { |
||||
t = tmpls.New(name) |
||||
} |
||||
if _, err := t.Parse(data); err != nil { |
||||
return nil, fmt.Errorf("parsing %s: %v", name, err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
missingTmpls := []string{} |
||||
for _, tmplName := range requiredTmpls { |
||||
if tmpls.Lookup(tmplName) == nil { |
||||
missingTmpls = append(missingTmpls, tmplName) |
||||
} |
||||
} |
||||
if len(missingTmpls) > 0 { |
||||
return nil, fmt.Errorf("missing template(s): %s", missingTmpls) |
||||
} |
||||
|
||||
if config.LogoURL == "" { |
||||
config.LogoURL = coreOSLogoURL |
||||
} |
||||
if config.Issuer == "" { |
||||
config.Issuer = "dex" |
||||
} |
||||
|
||||
return &templates{ |
||||
globalData: config, |
||||
loginTmpl: tmpls.Lookup(tmplLogin), |
||||
approvalTmpl: tmpls.Lookup(tmplApproval), |
||||
passwordTmpl: tmpls.Lookup(tmplPassword), |
||||
}, nil |
||||
} |
||||
|
||||
var scopeDescriptions = map[string]string{ |
||||
"offline_access": "Have offline access", |
||||
"profile": "View basic profile information", |
||||
"email": "View your email", |
||||
} |
||||
|
||||
type templates struct { |
||||
globalData TemplateConfig |
||||
loginTmpl *template.Template |
||||
approvalTmpl *template.Template |
||||
passwordTmpl *template.Template |
||||
} |
||||
|
||||
type connectorInfo struct { |
||||
DisplayName string |
||||
URL string |
||||
ID string |
||||
Name string |
||||
URL string |
||||
} |
||||
|
||||
var loginTmpl = template.Must(template.New("login-template").Parse(`<html> |
||||
<head></head> |
||||
<body> |
||||
<p>Login options</p> |
||||
{{ range $i, $connector := .Connectors }} |
||||
<a href="{{ $connector.URL }}?state={{ $.State }}">{{ $connector.DisplayName }}</a> |
||||
{{ end }} |
||||
</body> |
||||
</html>`)) |
||||
|
||||
func renderLoginOptions(w http.ResponseWriter, connectors []connectorInfo, state string) { |
||||
type byName []connectorInfo |
||||
|
||||
func (n byName) Len() int { return len(n) } |
||||
func (n byName) Less(i, j int) bool { return n[i].Name < n[j].Name } |
||||
func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] } |
||||
|
||||
func (t *templates) login(w http.ResponseWriter, connectors []connectorInfo, state string) { |
||||
sort.Sort(byName(connectors)) |
||||
|
||||
data := struct { |
||||
TemplateConfig |
||||
Connectors []connectorInfo |
||||
State string |
||||
}{connectors, state} |
||||
renderTemplate(w, loginTmpl, data) |
||||
}{t.globalData, connectors, state} |
||||
renderTemplate(w, t.loginTmpl, data) |
||||
} |
||||
|
||||
var passwordTmpl = template.Must(template.New("password-template").Parse(`<html> |
||||
<body> |
||||
<p>Login</p> |
||||
<form action="{{ .Callback }}" method="POST"> |
||||
Login: <input type="text" name="login"/><br/> |
||||
Password: <input type="password" name="password"/><br/> |
||||
<input type="hidden" name="state" value="{{ .State }}"/> |
||||
<input type="submit"/> |
||||
{{ if .Message }} |
||||
<p>Error: {{ .Message }}</p> |
||||
{{ end }} |
||||
</form> |
||||
</body> |
||||
</html>`)) |
||||
|
||||
func renderPasswordTmpl(w http.ResponseWriter, state, callback, message string) { |
||||
func (t *templates) password(w http.ResponseWriter, state, callback, lastUsername string, lastWasInvalid bool) { |
||||
data := struct { |
||||
TemplateConfig |
||||
State string |
||||
Callback string |
||||
Message string |
||||
}{state, callback, message} |
||||
renderTemplate(w, passwordTmpl, data) |
||||
PostURL string |
||||
Username string |
||||
Invalid bool |
||||
}{t.globalData, state, callback, lastUsername, lastWasInvalid} |
||||
renderTemplate(w, t.passwordTmpl, data) |
||||
} |
||||
|
||||
var approvalTmpl = template.Must(template.New("approval-template").Parse(`<html> |
||||
<body> |
||||
<p>User: {{ .User }}</p> |
||||
<p>Client: {{ .ClientName }}</p> |
||||
<form method="post"> |
||||
<input type="hidden" name="state" value="{{ .State }}"/> |
||||
<input type="hidden" name="approval" value="approve"> |
||||
<button type="submit">Approve</button> |
||||
</form> |
||||
<form method="post"> |
||||
<input type="hidden" name="state" value="{{ .State }}"/> |
||||
<input type="hidden" name="approval" value="reject"> |
||||
<button type="submit">Reject</button> |
||||
</form> |
||||
</body> |
||||
</html>`)) |
||||
|
||||
func renderApprovalTmpl(w http.ResponseWriter, state string, identity storage.Claims, client storage.Client, scopes []string) { |
||||
func (t *templates) approval(w http.ResponseWriter, state, username, clientName string, scopes []string) { |
||||
accesses := []string{} |
||||
for _, scope := range scopes { |
||||
access, ok := scopeDescriptions[scope] |
||||
if ok { |
||||
accesses = append(accesses, access) |
||||
} |
||||
} |
||||
sort.Strings(accesses) |
||||
data := struct { |
||||
User string |
||||
ClientName string |
||||
State string |
||||
}{identity.Email, client.Name, state} |
||||
renderTemplate(w, approvalTmpl, data) |
||||
TemplateConfig |
||||
User string |
||||
Client string |
||||
State string |
||||
Scopes []string |
||||
}{t.globalData, username, clientName, state, accesses} |
||||
renderTemplate(w, t.approvalTmpl, data) |
||||
} |
||||
|
||||
func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data interface{}) { |
||||
err := tmpl.Execute(w, data) |
||||
if err == nil { |
||||
return |
||||
} |
||||
// small io.Writer utilitiy to determine if executing the template wrote to the underlying response writer.
|
||||
type writeRecorder struct { |
||||
wrote bool |
||||
w io.Writer |
||||
} |
||||
|
||||
func (w *writeRecorder) Write(p []byte) (n int, err error) { |
||||
w.wrote = true |
||||
return w.w.Write(p) |
||||
} |
||||
|
||||
switch err := err.(type) { |
||||
case template.ExecError: |
||||
// An ExecError guarentees that Execute has not written to the underlying reader.
|
||||
func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data interface{}) { |
||||
wr := &writeRecorder{w: w} |
||||
if err := tmpl.Execute(wr, data); err != nil { |
||||
log.Printf("Error rendering template %s: %s", tmpl.Name(), err) |
||||
|
||||
// TODO(ericchiang): replace with better internal server error.
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError) |
||||
default: |
||||
// An error with the underlying write, such as the connection being
|
||||
// dropped. Ignore for now.
|
||||
if !wr.wrote { |
||||
// TODO(ericchiang): replace with better internal server error.
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError) |
||||
} |
||||
} |
||||
return |
||||
} |
||||
|
||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,79 @@
|
||||
// +build ignore
|
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"io/ioutil" |
||||
"log" |
||||
"os/exec" |
||||
"path/filepath" |
||||
) |
||||
|
||||
// ignoreFile uses "git check-ignore" to determine if we should ignore a file.
|
||||
func ignoreFile(p string) (ok bool, err error) { |
||||
err = exec.Command("git", "check-ignore", p).Run() |
||||
if err == nil { |
||||
return true, nil |
||||
} |
||||
exitErr, ok := err.(*exec.ExitError) |
||||
if ok { |
||||
if sys := exitErr.Sys(); sys != nil { |
||||
e, ok := sys.(interface { |
||||
// Is the returned value something that returns an exit status?
|
||||
ExitStatus() int |
||||
}) |
||||
if ok && e.ExitStatus() == 1 { |
||||
return false, nil |
||||
} |
||||
} |
||||
} |
||||
return false, err |
||||
} |
||||
|
||||
type fileData struct { |
||||
name string |
||||
data string |
||||
} |
||||
|
||||
func main() { |
||||
dir, err := ioutil.ReadDir("web/templates") |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
files := []fileData{} |
||||
for _, file := range dir { |
||||
p := filepath.Join("web/templates", file.Name()) |
||||
ignore, err := ignoreFile(p) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
if ignore { |
||||
continue |
||||
} |
||||
|
||||
data, err := ioutil.ReadFile(p) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
files = append(files, fileData{file.Name(), string(data)}) |
||||
} |
||||
|
||||
f := new(bytes.Buffer) |
||||
|
||||
fmt.Fprintln(f, "// This file was generated by the makefile. Do not edit.") |
||||
fmt.Fprintln(f) |
||||
fmt.Fprintln(f, "package server") |
||||
fmt.Fprintln(f) |
||||
fmt.Fprintln(f, "// defaultTemplates is a key for file name to file data of the files in web/templates.") |
||||
fmt.Fprintln(f, "var defaultTemplates = map[string]string{") |
||||
for _, file := range files { |
||||
fmt.Fprintf(f, "\t%q: %q,\n", file.name, file.data) |
||||
} |
||||
fmt.Fprintln(f, "}") |
||||
|
||||
if err := ioutil.WriteFile("server/templates_default.go", f.Bytes(), 0644); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
||||
@ -1 +1,16 @@
|
||||
package server |
||||
|
||||
import "testing" |
||||
|
||||
func TestNewTemplates(t *testing.T) { |
||||
var config TemplateConfig |
||||
if _, err := loadTemplates(config); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
} |
||||
|
||||
func TestLoadTemplates(t *testing.T) { |
||||
var config TemplateConfig |
||||
|
||||
config.Dir = "../web/templates" |
||||
} |
||||
|
||||
@ -0,0 +1,42 @@
|
||||
{{ template "header.html" . }} |
||||
|
||||
<div class="panel"> |
||||
<h2 class="heading">Grant Access</h2> |
||||
|
||||
<hr> |
||||
<div class="list-with-title"> |
||||
<div class="subtle-text">{{ .Client }} would like to:</div> |
||||
{{ range $scope := .Scopes }} |
||||
<li class="bullet-point"> |
||||
<div class="subtle-text"> |
||||
{{ $scope }} |
||||
</div> |
||||
</li> |
||||
{{ end }} |
||||
</div> |
||||
<hr> |
||||
|
||||
<div> |
||||
<div class="form-row"> |
||||
<form method="post"> |
||||
<input type="hidden" name="state" value="{{ .State }}"/> |
||||
<input type="hidden" name="approval" value="approve"> |
||||
<button type="submit" class="btn btn-success"> |
||||
<span class="btn-text">Grant Access</span> |
||||
</button> |
||||
</form> |
||||
</div> |
||||
<div class="form-row"> |
||||
<form method="post"> |
||||
<input type="hidden" name="state" value="{{ .State }}"/> |
||||
<input type="hidden" name="approval" value="rejected"> |
||||
<button type="submit" class="btn btn-provider"> |
||||
<span class="btn-text">Cancel</span> |
||||
</button> |
||||
</form> |
||||
</div> |
||||
</div> |
||||
|
||||
</div> |
||||
|
||||
{{ template "footer.html" . }} |
||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,22 @@
|
||||
{{ template "header.html" . }} |
||||
|
||||
<div class="panel"> |
||||
<h2 class="heading">Log in to {{ .Issuer }} </h2> |
||||
|
||||
<div> |
||||
{{ range $c := .Connectors }} |
||||
<div class="form-row"> |
||||
<a href="{{ $c.URL }}?state={{ $.State }}" target="_self"> |
||||
<button class="btn btn-provider"> |
||||
<span class="btn-icon btn-icon-{{ $c.ID }}"></span> |
||||
<span class="btn-text">Log in with {{ $c.Name }}</span> |
||||
</button> |
||||
</a> |
||||
</div> |
||||
{{ end }} |
||||
</div> |
||||
|
||||
</div> |
||||
|
||||
|
||||
{{ template "footer.html" . }} |
||||
@ -0,0 +1,31 @@
|
||||
{{ template "header.html" . }} |
||||
|
||||
<div class="panel"> |
||||
<h2 class="heading">Log in to Your Account</h2> |
||||
<form method="post" action="{{ .PostURL }}"> |
||||
<div class="form-row"> |
||||
<div class="input-desc"> |
||||
<label for="userid">Username</label> |
||||
</div> |
||||
<input tabindex="1" required id="login" name="login" type="text" class="input-box" placeholder="username" {{ if .Username }}value="{{ .Username }}" {{ else }} autofocus {{ end }}/> |
||||
</div> |
||||
<div class="form-row"> |
||||
<div class="input-desc"> |
||||
<label for="password">Password</label> |
||||
</div> |
||||
<input tabindex="2" required id="password" name="password" type="password" class="input-box" placeholder="password" {{ if .Invalid }} autofocus {{ end }}/> |
||||
</div> |
||||
<input type="hidden" name="state" value="{{ .State }}"/> |
||||
|
||||
{{ if .Invalid }} |
||||
<div class="error-box"> |
||||
Invalid username and password. |
||||
</div> |
||||
{{ end }} |
||||
|
||||
<button tabindex="3" type="submit" class="btn btn-primary">Login</button> |
||||
|
||||
</form> |
||||
</div> |
||||
|
||||
{{ template "footer.html" . }} |
||||
Loading…
Reference in new issue