trustednetworks

This commit is contained in:
Daniel Ponte 2022-11-12 16:31:26 -05:00
parent 90825fa01b
commit 2c997e3866
4 changed files with 106 additions and 13 deletions

View file

@ -11,9 +11,12 @@ import (
"github.com/rs/zerolog/log"
"dynatron.me/x/blasphem/pkg/auth/provider"
"dynatron.me/x/blasphem/pkg/auth/provider/hass"
"dynatron.me/x/blasphem/pkg/frontend"
"dynatron.me/x/blasphem/pkg/storage"
// providers
_ "dynatron.me/x/blasphem/pkg/auth/provider/hass"
_ "dynatron.me/x/blasphem/pkg/auth/provider/trustednets"
)
var (
@ -47,25 +50,26 @@ func (a *Authenticator) InstallRoutes(e *echo.Echo) {
}
func (a *Authenticator) InitAuth(s storage.Store) error {
a.providers = make(map[string]provider.AuthProvider)
for _, pI := range provider.Providers {
nProv, err := pI(s)
if err != nil {
return err
}
a.providers[nProv.ProviderType()] = nProv
}
a.flows = make(FlowStore)
a.sessions.init()
hap, err := hass.NewHAProvider(s)
if err != nil {
return err
}
// XXX: yuck. use init with a registry or something
a.providers = map[string]provider.AuthProvider{
hap.ProviderType(): hap,
}
var err error
a.store, err = a.newAuthStore(s)
if err != nil {
return err
}
return nil
}

View file

@ -40,7 +40,7 @@ type HomeAssistantProvider struct {
Users []HAUser `json:"users"`
}
func NewHAProvider(s storage.Store) (*HomeAssistantProvider, error) {
func NewHAProvider(s storage.Store) (provider.AuthProvider, error) {
hap := &HomeAssistantProvider{
AuthProviderBase: provider.AuthProviderBase{
Name: "Home Assistant Local",
@ -121,3 +121,8 @@ func (hap *HomeAssistantProvider) FlowSchema() []provider.FlowSchemaItem {
},
}
}
func init() {
provider.Register(HomeAssistant, NewHAProvider)
}

View file

@ -1,5 +1,13 @@
package provider
import (
"dynatron.me/x/blasphem/pkg/storage"
)
type Constructor func(storage.Store) (AuthProvider, error)
var Providers = make(map[string]Constructor)
type AuthProvider interface { // TODO: this should include stepping
AuthProviderMetadata
ProviderBase() AuthProviderBase
@ -8,6 +16,10 @@ type AuthProvider interface { // TODO: this should include stepping
ValidateCreds(reqMap map[string]interface{}) (user ProviderUser, success bool)
}
func Register(providerName string, f func(storage.Store) (AuthProvider, error)) {
Providers[providerName] = f
}
type ProviderUser interface {
AuthProviderMetadata
ProviderUserData() interface{}

View file

@ -0,0 +1,72 @@
package trustednets
// TODO: This doesn't work at all
import (
"dynatron.me/x/blasphem/pkg/auth/provider"
"dynatron.me/x/blasphem/pkg/storage"
)
type User struct {
UserID string `json:"user_id"`
provider.AuthProvider `json:"-"`
}
func (hau *User) UserData() interface{} {
return UserData{
UserID: hau.UserID,
}
}
type UserData struct {
UserID string `json:"user_id"`
}
const TrustedNetworks = "trusted_networks"
func (h *User) ProviderUserData() interface{} { return h.UserData() }
type TrustedNetworksProvider struct {
provider.AuthProviderBase `json:"-"`
}
func New(s storage.Store) (provider.AuthProvider, error) {
hap := &TrustedNetworksProvider{
AuthProviderBase: provider.AuthProviderBase{
Name: "Trusted Networks",
Type: TrustedNetworks,
},
}
return hap, nil
}
// TODO: To implement this, ValidateCreds needs to be changed to accept an http.Request, or the echo context.
func (hap *TrustedNetworksProvider) ValidateCreds(rm map[string]interface{}) (provider.ProviderUser, bool) {
return nil, false
}
func (hap *TrustedNetworksProvider) NewCredData() interface{} {
return &UserData{}
}
func (hap *TrustedNetworksProvider) FlowSchema() []provider.FlowSchemaItem {
return []provider.FlowSchemaItem{
{
Type: "string",
Name: "username",
Required: true,
},
{
Type: "string",
Name: "password",
Required: true,
},
}
}
func init() {
provider.Register(TrustedNetworks, New)
}