blasphem/pkg/auth/authenticator.go

74 lines
1.4 KiB
Go
Raw Normal View History

2022-09-30 23:54:21 -04:00
package auth
import (
"io"
"net/http"
"github.com/labstack/echo/v4"
"dynatron.me/x/blasphem/pkg/frontend"
)
type Authenticator struct {
Flows FlowStore
}
func (a *Authenticator) InitAuth() {
a.Flows = make(FlowStore)
}
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
}
var HomeAssistant = "homeassistant"
func hassProvider() *LocalProvider {
return &LocalProvider{
AuthProviderBase: AuthProviderBase{
Name: "Home Assistant Local",
Type: HomeAssistant,
},
}
}
// TODO: make this configurable
func (s *Authenticator) ProvidersHandler(c echo.Context) error {
providers := []AuthProvider{
hassProvider(),
}
return c.JSON(http.StatusOK, providers)
}
func (s *Authenticator) AuthorizeHandler(c echo.Context) error {
authContents, err := frontend.RootFS.Open("authorize.html")
if err != nil {
return err
}
defer authContents.Close()
b, err := io.ReadAll(authContents)
if err != nil {
return err
}
return c.HTML(http.StatusOK, string(b))
}