blasphem/pkg/server/authorize.go
2022-09-25 22:16:21 -04:00

71 lines
1.3 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)
}
}