2020-03-25 07:58:58 +01:00
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
2023-05-11 09:24:44 +02:00
|
|
|
"bytes"
|
2022-04-04 09:51:35 +02:00
|
|
|
"embed"
|
2022-02-14 17:22:30 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-05-11 09:24:44 +02:00
|
|
|
"html/template"
|
2022-04-04 09:51:35 +02:00
|
|
|
"io/fs"
|
2020-05-13 14:41:43 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-05-13 14:06:44 +02:00
|
|
|
"strings"
|
2020-06-22 13:17:29 +02:00
|
|
|
"time"
|
2020-05-13 14:41:43 +02:00
|
|
|
|
2022-04-26 12:13:16 +02:00
|
|
|
"github.com/gorilla/mux"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/oidc/v2/pkg/op"
|
2022-02-14 17:22:30 +01:00
|
|
|
|
2022-05-13 14:06:44 +02:00
|
|
|
"github.com/zitadel/zitadel/cmd/build"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
http_util "github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/http/middleware"
|
2020-03-25 07:58:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-05-11 09:24:44 +02:00
|
|
|
ShortCache middleware.CacheConfig
|
|
|
|
LongCache middleware.CacheConfig
|
|
|
|
InstanceManagementURL string
|
2020-05-13 14:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type spaHandler struct {
|
|
|
|
fileSystem http.FileSystem
|
|
|
|
}
|
|
|
|
|
2022-04-04 09:51:35 +02:00
|
|
|
var (
|
|
|
|
//go:embed static/*
|
|
|
|
static embed.FS
|
|
|
|
)
|
|
|
|
|
2020-06-09 07:38:44 +02:00
|
|
|
const (
|
2022-04-04 09:51:35 +02:00
|
|
|
envRequestPath = "/assets/environment.json"
|
|
|
|
HandlerPrefix = "/ui/console"
|
2020-06-24 14:26:27 +02:00
|
|
|
)
|
2020-06-22 13:17:29 +02:00
|
|
|
|
2020-06-24 14:26:27 +02:00
|
|
|
var (
|
2020-11-20 08:47:28 +01:00
|
|
|
shortCacheFiles = []string{
|
|
|
|
"/",
|
2020-06-24 14:26:27 +02:00
|
|
|
"/index.html",
|
|
|
|
"/manifest.webmanifest",
|
|
|
|
"/ngsw.json",
|
|
|
|
"/ngsw-worker.js",
|
|
|
|
"/safety-worker.js",
|
|
|
|
"/worker-basic.min.js",
|
|
|
|
}
|
2020-06-09 07:38:44 +02:00
|
|
|
)
|
|
|
|
|
2023-01-25 09:49:41 +01:00
|
|
|
func LoginHintLink(origin, username string) string {
|
|
|
|
return origin + HandlerPrefix + "?login_hint=" + username
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:41:43 +02:00
|
|
|
func (i *spaHandler) Open(name string) (http.File, error) {
|
|
|
|
ret, err := i.fileSystem.Open(name)
|
|
|
|
if !os.IsNotExist(err) || path.Ext(name) != "" {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2022-05-13 14:06:44 +02:00
|
|
|
f, err := i.fileSystem.Open("/index.html")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &file{File: f}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-31 13:03:23 +00:00
|
|
|
// file wraps the http.File and fs.FileInfo interfaces
|
|
|
|
// to return the build.Date() as ModTime() of the file
|
2022-05-13 14:06:44 +02:00
|
|
|
type file struct {
|
|
|
|
http.File
|
|
|
|
fs.FileInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) ModTime() time.Time {
|
|
|
|
return build.Date()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Stat() (_ fs.FileInfo, err error) {
|
|
|
|
f.FileInfo, err = f.File.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
2020-03-25 07:58:58 +01:00
|
|
|
}
|
|
|
|
|
2023-05-15 08:51:02 +02:00
|
|
|
func Start(config Config, externalSecure bool, issuer op.IssuerFromRequest, callDurationInterceptor, instanceHandler func(http.Handler) http.Handler, limitingAccessInterceptor *middleware.AccessInterceptor, customerPortal string) (http.Handler, error) {
|
2022-04-04 09:51:35 +02:00
|
|
|
fSys, err := fs.Sub(static, "static")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-09 07:38:44 +02:00
|
|
|
}
|
2022-02-14 17:22:30 +01:00
|
|
|
cache := assetsCacheInterceptorIgnoreManifest(
|
|
|
|
config.ShortCache.MaxAge,
|
|
|
|
config.ShortCache.SharedMaxAge,
|
|
|
|
config.LongCache.MaxAge,
|
|
|
|
config.LongCache.SharedMaxAge,
|
2020-06-24 14:26:27 +02:00
|
|
|
)
|
2022-04-25 10:01:17 +02:00
|
|
|
security := middleware.SecurityHeaders(csp(), nil)
|
2022-02-14 17:22:30 +01:00
|
|
|
|
2022-04-26 12:13:16 +02:00
|
|
|
handler := mux.NewRouter()
|
2022-08-17 08:07:41 +02:00
|
|
|
|
2023-05-15 08:51:02 +02:00
|
|
|
handler.Use(callDurationInterceptor, instanceHandler, security, limitingAccessInterceptor.WithoutLimiting().Handle)
|
2022-08-17 08:07:41 +02:00
|
|
|
handler.Handle(envRequestPath, middleware.TelemetryHandler()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-04-25 10:01:17 +02:00
|
|
|
url := http_util.BuildOrigin(r.Host, externalSecure)
|
2023-05-15 08:51:02 +02:00
|
|
|
ctx := r.Context()
|
|
|
|
instance := authz.GetInstance(ctx)
|
2023-05-11 09:24:44 +02:00
|
|
|
instanceMgmtURL, err := templateInstanceManagementURL(config.InstanceManagementURL, instance)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("unable to template instance management url for console: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-05-17 11:41:54 +02:00
|
|
|
exhausted := limitingAccessInterceptor.Limit(ctx)
|
|
|
|
environmentJSON, err := createEnvironmentJSON(url, issuer(r), instance.ConsoleClientID(), customerPortal, instanceMgmtURL, exhausted)
|
2022-03-29 11:53:19 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("unable to marshal env for console: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-05-17 11:41:54 +02:00
|
|
|
if exhausted {
|
2023-05-15 08:51:02 +02:00
|
|
|
limitingAccessInterceptor.SetExhaustedCookie(w, r)
|
|
|
|
} else {
|
2023-05-19 07:12:31 +02:00
|
|
|
limitingAccessInterceptor.DeleteExhaustedCookie(w)
|
2023-05-15 08:51:02 +02:00
|
|
|
}
|
2022-03-29 11:53:19 +02:00
|
|
|
_, err = w.Write(environmentJSON)
|
2022-02-14 17:22:30 +01:00
|
|
|
logging.OnError(err).Error("error serving environment.json")
|
2022-08-17 08:07:41 +02:00
|
|
|
})))
|
2022-05-04 17:09:49 +02:00
|
|
|
handler.SkipClean(true).PathPrefix("").Handler(cache(http.FileServer(&spaHandler{http.FS(fSys)})))
|
2022-02-14 17:22:30 +01:00
|
|
|
return handler, nil
|
2020-03-25 07:58:58 +01:00
|
|
|
}
|
2020-06-22 13:17:29 +02:00
|
|
|
|
2023-05-11 09:24:44 +02:00
|
|
|
func templateInstanceManagementURL(templateableCookieValue string, instance authz.Instance) (string, error) {
|
|
|
|
cookieValueTemplate, err := template.New("cookievalue").Parse(templateableCookieValue)
|
|
|
|
if err != nil {
|
|
|
|
return templateableCookieValue, err
|
|
|
|
}
|
|
|
|
cookieValue := new(bytes.Buffer)
|
|
|
|
if err = cookieValueTemplate.Execute(cookieValue, instance); err != nil {
|
|
|
|
return templateableCookieValue, err
|
|
|
|
}
|
|
|
|
return cookieValue.String(), nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:01:17 +02:00
|
|
|
func csp() *middleware.CSP {
|
2020-06-22 13:17:29 +02:00
|
|
|
csp := middleware.DefaultSCP
|
2022-02-03 07:21:00 +01:00
|
|
|
csp.StyleSrc = csp.StyleSrc.AddInline()
|
2020-06-22 13:17:29 +02:00
|
|
|
csp.ScriptSrc = csp.ScriptSrc.AddEval()
|
2022-04-25 10:01:17 +02:00
|
|
|
csp.ConnectSrc = csp.ConnectSrc.AddOwnHost()
|
|
|
|
csp.ImgSrc = csp.ImgSrc.AddOwnHost().AddScheme("blob")
|
2020-06-22 13:17:29 +02:00
|
|
|
return &csp
|
|
|
|
}
|
|
|
|
|
2023-05-17 11:41:54 +02:00
|
|
|
func createEnvironmentJSON(api, issuer, clientID, customerPortal, instanceMgmtUrl string, exhausted bool) ([]byte, error) {
|
2022-02-14 17:22:30 +01:00
|
|
|
environment := struct {
|
2023-05-11 09:24:44 +02:00
|
|
|
API string `json:"api,omitempty"`
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
ClientID string `json:"clientid,omitempty"`
|
|
|
|
CustomerPortal string `json:"customer_portal,omitempty"`
|
|
|
|
InstanceManagementURL string `json:"instance_management_url,omitempty"`
|
2023-05-17 11:41:54 +02:00
|
|
|
Exhausted bool `json:"exhausted,omitempty"`
|
2022-02-14 17:22:30 +01:00
|
|
|
}{
|
2023-05-11 09:24:44 +02:00
|
|
|
API: api,
|
|
|
|
Issuer: issuer,
|
|
|
|
ClientID: clientID,
|
|
|
|
CustomerPortal: customerPortal,
|
|
|
|
InstanceManagementURL: instanceMgmtUrl,
|
2023-05-17 11:41:54 +02:00
|
|
|
Exhausted: exhausted,
|
2022-02-14 17:22:30 +01:00
|
|
|
}
|
|
|
|
return json.Marshal(environment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assetsCacheInterceptorIgnoreManifest(shortMaxAge, shortSharedMaxAge, longMaxAge, longSharedMaxAge time.Duration) func(http.Handler) http.Handler {
|
2020-06-22 13:17:29 +02:00
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-11-20 08:47:28 +01:00
|
|
|
for _, file := range shortCacheFiles {
|
2022-05-13 14:06:44 +02:00
|
|
|
if r.URL.Path == file || isIndexOrSubPath(r.URL.Path) {
|
2022-08-16 07:04:36 +02:00
|
|
|
middleware.AssetsCacheInterceptor(shortMaxAge, shortSharedMaxAge).Handler(handler).ServeHTTP(w, r)
|
2020-06-24 14:26:27 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-22 13:17:29 +02:00
|
|
|
}
|
2022-08-16 07:04:36 +02:00
|
|
|
middleware.AssetsCacheInterceptor(longMaxAge, longSharedMaxAge).Handler(handler).ServeHTTP(w, r)
|
2020-06-22 13:17:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 14:06:44 +02:00
|
|
|
|
|
|
|
func isIndexOrSubPath(path string) bool {
|
|
|
|
//files will have an extension
|
|
|
|
return !strings.Contains(path, ".")
|
|
|
|
}
|