2016-07-26 01:30:28 +05:30
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-08-26 01:40:19 +05:30
|
|
|
"fmt"
|
2017-02-02 23:59:57 +05:30
|
|
|
"html/template"
|
2016-08-26 01:40:19 +05:30
|
|
|
"io"
|
2020-10-15 20:20:39 +05:30
|
|
|
"io/fs"
|
2016-07-26 01:30:28 +05:30
|
|
|
"net/http"
|
2020-10-15 20:20:39 +05:30
|
|
|
"net/url"
|
|
|
|
"os"
|
2019-09-27 19:34:43 +05:30
|
|
|
"path"
|
2016-08-26 01:40:19 +05:30
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2016-12-01 03:56:54 +05:30
|
|
|
"strings"
|
2016-08-26 01:40:19 +05:30
|
|
|
)
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-08-26 01:40:19 +05:30
|
|
|
const (
|
2020-01-29 00:44:30 +05:30
|
|
|
tmplApproval = "approval.html"
|
|
|
|
tmplLogin = "login.html"
|
|
|
|
tmplPassword = "password.html"
|
|
|
|
tmplOOB = "oob.html"
|
|
|
|
tmplError = "error.html"
|
|
|
|
tmplDevice = "device.html"
|
|
|
|
tmplDeviceSuccess = "device_success.html"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
var requiredTmpls = []string{
|
|
|
|
tmplApproval,
|
|
|
|
tmplLogin,
|
|
|
|
tmplPassword,
|
|
|
|
tmplOOB,
|
|
|
|
tmplError,
|
|
|
|
tmplDevice,
|
|
|
|
tmplDeviceSuccess,
|
|
|
|
}
|
|
|
|
|
2016-12-01 03:56:54 +05:30
|
|
|
type templates struct {
|
2020-01-29 00:44:30 +05:30
|
|
|
loginTmpl *template.Template
|
|
|
|
approvalTmpl *template.Template
|
|
|
|
passwordTmpl *template.Template
|
|
|
|
oobTmpl *template.Template
|
|
|
|
errorTmpl *template.Template
|
|
|
|
deviceTmpl *template.Template
|
|
|
|
deviceSuccessTmpl *template.Template
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
2016-08-26 01:40:19 +05:30
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
type webConfig struct {
|
|
|
|
dir string
|
|
|
|
webFS fs.FS
|
|
|
|
logoURL string
|
|
|
|
issuer string
|
|
|
|
theme string
|
|
|
|
issuerURL string
|
|
|
|
extra map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadWebConfig returns static assets, theme assets, and templates used by the frontend by
|
|
|
|
// reading the dir specified in the webConfig. If directory is not specified it will
|
|
|
|
// use the file system specified by webFS.
|
|
|
|
//
|
|
|
|
// The directory layout is expected to be:
|
|
|
|
//
|
|
|
|
// ( web directory )
|
|
|
|
// |- static
|
|
|
|
// |- themes
|
|
|
|
// | |- (theme name)
|
|
|
|
// |- templates
|
|
|
|
//
|
|
|
|
func loadWebConfig(c webConfig) (http.Handler, http.Handler, *templates, error) {
|
2020-12-22 12:37:28 +05:30
|
|
|
// fallback to the default theme if the legacy theme name is provided
|
2020-10-15 20:20:39 +05:30
|
|
|
if c.theme == "coreos" || c.theme == "tectonic" {
|
|
|
|
c.theme = ""
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
2020-10-15 20:20:39 +05:30
|
|
|
if c.theme == "" {
|
|
|
|
c.theme = "light"
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
2020-10-15 20:20:39 +05:30
|
|
|
if c.issuer == "" {
|
|
|
|
c.issuer = "dex"
|
2020-01-15 23:34:48 +05:30
|
|
|
}
|
2020-10-15 20:20:39 +05:30
|
|
|
if c.dir != "" {
|
|
|
|
c.webFS = os.DirFS(c.dir)
|
2021-03-04 20:47:05 +05:30
|
|
|
} else if c.webFS == nil {
|
|
|
|
c.webFS = os.DirFS("./web")
|
2020-09-22 08:56:35 +05:30
|
|
|
}
|
2020-10-15 20:20:39 +05:30
|
|
|
if c.logoURL == "" {
|
|
|
|
c.logoURL = "theme/logo.png"
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
staticFiles, err := fs.Sub(c.webFS, "static")
|
2016-12-01 03:56:54 +05:30
|
|
|
if err != nil {
|
2020-10-15 20:20:39 +05:30
|
|
|
return nil, nil, nil, fmt.Errorf("read static dir: %v", err)
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
2020-10-15 20:20:39 +05:30
|
|
|
themeFiles, err := fs.Sub(c.webFS, filepath.Join("themes", c.theme))
|
2020-01-15 23:34:48 +05:30
|
|
|
if err != nil {
|
2020-10-15 20:20:39 +05:30
|
|
|
return nil, nil, nil, fmt.Errorf("read themes dir: %v", err)
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
2020-01-15 23:34:48 +05:30
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
static := http.FileServer(http.FS(staticFiles))
|
|
|
|
theme := http.FileServer(http.FS(themeFiles))
|
2016-08-26 01:40:19 +05:30
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
templates, err := loadTemplates(c, "templates")
|
|
|
|
return static, theme, templates, err
|
|
|
|
}
|
2019-09-27 19:34:43 +05:30
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
// loadTemplates parses the expected templates from the provided directory.
|
|
|
|
func loadTemplates(c webConfig, templatesDir string) (*templates, error) {
|
|
|
|
files, err := fs.ReadDir(c.webFS, templatesDir)
|
2020-01-15 23:34:48 +05:30
|
|
|
if err != nil {
|
2020-10-15 20:20:39 +05:30
|
|
|
return nil, fmt.Errorf("read dir: %v", err)
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
filenames := []string{}
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filenames = append(filenames, filepath.Join(templatesDir, file.Name()))
|
|
|
|
}
|
|
|
|
if len(filenames) == 0 {
|
|
|
|
return nil, fmt.Errorf("no files in template dir %q", templatesDir)
|
2016-12-01 03:56:54 +05:30
|
|
|
}
|
2020-01-15 23:34:48 +05:30
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
issuerURL, err := url.Parse(c.issuerURL)
|
2020-01-15 23:34:48 +05:30
|
|
|
if err != nil {
|
2020-10-15 20:20:39 +05:30
|
|
|
return nil, fmt.Errorf("error parsing issuerURL: %v", err)
|
2016-08-26 01:40:19 +05:30
|
|
|
}
|
2020-01-15 23:34:48 +05:30
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
funcs := map[string]interface{}{
|
|
|
|
"issuer": func() string { return c.issuer },
|
|
|
|
"logo": func() string { return c.logoURL },
|
|
|
|
"url": func(reqPath, assetPath string) string { return relativeURL(issuerURL.Path, reqPath, assetPath) },
|
|
|
|
"lower": strings.ToLower,
|
|
|
|
"extra": func(k string) string { return c.extra[k] },
|
2016-08-26 01:40:19 +05:30
|
|
|
}
|
2020-01-15 23:34:48 +05:30
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
tmpls, err := template.New("").Funcs(funcs).ParseFS(c.webFS, filenames...)
|
2020-01-15 23:34:48 +05:30
|
|
|
if err != nil {
|
2020-10-15 20:20:39 +05:30
|
|
|
return nil, fmt.Errorf("parse files: %v", 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)
|
2020-01-15 23:34:48 +05:30
|
|
|
}
|
2016-08-26 01:40:19 +05:30
|
|
|
return &templates{
|
2020-10-15 20:20:39 +05:30
|
|
|
loginTmpl: tmpls.Lookup(tmplLogin),
|
|
|
|
approvalTmpl: tmpls.Lookup(tmplApproval),
|
|
|
|
passwordTmpl: tmpls.Lookup(tmplPassword),
|
|
|
|
oobTmpl: tmpls.Lookup(tmplOOB),
|
|
|
|
errorTmpl: tmpls.Lookup(tmplError),
|
|
|
|
deviceTmpl: tmpls.Lookup(tmplDevice),
|
|
|
|
deviceSuccessTmpl: tmpls.Lookup(tmplDeviceSuccess),
|
2016-08-26 01:40:19 +05:30
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:20:39 +05:30
|
|
|
// 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 {
|
|
|
|
if u, err := url.ParseRequestURI(assetPath); err == nil && u.Scheme != "" {
|
|
|
|
// assetPath points to the external URL, no changes needed
|
|
|
|
return assetPath
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
_, 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
|
2020-01-15 23:34:48 +05:30
|
|
|
}
|
|
|
|
|
2016-08-26 01:40:19 +05:30
|
|
|
var scopeDescriptions = map[string]string{
|
|
|
|
"offline_access": "Have offline access",
|
|
|
|
"profile": "View basic profile information",
|
2017-12-17 22:38:19 +05:30
|
|
|
"email": "View your email address",
|
2016-08-26 01:40:19 +05:30
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
type connectorInfo struct {
|
2016-08-26 01:40:19 +05:30
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
URL string
|
2019-10-30 05:03:52 +05:30
|
|
|
Type string
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-26 01:40:19 +05:30
|
|
|
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] }
|
|
|
|
|
2020-01-29 00:44:30 +05:30
|
|
|
func (t *templates) device(r *http.Request, w http.ResponseWriter, postURL string, userCode string, lastWasInvalid bool) error {
|
2020-02-04 20:37:18 +05:30
|
|
|
if lastWasInvalid {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
2020-01-29 00:44:30 +05:30
|
|
|
data := struct {
|
|
|
|
PostURL string
|
|
|
|
UserCode string
|
|
|
|
Invalid bool
|
|
|
|
ReqPath string
|
|
|
|
}{postURL, userCode, lastWasInvalid, r.URL.Path}
|
|
|
|
return renderTemplate(w, t.deviceTmpl, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templates) deviceSuccess(r *http.Request, w http.ResponseWriter, clientName string) error {
|
|
|
|
data := struct {
|
|
|
|
ClientName string
|
|
|
|
ReqPath string
|
|
|
|
}{clientName, r.URL.Path}
|
|
|
|
return renderTemplate(w, t.deviceSuccessTmpl, data)
|
|
|
|
}
|
|
|
|
|
2020-10-18 02:32:29 +05:30
|
|
|
func (t *templates) login(r *http.Request, w http.ResponseWriter, connectors []connectorInfo) error {
|
2016-08-26 01:40:19 +05:30
|
|
|
sort.Sort(byName(connectors))
|
2016-07-26 01:30:28 +05:30
|
|
|
data := struct {
|
|
|
|
Connectors []connectorInfo
|
2019-09-27 19:34:43 +05:30
|
|
|
ReqPath string
|
|
|
|
}{connectors, r.URL.Path}
|
2016-12-13 04:24:01 +05:30
|
|
|
return renderTemplate(w, t.loginTmpl, data)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2020-10-18 02:32:29 +05:30
|
|
|
func (t *templates) password(r *http.Request, w http.ResponseWriter, postURL, lastUsername, usernamePrompt string, lastWasInvalid, showBacklink bool) error {
|
2016-07-26 01:30:28 +05:30
|
|
|
data := struct {
|
2017-11-07 14:58:21 +05:30
|
|
|
PostURL string
|
2017-11-09 20:50:20 +05:30
|
|
|
BackLink bool
|
2017-11-07 14:58:21 +05:30
|
|
|
Username string
|
|
|
|
UsernamePrompt string
|
|
|
|
Invalid bool
|
2019-09-27 19:34:43 +05:30
|
|
|
ReqPath string
|
|
|
|
}{postURL, showBacklink, lastUsername, usernamePrompt, lastWasInvalid, r.URL.Path}
|
2016-12-13 04:24:01 +05:30
|
|
|
return renderTemplate(w, t.passwordTmpl, data)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2020-10-18 02:32:29 +05:30
|
|
|
func (t *templates) approval(r *http.Request, w http.ResponseWriter, authReqID, username, clientName string, scopes []string) error {
|
2016-08-26 01:40:19 +05:30
|
|
|
accesses := []string{}
|
|
|
|
for _, scope := range scopes {
|
|
|
|
access, ok := scopeDescriptions[scope]
|
|
|
|
if ok {
|
|
|
|
accesses = append(accesses, access)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(accesses)
|
2016-07-26 01:30:28 +05:30
|
|
|
data := struct {
|
2016-10-27 22:50:30 +05:30
|
|
|
User string
|
|
|
|
Client string
|
|
|
|
AuthReqID string
|
|
|
|
Scopes []string
|
2019-09-27 19:34:43 +05:30
|
|
|
ReqPath string
|
|
|
|
}{username, clientName, authReqID, accesses, r.URL.Path}
|
2016-12-13 04:24:01 +05:30
|
|
|
return renderTemplate(w, t.approvalTmpl, data)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2020-10-18 02:32:29 +05:30
|
|
|
func (t *templates) oob(r *http.Request, w http.ResponseWriter, code string) error {
|
2016-10-20 01:15:17 +05:30
|
|
|
data := struct {
|
2019-09-27 19:34:43 +05:30
|
|
|
Code string
|
|
|
|
ReqPath string
|
|
|
|
}{code, r.URL.Path}
|
2016-12-13 04:24:01 +05:30
|
|
|
return renderTemplate(w, t.oobTmpl, data)
|
2016-10-20 01:15:17 +05:30
|
|
|
}
|
|
|
|
|
2019-09-27 19:34:43 +05:30
|
|
|
func (t *templates) err(r *http.Request, w http.ResponseWriter, errCode int, errMsg string) error {
|
2017-12-01 10:53:45 +05:30
|
|
|
w.WriteHeader(errCode)
|
2016-12-15 03:47:59 +05:30
|
|
|
data := struct {
|
|
|
|
ErrType string
|
|
|
|
ErrMsg string
|
2019-09-27 19:34:43 +05:30
|
|
|
ReqPath string
|
|
|
|
}{http.StatusText(errCode), errMsg, r.URL.Path}
|
2017-12-01 10:53:45 +05:30
|
|
|
if err := t.errorTmpl.Execute(w, data); err != nil {
|
2020-10-18 02:32:29 +05:30
|
|
|
return fmt.Errorf("rendering template %s failed: %s", t.errorTmpl.Name(), err)
|
2017-12-01 10:53:45 +05:30
|
|
|
}
|
|
|
|
return nil
|
2016-12-15 03:47:59 +05:30
|
|
|
}
|
|
|
|
|
2016-12-14 01:53:16 +05:30
|
|
|
// small io.Writer utility to determine if executing the template wrote to the underlying response writer.
|
2016-08-26 01:40:19 +05:30
|
|
|
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-26 01:30:28 +05:30
|
|
|
|
2016-12-13 04:24:01 +05:30
|
|
|
func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data interface{}) error {
|
2016-08-26 01:40:19 +05:30
|
|
|
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)
|
|
|
|
}
|
2020-10-18 02:32:29 +05:30
|
|
|
return fmt.Errorf("rendering template %s failed: %s", tmpl.Name(), err)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2016-12-13 04:24:01 +05:30
|
|
|
return nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|