2016-07-25 20:00:28 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-08-25 20:10:19 +00:00
|
|
|
"fmt"
|
2017-02-02 18:29:57 +00:00
|
|
|
"html/template"
|
2016-08-25 20:10:19 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-07-25 20:00:28 +00:00
|
|
|
"net/http"
|
2019-09-27 14:04:43 +00:00
|
|
|
"net/url"
|
2016-11-30 22:26:54 +00:00
|
|
|
"os"
|
2019-09-27 14:04:43 +00:00
|
|
|
"path"
|
2016-08-25 20:10:19 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2016-11-30 22:26:54 +00:00
|
|
|
"strings"
|
2016-08-25 20:10:19 +00:00
|
|
|
)
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2016-08-25 20:10:19 +00:00
|
|
|
const (
|
|
|
|
tmplApproval = "approval.html"
|
|
|
|
tmplLogin = "login.html"
|
|
|
|
tmplPassword = "password.html"
|
2016-10-19 19:45:17 +00:00
|
|
|
tmplOOB = "oob.html"
|
2016-12-14 22:17:59 +00:00
|
|
|
tmplError = "error.html"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
2016-08-25 20:10:19 +00:00
|
|
|
var requiredTmpls = []string{
|
|
|
|
tmplApproval,
|
|
|
|
tmplLogin,
|
|
|
|
tmplPassword,
|
2016-10-19 19:45:17 +00:00
|
|
|
tmplOOB,
|
2016-12-14 22:17:59 +00:00
|
|
|
tmplError,
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
type templates struct {
|
|
|
|
loginTmpl *template.Template
|
|
|
|
approvalTmpl *template.Template
|
|
|
|
passwordTmpl *template.Template
|
|
|
|
oobTmpl *template.Template
|
2016-12-14 22:17:59 +00:00
|
|
|
errorTmpl *template.Template
|
2016-11-30 22:26:54 +00:00
|
|
|
}
|
2016-08-25 20:10:19 +00:00
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
type webConfig struct {
|
|
|
|
dir string
|
|
|
|
logoURL string
|
|
|
|
issuer string
|
|
|
|
theme string
|
|
|
|
issuerURL string
|
2019-08-06 17:14:53 +00:00
|
|
|
extra map[string]string
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
func join(base, path string) string {
|
|
|
|
b := strings.HasSuffix(base, "/")
|
|
|
|
p := strings.HasPrefix(path, "/")
|
|
|
|
switch {
|
|
|
|
case b && p:
|
|
|
|
return base + path[1:]
|
|
|
|
case b || p:
|
|
|
|
return base + path
|
|
|
|
default:
|
|
|
|
return base + "/" + path
|
|
|
|
}
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
func dirExists(dir string) error {
|
|
|
|
stat, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("directory %q does not exist", dir)
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
2016-11-30 22:26:54 +00:00
|
|
|
return fmt.Errorf("stat directory %q: %v", dir, err)
|
|
|
|
}
|
|
|
|
if !stat.IsDir() {
|
|
|
|
return fmt.Errorf("path %q is a file not a directory", dir)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadWebConfig returns static assets, theme assets, and templates used by the frontend by
|
|
|
|
// reading the directory specified in the webConfig.
|
|
|
|
//
|
|
|
|
// The directory layout is expected to be:
|
|
|
|
//
|
|
|
|
// ( web directory )
|
|
|
|
// |- static
|
|
|
|
// |- themes
|
|
|
|
// | |- (theme name)
|
|
|
|
// |- templates
|
|
|
|
//
|
|
|
|
func loadWebConfig(c webConfig) (static, theme http.Handler, templates *templates, err error) {
|
|
|
|
if c.theme == "" {
|
|
|
|
c.theme = "coreos"
|
|
|
|
}
|
|
|
|
if c.issuer == "" {
|
|
|
|
c.issuer = "dex"
|
|
|
|
}
|
|
|
|
if c.dir == "" {
|
|
|
|
c.dir = "./web"
|
|
|
|
}
|
|
|
|
if c.logoURL == "" {
|
2019-09-27 14:04:43 +00:00
|
|
|
c.logoURL = "theme/logo.png"
|
2016-11-30 22:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := dirExists(c.dir); err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("load web dir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
staticDir := filepath.Join(c.dir, "static")
|
|
|
|
templatesDir := filepath.Join(c.dir, "templates")
|
|
|
|
themeDir := filepath.Join(c.dir, "themes", c.theme)
|
|
|
|
|
|
|
|
for _, dir := range []string{staticDir, templatesDir, themeDir} {
|
|
|
|
if err := dirExists(dir); err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("load dir: %v", err)
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
2016-11-30 22:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static = http.FileServer(http.Dir(staticDir))
|
|
|
|
theme = http.FileServer(http.Dir(themeDir))
|
|
|
|
|
|
|
|
templates, err = loadTemplates(c, templatesDir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadTemplates parses the expected templates from the provided directory.
|
|
|
|
func loadTemplates(c webConfig, templatesDir string) (*templates, error) {
|
|
|
|
files, err := ioutil.ReadDir(templatesDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read dir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filenames := []string{}
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
2016-11-30 22:26:54 +00:00
|
|
|
filenames = append(filenames, filepath.Join(templatesDir, file.Name()))
|
|
|
|
}
|
|
|
|
if len(filenames) == 0 {
|
|
|
|
return nil, fmt.Errorf("no files in template dir %q", templatesDir)
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:04:43 +00:00
|
|
|
issuerURL, err := url.Parse(c.issuerURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing issuerURL: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
funcs := map[string]interface{}{
|
|
|
|
"issuer": func() string { return c.issuer },
|
|
|
|
"logo": func() string { return c.logoURL },
|
2019-09-27 14:04:43 +00:00
|
|
|
"url": func(reqPath, assetPath string) string { return relativeURL(issuerURL.Path, reqPath, assetPath) },
|
2017-11-07 09:28:21 +00:00
|
|
|
"lower": strings.ToLower,
|
2019-08-06 17:14:53 +00:00
|
|
|
"extra": func(k string) string { return c.extra[k] },
|
2016-11-30 22:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpls, err := template.New("").Funcs(funcs).ParseFiles(filenames...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse files: %v", err)
|
|
|
|
}
|
2016-08-25 20:10:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
return &templates{
|
|
|
|
loginTmpl: tmpls.Lookup(tmplLogin),
|
|
|
|
approvalTmpl: tmpls.Lookup(tmplApproval),
|
|
|
|
passwordTmpl: tmpls.Lookup(tmplPassword),
|
2016-10-19 19:45:17 +00:00
|
|
|
oobTmpl: tmpls.Lookup(tmplOOB),
|
2016-12-14 22:17:59 +00:00
|
|
|
errorTmpl: tmpls.Lookup(tmplError),
|
2016-08-25 20:10:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-09-27 14:04:43 +00:00
|
|
|
// relativeURL returns the URL of the asset relative to the URL of the request path.
|
|
|
|
// The serverPath is consulted to trim any prefix due in case it is not listening
|
|
|
|
// to the root path.
|
|
|
|
//
|
|
|
|
// Algorithm:
|
|
|
|
// 1. Remove common prefix of serverPath and reqPath
|
|
|
|
// 2. Remove common prefix of assetPath and reqPath
|
|
|
|
// 3. For each part of reqPath remaining(minus one), go up one level (..)
|
|
|
|
// 4. For each part of assetPath remaining, append it to result
|
|
|
|
//
|
|
|
|
//eg
|
|
|
|
//server listens at localhost/dex so serverPath is dex
|
|
|
|
//reqPath is /dex/auth
|
|
|
|
//assetPath is static/main.css
|
|
|
|
//relativeURL("/dex", "/dex/auth", "static/main.css") = "../static/main.css"
|
|
|
|
func relativeURL(serverPath, reqPath, assetPath string) string {
|
|
|
|
|
|
|
|
splitPath := func(p string) []string {
|
|
|
|
res := []string{}
|
|
|
|
parts := strings.Split(path.Clean(p), "/")
|
|
|
|
for _, part := range parts {
|
|
|
|
if part != "" {
|
|
|
|
res = append(res, part)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
stripCommonParts := func(s1, s2 []string) ([]string, []string) {
|
|
|
|
min := len(s1)
|
|
|
|
if len(s2) < min {
|
|
|
|
min = len(s2)
|
|
|
|
}
|
|
|
|
|
|
|
|
splitIndex := min
|
|
|
|
for i := 0; i < min; i++ {
|
|
|
|
if s1[i] != s2[i] {
|
|
|
|
splitIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s1[splitIndex:], s2[splitIndex:]
|
|
|
|
}
|
|
|
|
|
|
|
|
server, req, asset := splitPath(serverPath), splitPath(reqPath), splitPath(assetPath)
|
|
|
|
|
|
|
|
// Remove common prefix of request path with server path
|
|
|
|
server, req = stripCommonParts(server, req)
|
|
|
|
|
|
|
|
// Remove common prefix of request path with asset path
|
|
|
|
asset, req = stripCommonParts(asset, req)
|
|
|
|
|
|
|
|
// For each part of the request remaining (minus one) -> go up one level (..)
|
|
|
|
// For each part of the asset remaining -> append it
|
|
|
|
var relativeURL string
|
|
|
|
for i := 0; i < len(req)-1; i++ {
|
|
|
|
relativeURL = path.Join("..", relativeURL)
|
|
|
|
}
|
|
|
|
relativeURL = path.Join(relativeURL, path.Join(asset...))
|
|
|
|
|
|
|
|
return relativeURL
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:10:19 +00:00
|
|
|
var scopeDescriptions = map[string]string{
|
|
|
|
"offline_access": "Have offline access",
|
|
|
|
"profile": "View basic profile information",
|
2017-12-17 17:08:19 +00:00
|
|
|
"email": "View your email address",
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
type connectorInfo struct {
|
2016-08-25 20:10:19 +00:00
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
URL string
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:10:19 +00:00
|
|
|
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] }
|
|
|
|
|
2019-09-27 14:04:43 +00:00
|
|
|
func (t *templates) login(r *http.Request, w http.ResponseWriter, connectors []connectorInfo, reqPath string) error {
|
2016-08-25 20:10:19 +00:00
|
|
|
sort.Sort(byName(connectors))
|
2016-07-25 20:00:28 +00:00
|
|
|
data := struct {
|
|
|
|
Connectors []connectorInfo
|
2019-09-27 14:04:43 +00:00
|
|
|
ReqPath string
|
|
|
|
}{connectors, r.URL.Path}
|
2016-12-12 22:54:01 +00:00
|
|
|
return renderTemplate(w, t.loginTmpl, data)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:04:43 +00:00
|
|
|
func (t *templates) password(r *http.Request, w http.ResponseWriter, postURL, lastUsername, usernamePrompt string, lastWasInvalid, showBacklink bool, reqPath string) error {
|
2016-07-25 20:00:28 +00:00
|
|
|
data := struct {
|
2017-11-07 09:28:21 +00:00
|
|
|
PostURL string
|
2017-11-09 15:20:20 +00:00
|
|
|
BackLink bool
|
2017-11-07 09:28:21 +00:00
|
|
|
Username string
|
|
|
|
UsernamePrompt string
|
|
|
|
Invalid bool
|
2019-09-27 14:04:43 +00:00
|
|
|
ReqPath string
|
|
|
|
}{postURL, showBacklink, lastUsername, usernamePrompt, lastWasInvalid, r.URL.Path}
|
2016-12-12 22:54:01 +00:00
|
|
|
return renderTemplate(w, t.passwordTmpl, data)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:04:43 +00:00
|
|
|
func (t *templates) approval(r *http.Request, w http.ResponseWriter, authReqID, username, clientName string, scopes []string, reqPath string) error {
|
2016-08-25 20:10:19 +00:00
|
|
|
accesses := []string{}
|
|
|
|
for _, scope := range scopes {
|
|
|
|
access, ok := scopeDescriptions[scope]
|
|
|
|
if ok {
|
|
|
|
accesses = append(accesses, access)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(accesses)
|
2016-07-25 20:00:28 +00:00
|
|
|
data := struct {
|
2016-10-27 17:20:30 +00:00
|
|
|
User string
|
|
|
|
Client string
|
|
|
|
AuthReqID string
|
|
|
|
Scopes []string
|
2019-09-27 14:04:43 +00:00
|
|
|
ReqPath string
|
|
|
|
}{username, clientName, authReqID, accesses, r.URL.Path}
|
2016-12-12 22:54:01 +00:00
|
|
|
return renderTemplate(w, t.approvalTmpl, data)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:04:43 +00:00
|
|
|
func (t *templates) oob(r *http.Request, w http.ResponseWriter, code string, reqPath string) error {
|
2016-10-19 19:45:17 +00:00
|
|
|
data := struct {
|
2019-09-27 14:04:43 +00:00
|
|
|
Code string
|
|
|
|
ReqPath string
|
|
|
|
}{code, r.URL.Path}
|
2016-12-12 22:54:01 +00:00
|
|
|
return renderTemplate(w, t.oobTmpl, data)
|
2016-10-19 19:45:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:04:43 +00:00
|
|
|
func (t *templates) err(r *http.Request, w http.ResponseWriter, errCode int, errMsg string) error {
|
2017-12-01 05:23:45 +00:00
|
|
|
w.WriteHeader(errCode)
|
2016-12-14 22:17:59 +00:00
|
|
|
data := struct {
|
|
|
|
ErrType string
|
|
|
|
ErrMsg string
|
2019-09-27 14:04:43 +00:00
|
|
|
ReqPath string
|
|
|
|
}{http.StatusText(errCode), errMsg, r.URL.Path}
|
2017-12-01 05:23:45 +00:00
|
|
|
if err := t.errorTmpl.Execute(w, data); err != nil {
|
|
|
|
return fmt.Errorf("Error rendering template %s: %s", t.errorTmpl.Name(), err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-12-14 22:17:59 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:23:16 +00:00
|
|
|
// small io.Writer utility to determine if executing the template wrote to the underlying response writer.
|
2016-08-25 20:10:19 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2016-12-12 22:54:01 +00:00
|
|
|
func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data interface{}) error {
|
2016-08-25 20:10:19 +00:00
|
|
|
wr := &writeRecorder{w: w}
|
|
|
|
if err := tmpl.Execute(wr, data); err != nil {
|
|
|
|
if !wr.wrote {
|
|
|
|
// TODO(ericchiang): replace with better internal server error.
|
|
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
|
|
}
|
2016-12-12 22:54:01 +00:00
|
|
|
return fmt.Errorf("Error rendering template %s: %s", tmpl.Name(), err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-12-12 22:54:01 +00:00
|
|
|
return nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|