From 1e8b9ef161c7f5ba7e5ff6029ad1a69ad959ea5d Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sun, 15 Oct 2023 20:11:50 +0200 Subject: [PATCH] Set correct login flow --- polyculeconnect/controller/ui/static.go | 13 +++++++++---- polyculeconnect/helpers/helpers.go | 5 ----- polyculeconnect/middlewares/logger.go | 2 +- polyculeconnect/server/server.go | 2 +- polyculeconnect/static/scripts/index.js | 11 +++++++++++ polyculeconnect/templates/footer.html | 1 - polyculeconnect/templates/header.html | 9 +-------- polyculeconnect/templates/index.html | 16 +++------------- polyculeconnect/templates/login.html | 18 ++++++++---------- 9 files changed, 34 insertions(+), 43 deletions(-) create mode 100644 polyculeconnect/static/scripts/index.js diff --git a/polyculeconnect/controller/ui/static.go b/polyculeconnect/controller/ui/static.go index 5ede25c..0678a2d 100644 --- a/polyculeconnect/controller/ui/static.go +++ b/polyculeconnect/controller/ui/static.go @@ -35,8 +35,14 @@ func NewIndexController(l *logrus.Logger, downstream http.Handler) *IndexControl } func (ic IndexController) serveUI(w http.ResponseWriter, r *http.Request) (int, int, error) { + funcs := template.FuncMap{ + "issuer": func() string { return "toto" }, + } + lp := filepath.Join("templates", "index.html") - tmpl, err := template.ParseFiles(lp) + hdrTpl := filepath.Join("templates", "header.html") + footTpl := filepath.Join("templates", "footer.html") + tmpl, err := template.New("index.html").Funcs(funcs).ParseFiles(hdrTpl, footTpl, lp) if err != nil { return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err) } @@ -55,14 +61,13 @@ func (ic IndexController) serveUI(w http.ResponseWriter, r *http.Request) (int, func (ic *IndexController) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.RequestURI != "/" { + ic.l.Debugf("Serving URI %q to dex handler", r.RequestURI) ic.downstreamConstroller.ServeHTTP(w, r) return } - returncode, contentLength, err := ic.serveUI(w, r) + returncode, _, err := ic.serveUI(w, r) if err != nil { ic.l.Errorf("Error serving UI: %s", err.Error()) helpers.HandleResponse(w, r, returncode, nil, ic.l) - } else { - helpers.AddToContext(r, returncode, contentLength) } } diff --git a/polyculeconnect/helpers/helpers.go b/polyculeconnect/helpers/helpers.go index 035ed63..5a6e659 100644 --- a/polyculeconnect/helpers/helpers.go +++ b/polyculeconnect/helpers/helpers.go @@ -29,8 +29,3 @@ func HandleResponse(w http.ResponseWriter, r *http.Request, returncode int, cont contextedReq := r.WithContext(context.WithValue(r.Context(), ResponseInfoKey, ResponseInfo{ReturnCode: returncode, ContentLength: len(content)})) *r = *contextedReq } - -func AddToContext(r *http.Request, returncode, contentLength int) { - contextedReq := r.WithContext(context.WithValue(r.Context(), ResponseInfoKey, ResponseInfo{ReturnCode: returncode, ContentLength: contentLength})) - *r = *contextedReq -} diff --git a/polyculeconnect/middlewares/logger.go b/polyculeconnect/middlewares/logger.go index 6c82ac3..0808010 100644 --- a/polyculeconnect/middlewares/logger.go +++ b/polyculeconnect/middlewares/logger.go @@ -39,7 +39,7 @@ type LoggerMiddleware struct { func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { recorder := loggedResponseWriter{ - contentLength: -1, + contentLength: 0, statusCode: -1, w: w, } diff --git a/polyculeconnect/server/server.go b/polyculeconnect/server/server.go index 2b6ee78..f225e4b 100644 --- a/polyculeconnect/server/server.go +++ b/polyculeconnect/server/server.go @@ -64,7 +64,7 @@ func New(appConf *config.AppConfig, dexSrv *dex_server.Server, logger *logrus.Lo } controllers := map[string]http.Handler{ - ui.StaticRoute: &ui.StaticController{}, + ui.StaticRoute: middlewares.WithLogger(&ui.StaticController{}, logger), "/": middlewares.WithLogger(ui.NewIndexController(logger, dexSrv), logger), } diff --git a/polyculeconnect/static/scripts/index.js b/polyculeconnect/static/scripts/index.js new file mode 100644 index 0000000..d9f7b6b --- /dev/null +++ b/polyculeconnect/static/scripts/index.js @@ -0,0 +1,11 @@ +let connectorForm = document.getElementById("connectorform"); + +connectorForm.addEventListener("submit", (e) => { + e.preventDefault(); + + let nextURL = new URL(window.location.href); + let connectorName = document.getElementById("cname").value; + nextURL.searchParams.append("connector_id", connectorName) + + window.location.href = nextURL; +}); diff --git a/polyculeconnect/templates/footer.html b/polyculeconnect/templates/footer.html index cefcdb1..9870926 100644 --- a/polyculeconnect/templates/footer.html +++ b/polyculeconnect/templates/footer.html @@ -1,4 +1,3 @@ - \ No newline at end of file diff --git a/polyculeconnect/templates/header.html b/polyculeconnect/templates/header.html index 7999d2b..7024820 100644 --- a/polyculeconnect/templates/header.html +++ b/polyculeconnect/templates/header.html @@ -8,11 +8,4 @@ - -
-
- -
-
- -
\ No newline at end of file + \ No newline at end of file diff --git a/polyculeconnect/templates/index.html b/polyculeconnect/templates/index.html index 556d339..2c8c9f0 100644 --- a/polyculeconnect/templates/index.html +++ b/polyculeconnect/templates/index.html @@ -1,15 +1,5 @@ - +{{ template "header.html" . }} - +

Polycule Connect home page

- - - -
-
- -
-
- - - \ No newline at end of file +{{ template "footer.html" . }} \ No newline at end of file diff --git a/polyculeconnect/templates/login.html b/polyculeconnect/templates/login.html index 3f905d5..cdb858e 100644 --- a/polyculeconnect/templates/login.html +++ b/polyculeconnect/templates/login.html @@ -1,15 +1,13 @@ {{ template "header.html" . }} -
-

Log in to {{ issuer }}

-
- {{ range $c := .Connectors }} -
- '{{ $c.ID }}' - {{ $c.Name }} -
- {{ end }} -
+ + +
+
+ + + +
{{ template "footer.html" . }} \ No newline at end of file