Provider Auth: use base logger if available (#26138)

This commit is contained in:
andig 2025-12-22 10:22:10 +01:00 • committed by GitHub
parent 6ecf041d9a
commit 31bb62148d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 45 additions and 33 deletions

View file

@ -92,7 +92,7 @@ func NewOAuth(ctx context.Context, name, device string, oc *oauth2.Config, opts
return instance, nil return instance, nil
} }
log := util.NewLogger("oauth-" + hash) log := util.ContextLoggerWithDefault(ctx, util.NewLogger("oauth-"+hash))
if client, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); client == nil || !ok { if client, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); client == nil || !ok {
ctx = context.WithValue(ctx, oauth2.HTTPClient, request.NewClient(log)) ctx = context.WithValue(ctx, oauth2.HTTPClient, request.NewClient(log))

View file

@ -1,7 +1,6 @@
package plugin package plugin
import ( import (
"context"
"fmt" "fmt"
"math" "math"
"strconv" "strconv"
@ -35,16 +34,6 @@ func knownErrors(b []byte) error {
} }
} }
func contextLogger(ctx context.Context, log *util.Logger) *util.Logger {
if ctx != nil {
if l, ok := ctx.Value(util.CtxLogger).(*util.Logger); ok {
log = l
}
}
return log
}
// parseFloat rejects NaN and Inf values // parseFloat rejects NaN and Inf values
func parseFloat(payload string) (float64, error) { func parseFloat(payload string) (float64, error) {
f, err := strconv.ParseFloat(payload, 64) f, err := strconv.ParseFloat(payload, 64)

View file

@ -61,7 +61,7 @@ func NewHTTPPluginFromConfig(ctx context.Context, other map[string]any) (Plugin,
return nil, errors.New("missing uri") return nil, errors.New("missing uri")
} }
log := contextLogger(ctx, util.NewLogger("http")) log := util.ContextLoggerWithDefault(ctx, util.NewLogger("http"))
p := NewHTTP( p := NewHTTP(
log, log,
strings.ToUpper(cc.Method), strings.ToUpper(cc.Method),

View file

@ -59,7 +59,7 @@ func NewModbusFromConfig(ctx context.Context, other map[string]any) (Plugin, err
// set non-default connect delay // set non-default connect delay
conn.ConnectDelay(cc.ConnectDelay) conn.ConnectDelay(cc.ConnectDelay)
log := contextLogger(ctx, util.NewLogger("modbus")) log := util.ContextLoggerWithDefault(ctx, util.NewLogger("modbus"))
conn.Logger(log.TRACE) conn.Logger(log.TRACE)
if err := cc.Register.Error(); err != nil { if err := cc.Register.Error(); err != nil {

View file

@ -42,7 +42,7 @@ func NewMqttPluginFromConfig(ctx context.Context, other map[string]any) (Plugin,
return nil, err return nil, err
} }
log := contextLogger(ctx, util.NewLogger("mqtt")) log := util.ContextLoggerWithDefault(ctx, util.NewLogger("mqtt"))
client, err := mqtt.RegisteredClientOrDefault(log, cc.Config) client, err := mqtt.RegisteredClientOrDefault(log, cc.Config)
if err != nil { if err != nil {

View file

@ -41,7 +41,7 @@ func NewWatchDogFromConfig(ctx context.Context, other map[string]any) (Plugin, e
o := &watchdogPlugin{ o := &watchdogPlugin{
ctx: ctx, ctx: ctx,
log: contextLogger(ctx, util.NewLogger("watchdog")), log: util.ContextLoggerWithDefault(ctx, util.NewLogger("watchdog")),
reset: cc.Reset, reset: cc.Reset,
initial: cc.Initial, initial: cc.Initial,
set: cc.Set, set: cc.Set,

View file

@ -10,7 +10,6 @@ import (
"github.com/evcc-io/evcc/plugin/auth" "github.com/evcc-io/evcc/plugin/auth"
"github.com/evcc-io/evcc/server/network" "github.com/evcc-io/evcc/server/network"
"github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
@ -48,9 +47,6 @@ func NewHomeAssistant(uri string) (oauth2.TokenSource, error) {
extUrl := network.Config().ExternalURL() extUrl := network.Config().ExternalURL()
redirectUri := extUrl + network.CallbackPath redirectUri := extUrl + network.CallbackPath
log := util.NewLogger("homeassistant")
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
oc := oauth2.Config{ oc := oauth2.Config{
ClientID: extUrl, ClientID: extUrl,
RedirectURL: redirectUri, RedirectURL: redirectUri,
@ -77,5 +73,8 @@ func NewHomeAssistant(uri string) (oauth2.TokenSource, error) {
host = name host = name
} }
log := util.NewLogger("homeassistant")
ctx := util.WithLogger(context.Background(), log)
return auth.NewOAuth(ctx, "HomeAssistant", host, &oc) return auth.NewOAuth(ctx, "HomeAssistant", host, &oc)
} }

View file

@ -9,3 +9,21 @@ var CtxLogger = struct{}{}
func WithLogger(ctx context.Context, log *Logger) context.Context { func WithLogger(ctx context.Context, log *Logger) context.Context {
return context.WithValue(ctx, CtxLogger, log) return context.WithValue(ctx, CtxLogger, log)
} }
func ContextLogger(ctx context.Context) *Logger {
if ctx != nil {
if l, ok := ctx.Value(CtxLogger).(*Logger); ok {
return l
}
}
return nil
}
func ContextLoggerWithDefault(ctx context.Context, log *Logger) *Logger {
if log := ContextLogger(ctx); log != nil {
return log
}
return log
}

View file

@ -19,7 +19,8 @@ func init() {
return nil, err return nil, err
} }
return NewOAuth(cc.ClientID, "") ctx := util.WithLogger(context.Background(), util.NewLogger("cardata").Redact(cc.ClientID))
return NewOAuth(ctx, cc.ClientID, "")
}) })
} }
@ -40,10 +41,10 @@ func OAuthConfig(clientId string) *oauth2.Config {
} }
} }
func NewOAuth(clientId, title string) (oauth2.TokenSource, error) { func NewOAuth(ctx context.Context, clientId, title string) (oauth2.TokenSource, error) {
oc := OAuthConfig(clientId) oc := OAuthConfig(clientId)
return auth.NewOAuth(context.Background(), "BMW/Mini", title, oc, return auth.NewOAuth(ctx, "BMW/Mini", title, oc,
auth.WithOauthDeviceFlowOption(), auth.WithOauthDeviceFlowOption(),
auth.WithTokenRetrieverOption(func(data string, res *oauth2.Token) error { auth.WithTokenRetrieverOption(func(data string, res *oauth2.Token) error {
var token Token var token Token

View file

@ -53,7 +53,8 @@ func NewCardataFromConfig(ctx context.Context, other map[string]any) (api.Vehicl
log := util.NewLogger("cardata").Redact(cc.ClientID, cc.VIN) log := util.NewLogger("cardata").Redact(cc.ClientID, cc.VIN)
ts, err := cardata.NewOAuth(cc.ClientID, cc.embed.GetTitle()) authCtx := util.WithLogger(context.Background(), log)
ts, err := cardata.NewOAuth(authCtx, cc.ClientID, cc.embed.GetTitle())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,6 +1,7 @@
package vehicle package vehicle
import ( import (
"context"
"time" "time"
"github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/api"
@ -44,13 +45,14 @@ func NewFordConnectQueryFromConfig(other map[string]any) (api.Vehicle, error) {
return nil, err return nil, err
} }
log := util.NewLogger("ford").Redact(cc.Credentials.ID, cc.VIN)
oc := query.OAuth2Config(cc.Credentials.ID, cc.Credentials.Secret, cc.RedirectURI) oc := query.OAuth2Config(cc.Credentials.ID, cc.Credentials.Secret, cc.RedirectURI)
ts, err := query.NewOAuth(oc, cc.embed.GetTitle()) ts, err := query.NewOAuth(util.WithLogger(context.Background(), log), oc, cc.embed.GetTitle())
if err != nil { if err != nil {
return nil, err return nil, err
} }
log := util.NewLogger("ford").Redact(cc.VIN)
api := query.NewAPI(log, ts) api := query.NewAPI(log, ts)
vehicle, err := ensureVehicleEx(cc.VIN, api.Vehicles, func(v query.Vehicle) (string, error) { vehicle, err := ensureVehicleEx(cc.VIN, api.Vehicles, func(v query.Vehicle) (string, error) {

View file

@ -21,9 +21,10 @@ func init() {
return nil, err return nil, err
} }
log := util.NewLogger("ford").Redact(cc.ClientID)
oc := OAuth2Config(cc.ClientID, cc.ClientSecret, cc.RedirectURI) oc := OAuth2Config(cc.ClientID, cc.ClientSecret, cc.RedirectURI)
return NewOAuth(oc, "") return NewOAuth(util.WithLogger(context.Background(), log), oc, "")
}) })
} }
@ -45,6 +46,6 @@ func OAuth2Config(id, secret, redirectUri string) *oauth2.Config {
} }
// NewOAuth creates FordConnect token source // NewOAuth creates FordConnect token source
func NewOAuth(oc *oauth2.Config, title string) (oauth2.TokenSource, error) { func NewOAuth(ctx context.Context, oc *oauth2.Config, title string) (oauth2.TokenSource, error) {
return auth.NewOAuth(context.Background(), "Ford Connect", title, oc) return auth.NewOAuth(ctx, "Ford Connect", title, oc)
} }

View file

@ -52,7 +52,7 @@ func NewVolvoConnectedFromConfig(ctx context.Context, other map[string]any) (api
log := util.NewLogger("volvo-connected").Redact(cc.VIN, cc.Credentials.ID, cc.Credentials.Secret, cc.VccApiKey) log := util.NewLogger("volvo-connected").Redact(cc.VIN, cc.Credentials.ID, cc.Credentials.Secret, cc.VccApiKey)
oc := connected.OAuthConfig(cc.Credentials.ID, cc.Credentials.Secret, cc.RedirectUri) oc := connected.OAuthConfig(cc.Credentials.ID, cc.Credentials.Secret, cc.RedirectUri)
ts, err := connected.NewOAuth(oc, cc.embed.GetTitle()) ts, err := connected.NewOAuth(util.WithLogger(context.Background(), log), oc, cc.embed.GetTitle())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -21,9 +21,10 @@ func init() {
return nil, err return nil, err
} }
log := util.NewLogger("volvo").Redact(cc.ClientID)
oc := OAuthConfig(cc.ClientID, cc.ClientSecret, cc.RedirectUri) oc := OAuthConfig(cc.ClientID, cc.ClientSecret, cc.RedirectUri)
return NewOAuth(oc, "") return NewOAuth(util.WithLogger(context.Background(), log), oc, "")
}) })
} }
@ -46,6 +47,6 @@ func OAuthConfig(id, secret, redirectUri string) *oauth2.Config {
} }
} }
func NewOAuth(oc *oauth2.Config, title string) (oauth2.TokenSource, error) { func NewOAuth(ctx context.Context, oc *oauth2.Config, title string) (oauth2.TokenSource, error) {
return auth.NewOAuth(context.Background(), "Volvo", title, oc) return auth.NewOAuth(ctx, "Volvo", title, oc)
} }