Made the url used to check the token a config option
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
501775654f
commit
6f5b3d13f7
|
@ -43,6 +43,7 @@ type config struct {
|
||||||
|
|
||||||
Google struct {
|
Google struct {
|
||||||
Username string `yaml:"username" envconfig:"GOOGLE_USERNAME"`
|
Username string `yaml:"username" envconfig:"GOOGLE_USERNAME"`
|
||||||
|
OAuthUrl string `yaml:"oauth_url" envconfig:"GOOGLE_OAUTH_URL"`
|
||||||
Credentials Credentials `yaml:"credentials" envconfig:"GOOGLE_CREDENTIALS"`
|
Credentials Credentials `yaml:"credentials" envconfig:"GOOGLE_CREDENTIALS"`
|
||||||
} `yaml:"google"`
|
} `yaml:"google"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ type Home struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto populate and update the device list
|
// Auto populate and update the device list
|
||||||
func New(username string, credentials config.Credentials) *Home {
|
func New(username string, credentials config.Credentials, oauthUrl string) *Home {
|
||||||
home := &Home{Username: username, Devices: make(map[device.InternalName]device.Basic)}
|
home := &Home{Username: username, Devices: make(map[device.InternalName]device.Basic)}
|
||||||
|
|
||||||
homegraphService, err := homegraph.NewService(context.Background(), option.WithCredentialsJSON(credentials))
|
homegraphService, err := homegraph.NewService(context.Background(), option.WithCredentialsJSON(credentials))
|
||||||
|
@ -27,7 +27,7 @@ func New(username string, credentials config.Credentials) *Home {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
home.Service = google.NewService(home, homegraphService)
|
home.Service = google.NewService(home, homegraphService, oauthUrl)
|
||||||
|
|
||||||
return home
|
return home
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package google
|
||||||
import (
|
import (
|
||||||
"automation/device"
|
"automation/device"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -34,8 +35,8 @@ type syncResponse struct {
|
||||||
type queryResponse struct {
|
type queryResponse struct {
|
||||||
RequestID string `json:"requestId"`
|
RequestID string `json:"requestId"`
|
||||||
Payload struct {
|
Payload struct {
|
||||||
ErrorCode string `json:"errorCode,omitempty"`
|
ErrorCode string `json:"errorCode,omitempty"`
|
||||||
DebugString string `json:"debugString,omitempty"`
|
DebugString string `json:"debugString,omitempty"`
|
||||||
Devices map[string]DeviceState `json:"devices"`
|
Devices map[string]DeviceState `json:"devices"`
|
||||||
} `json:"payload"`
|
} `json:"payload"`
|
||||||
}
|
}
|
||||||
|
@ -66,7 +67,7 @@ func (s *Service) getUser(authorization string) (string, int) {
|
||||||
return cached.Value(), http.StatusOK
|
return cached.Value(), http.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", "https://login.huizinga.dev/api/oidc/userinfo", nil)
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/userinfo", s.oauthUrl), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to make request to to login server")
|
log.Println("Failed to make request to to login server")
|
||||||
return "", http.StatusInternalServerError
|
return "", http.StatusInternalServerError
|
||||||
|
@ -219,7 +220,7 @@ func (s *Service) FullfillmentHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
for errCode, details := range response.FailedDevices {
|
for errCode, details := range response.FailedDevices {
|
||||||
c := executeRespPayload{
|
c := executeRespPayload{
|
||||||
Status: StatusError,
|
Status: StatusError,
|
||||||
ErrorCode: errCode,
|
ErrorCode: errCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,13 +32,16 @@ type Provider interface {
|
||||||
type Service struct {
|
type Service struct {
|
||||||
provider Provider
|
provider Provider
|
||||||
deviceService *homegraph.DevicesService
|
deviceService *homegraph.DevicesService
|
||||||
cache *ttlcache.Cache[string, string]
|
oauthUrl string
|
||||||
|
|
||||||
|
cache *ttlcache.Cache[string, string]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(provider Provider, service *homegraph.Service) *Service {
|
func NewService(provider Provider, service *homegraph.Service, oauthUrl string) *Service {
|
||||||
s := Service{
|
s := Service{
|
||||||
provider: provider,
|
provider: provider,
|
||||||
deviceService: homegraph.NewDevicesService(service),
|
deviceService: homegraph.NewDevicesService(service),
|
||||||
|
oauthUrl: oauthUrl,
|
||||||
cache: ttlcache.New(
|
cache: ttlcache.New(
|
||||||
ttlcache.WithTTL[string, string](30 * time.Minute),
|
ttlcache.WithTTL[string, string](30 * time.Minute),
|
||||||
),
|
),
|
||||||
|
|
2
main.go
2
main.go
|
@ -29,7 +29,7 @@ func main() {
|
||||||
notify := ntfy.New(cfg.Ntfy.Topic)
|
notify := ntfy.New(cfg.Ntfy.Topic)
|
||||||
hue := hue.New(cfg.Hue.IP, cfg.Hue.Token)
|
hue := hue.New(cfg.Hue.IP, cfg.Hue.Token)
|
||||||
|
|
||||||
home := home.New(cfg.Google.Username, cfg.Google.Credentials)
|
home := home.New(cfg.Google.Username, cfg.Google.Credentials, cfg.Google.OAuthUrl)
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/assistant", home.Service.FullfillmentHandler)
|
r.HandleFunc("/assistant", home.Service.FullfillmentHandler)
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user