From d955a158311371fab2c6a71dd15d5fafcd5b9fcd Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Fri, 30 Sep 2022 10:08:42 -0400 Subject: [PATCH] WIP: login form shows --- pkg/server/authorize.go | 76 ++++++++++++++++++++++++++++++++++++++++- pkg/server/server.go | 1 + 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/pkg/server/authorize.go b/pkg/server/authorize.go index 37656a6..f5142bd 100644 --- a/pkg/server/authorize.go +++ b/pkg/server/authorize.go @@ -1,6 +1,8 @@ package server import ( + "crypto/rand" + "encoding/hex" "encoding/json" "io" "net/http" @@ -26,11 +28,13 @@ type LocalProvider struct { AuthProviderBase } +var HomeAssistant = "homeassistant" + func hassProvider() *LocalProvider { return &LocalProvider{ AuthProviderBase: AuthProviderBase{ Name: "Home Assistant Local", - Type: "homeassistant", + Type: HomeAssistant, }, } } @@ -48,6 +52,7 @@ func (s *Server) providersHandler(w http.ResponseWriter, r *http.Request) { logRequest(http.StatusOK, r) + w.Header()["Content-Type"] = []string{"application/json"} _, err = w.Write(rjs) if err != nil { panic(err) @@ -75,6 +80,45 @@ type flowRequest struct { RedirectURI string `json:"redirect_uri"` } +type FlowSchemaItem struct { + Type string `json:"type"` + Name string `json:"name"` + Required bool `json:"required"` +} + +type FlowType string + +const ( + TypeForm FlowType = "form" +) + +type FlowID string +type Step string + +const ( + StepInit Step = "init" +) + +type flowResponse struct { + Type FlowType `json:"type"` + ID FlowID `json:"flow_id"` + Handler []*string `json:"handler"` + StepID Step `json:"step_id"` + Schema []FlowSchemaItem `json:"data_schema"` + Errors []string `json:"errors"` + DescPlace *string `json:"description_placeholders"` + LastStep *string `json:"last_step"` +} + +func GenFlowID() FlowID { + b := make([]byte, 16) + if _, err := rand.Read(b); err != nil { + panic(err) + } + + return FlowID(hex.EncodeToString(b)) +} + func (s *Server) loginFlowHandler(w http.ResponseWriter, r *http.Request) { var flowReq flowRequest err := json.NewDecoder(r.Body).Decode(&flowReq) @@ -84,4 +128,34 @@ func (s *Server) loginFlowHandler(w http.ResponseWriter, r *http.Request) { return } + resp := flowResponse{ + Type: TypeForm, + ID: GenFlowID(), + StepID: StepInit, + Schema: []FlowSchemaItem{ + { + Type: "string", + Name: "username", + Required: true, + }, + { + Type: "string", + Name: "password", + Required: true, + }, + }, + Handler: []*string{ + &HomeAssistant, + nil, + }, + Errors: []string{}, + } + + w.Header()["Content-Type"] = []string{"application/json"} + + respByte, err := json.Marshal(&resp) + _, err = w.Write(respByte) + if err != nil { + panic(err) + } } diff --git a/pkg/server/server.go b/pkg/server/server.go index c38bf9d..f338488 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -63,6 +63,7 @@ func New(cfg *config.Config) (s *Server, err error) { mux.Handle("/", logHandler(http.FileServer(http.FS(s.rootFS)))) mux.HandleFunc("/auth/authorize", s.authorizeHandler) mux.HandleFunc("/auth/providers", s.providersHandler) + mux.HandleFunc("/auth/login_flow", s.loginFlowHandler) return s, nil }