WIP: login form shows

This commit is contained in:
Daniel 2022-09-30 10:08:42 -04:00
parent efd920cb04
commit d955a15831
2 changed files with 76 additions and 1 deletions

View file

@ -1,6 +1,8 @@
package server package server
import ( import (
"crypto/rand"
"encoding/hex"
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
@ -26,11 +28,13 @@ type LocalProvider struct {
AuthProviderBase AuthProviderBase
} }
var HomeAssistant = "homeassistant"
func hassProvider() *LocalProvider { func hassProvider() *LocalProvider {
return &LocalProvider{ return &LocalProvider{
AuthProviderBase: AuthProviderBase{ AuthProviderBase: AuthProviderBase{
Name: "Home Assistant Local", 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) logRequest(http.StatusOK, r)
w.Header()["Content-Type"] = []string{"application/json"}
_, err = w.Write(rjs) _, err = w.Write(rjs)
if err != nil { if err != nil {
panic(err) panic(err)
@ -75,6 +80,45 @@ type flowRequest struct {
RedirectURI string `json:"redirect_uri"` 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) { func (s *Server) loginFlowHandler(w http.ResponseWriter, r *http.Request) {
var flowReq flowRequest var flowReq flowRequest
err := json.NewDecoder(r.Body).Decode(&flowReq) err := json.NewDecoder(r.Body).Decode(&flowReq)
@ -84,4 +128,34 @@ func (s *Server) loginFlowHandler(w http.ResponseWriter, r *http.Request) {
return 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)
}
} }

View file

@ -63,6 +63,7 @@ func New(cfg *config.Config) (s *Server, err error) {
mux.Handle("/", logHandler(http.FileServer(http.FS(s.rootFS)))) mux.Handle("/", logHandler(http.FileServer(http.FS(s.rootFS))))
mux.HandleFunc("/auth/authorize", s.authorizeHandler) mux.HandleFunc("/auth/authorize", s.authorizeHandler)
mux.HandleFunc("/auth/providers", s.providersHandler) mux.HandleFunc("/auth/providers", s.providersHandler)
mux.HandleFunc("/auth/login_flow", s.loginFlowHandler)
return s, nil return s, nil
} }