87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type AuthProvider interface {
|
|
ProviderName() string
|
|
ProviderID() *string
|
|
ProviderType() string
|
|
}
|
|
|
|
type AuthProviderBase struct {
|
|
Name string `json:"name"`
|
|
ID *string `json:"id"`
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
func (bp *AuthProviderBase) ProviderName() string { return bp.Name }
|
|
func (bp *AuthProviderBase) ProviderID() *string { return bp.ID }
|
|
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)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
}
|