Set correct login flow
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Melora Hugues 2023-10-15 20:11:50 +02:00
parent 20fde2335e
commit 1e8b9ef161
9 changed files with 34 additions and 43 deletions

View file

@ -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) { 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") 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 { if err != nil {
return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err) 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) { func (ic *IndexController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.RequestURI != "/" { if r.RequestURI != "/" {
ic.l.Debugf("Serving URI %q to dex handler", r.RequestURI)
ic.downstreamConstroller.ServeHTTP(w, r) ic.downstreamConstroller.ServeHTTP(w, r)
return return
} }
returncode, contentLength, err := ic.serveUI(w, r) returncode, _, err := ic.serveUI(w, r)
if err != nil { if err != nil {
ic.l.Errorf("Error serving UI: %s", err.Error()) ic.l.Errorf("Error serving UI: %s", err.Error())
helpers.HandleResponse(w, r, returncode, nil, ic.l) helpers.HandleResponse(w, r, returncode, nil, ic.l)
} else {
helpers.AddToContext(r, returncode, contentLength)
} }
} }

View file

@ -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)})) contextedReq := r.WithContext(context.WithValue(r.Context(), ResponseInfoKey, ResponseInfo{ReturnCode: returncode, ContentLength: len(content)}))
*r = *contextedReq *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
}

View file

@ -39,7 +39,7 @@ type LoggerMiddleware struct {
func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (lm *LoggerMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
recorder := loggedResponseWriter{ recorder := loggedResponseWriter{
contentLength: -1, contentLength: 0,
statusCode: -1, statusCode: -1,
w: w, w: w,
} }

View file

@ -64,7 +64,7 @@ func New(appConf *config.AppConfig, dexSrv *dex_server.Server, logger *logrus.Lo
} }
controllers := map[string]http.Handler{ controllers := map[string]http.Handler{
ui.StaticRoute: &ui.StaticController{}, ui.StaticRoute: middlewares.WithLogger(&ui.StaticController{}, logger),
"/": middlewares.WithLogger(ui.NewIndexController(logger, dexSrv), logger), "/": middlewares.WithLogger(ui.NewIndexController(logger, dexSrv), logger),
} }

View file

@ -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;
});

View file

@ -1,4 +1,3 @@
</div>
</body> </body>
</html> </html>

View file

@ -8,11 +8,4 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head> </head>
<body class="theme-body"> <body>
<div class="theme-navbar">
<div class="theme-navbar__logo-wrap">
</div>
</div>
<div class="dex-container">

View file

@ -1,15 +1,5 @@
<html> {{ template "header.html" . }}
<head> <h1>Polycule Connect home page</h1>
</head> {{ template "footer.html" . }}
<body>
<form action="/login" method="post">
<div>
<input type="submit" value="login">
</div>
</form>
</body>
</html>

View file

@ -1,15 +1,13 @@
{{ template "header.html" . }} {{ template "header.html" . }}
<div class="theme-panel"> <script src="/static/scripts/index.js" defer></script>
<h2 class="theme-heading">Log in to {{ issuer }} </h2>
<div> <div>
{{ range $c := .Connectors }} <form action="" id="connectorform">
<div> <label for="cname">Connector name</label>
'{{ $c.ID }}' <input type="text" id="cname" name="connector_id">
<a href="{{ $c.URL }}">{{ $c.Name }} </a> <input type="submit">
</div> </form>
{{ end }}
</div>
</div> </div>
{{ template "footer.html" . }} {{ template "footer.html" . }}