2022-09-25 22:16:21 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthProvider interface {
|
|
|
|
ProviderName() string
|
|
|
|
ProviderID() *string
|
|
|
|
ProviderType() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthProviderBase struct {
|
2022-09-26 15:00:21 -04:00
|
|
|
Name string `json:"name"`
|
|
|
|
ID *string `json:"id"`
|
|
|
|
Type string `json:"type"`
|
2022-09-25 22:16:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *AuthProviderBase) ProviderName() string { return bp.Name }
|
2022-09-26 15:00:21 -04:00
|
|
|
func (bp *AuthProviderBase) ProviderID() *string { return bp.ID }
|
2022-09-25 22:16:21 -04:00
|
|
|
func (bp *AuthProviderBase) ProviderType() string { return bp.Type }
|
|
|
|
|
|
|
|
type LocalProvider struct {
|
|
|
|
AuthProviderBase
|
|
|
|
}
|
|
|
|
|
|
|
|
func hassProvider() *LocalProvider {
|
|
|
|
return &LocalProvider{
|
|
|
|
AuthProviderBase: AuthProviderBase{
|
|
|
|
Name: "Home Assistant Local",
|
|
|
|
Type: "homeassistant",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make this configurable
|
|
|
|
func (s *Server) providersHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
providers := []AuthProvider{
|
|
|
|
hassProvider(),
|
|
|
|
}
|
|
|
|
|
|
|
|
rjs, err := json.Marshal(providers)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logRequest(http.StatusOK, r)
|
|
|
|
|
|
|
|
_, err = w.Write(rjs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) authorizeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
authPage, err := s.rootFS.Open("authorize.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer authPage.Close()
|
|
|
|
|
|
|
|
logRequest(http.StatusOK, r)
|
|
|
|
|
|
|
|
_, err = io.Copy(w, authPage)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 15:00:21 -04:00
|
|
|
|
|
|
|
type flowRequest struct {
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
Handler []*string `json:"handler"`
|
|
|
|
RedirectURI string `json:"redirect_uri"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) loginFlowHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var flowReq flowRequest
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&flowReq)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|