blasphem/pkg/auth/provider/hass/provider.go

129 lines
2.7 KiB
Go
Raw Permalink Normal View History

2022-11-12 15:56:17 -05:00
package hass
2022-10-25 00:16:29 -04:00
import (
"encoding/base64"
2022-10-26 19:43:51 -04:00
"github.com/rs/zerolog/log"
2022-10-25 00:16:29 -04:00
"golang.org/x/crypto/bcrypt"
2022-11-12 15:56:17 -05:00
"dynatron.me/x/blasphem/pkg/auth/provider"
2022-10-25 00:16:29 -04:00
"dynatron.me/x/blasphem/pkg/storage"
)
const (
HAProviderKey = "auth_provider.homeassistant"
)
2022-11-11 18:02:52 -05:00
type HAUser struct {
2022-10-25 00:16:29 -04:00
Password string `json:"password"`
Username string `json:"username"`
2022-11-12 15:56:17 -05:00
provider.AuthProvider `json:"-"`
}
func (hau *HAUser) UserData() interface{} {
return UserData{
Username: hau.Username,
}
}
type UserData struct {
Username string `json:"username"`
2022-10-25 00:16:29 -04:00
}
2022-10-26 18:27:24 -04:00
2022-11-12 15:56:17 -05:00
const HomeAssistant = "homeassistant"
func (h *HAUser) ProviderUserData() interface{} { return h.UserData() }
2022-11-11 18:02:52 -05:00
2022-10-25 00:16:29 -04:00
type HomeAssistantProvider struct {
2022-11-12 15:56:17 -05:00
provider.AuthProviderBase `json:"-"`
2022-11-11 18:02:52 -05:00
Users []HAUser `json:"users"`
2022-10-25 00:16:29 -04:00
}
2022-11-12 16:31:26 -05:00
func NewHAProvider(s storage.Store) (provider.AuthProvider, error) {
2022-10-25 00:16:29 -04:00
hap := &HomeAssistantProvider{
2022-11-12 15:56:17 -05:00
AuthProviderBase: provider.AuthProviderBase{
2022-10-25 00:16:29 -04:00
Name: "Home Assistant Local",
Type: HomeAssistant,
},
}
err := s.Get(HAProviderKey, hap)
if err != nil {
return hap, err
}
2022-11-12 15:56:17 -05:00
for i := range hap.Users {
hap.Users[i].AuthProvider = hap
}
2022-10-25 00:16:29 -04:00
return hap, nil
}
func (hap *HomeAssistantProvider) hashPass(p string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(p), bcrypt.DefaultCost)
}
2022-11-12 15:56:17 -05:00
func (hap *HomeAssistantProvider) ValidateCreds(rm map[string]interface{}) (provider.ProviderUser, bool) {
2022-10-25 00:16:29 -04:00
usernameE, hasU := rm["username"]
passwordE, hasP := rm["password"]
username, unStr := usernameE.(string)
password, paStr := passwordE.(string)
if !hasU || !hasP || !unStr || !paStr || username == "" || password == "" {
2022-11-11 18:02:52 -05:00
return nil, false
2022-10-25 00:16:29 -04:00
}
2022-11-11 18:02:52 -05:00
var found *HAUser
2022-10-25 00:16:29 -04:00
const dummyHash = "$2b$12$CiuFGszHx9eNHxPuQcwBWez4CwDTOcLTX5CbOpV6gef2nYuXkY7BO"
for _, v := range hap.Users { // iterate all to thwart timing attacks
if v.Username == username {
found = &v
}
}
if found == nil { // one more compare to thwart timing attacks
bcrypt.CompareHashAndPassword([]byte("foo"), []byte(dummyHash))
2022-11-11 18:02:52 -05:00
return nil, false
2022-10-25 00:16:29 -04:00
}
var hash []byte
hash, err := base64.StdEncoding.DecodeString(found.Password)
if err != nil {
2022-10-26 19:43:51 -04:00
log.Error().Err(err).Msg("b64 encode fail")
2022-11-11 18:02:52 -05:00
return nil, false
2022-10-25 00:16:29 -04:00
}
err = bcrypt.CompareHashAndPassword(hash, []byte(password))
if err == nil {
2022-11-11 18:02:52 -05:00
return found, true
2022-10-25 00:16:29 -04:00
}
2022-11-11 18:02:52 -05:00
return nil, false
2022-10-25 00:16:29 -04:00
}
2022-11-12 15:56:17 -05:00
func (hap *HomeAssistantProvider) NewCredData() interface{} {
return &UserData{}
}
func (hap *HomeAssistantProvider) FlowSchema() []provider.FlowSchemaItem {
return []provider.FlowSchemaItem{
2022-10-25 00:16:29 -04:00
{
Type: "string",
Name: "username",
Required: true,
},
{
Type: "string",
Name: "password",
Required: true,
},
}
}
2022-11-12 16:31:26 -05:00
func init() {
provider.Register(HomeAssistant, NewHAProvider)
}