Refactor provider authorization (#24264)

This commit is contained in:
andig 2025-10-09 09:36:10 +02:00 • committed by GitHub
parent e48927af32
commit 21682e7619
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 26 additions and 9 deletions

View file

@ -24,6 +24,8 @@ type OAuth struct {
log *util.Logger
oc *oauth2.Config
token *oauth2.Token
name string
devices []string
subject string
cv string
ctx context.Context
@ -73,7 +75,7 @@ func init() {
func NewOauthFromConfig(ctx context.Context, other map[string]any) (oauth2.TokenSource, error) {
var cc struct {
Name string
Name, Device string
oauth2.Config `mapstructure:",squash"`
}
@ -81,12 +83,12 @@ func NewOauthFromConfig(ctx context.Context, other map[string]any) (oauth2.Token
return nil, err
}
return NewOauth(ctx, cc.Name, &cc.Config)
return NewOauth(ctx, cc.Name, cc.Device, &cc.Config)
}
var _ api.AuthProvider = (*OAuth)(nil)
func NewOauth(ctx context.Context, name string, oc *oauth2.Config, opts ...oauthOption) (oauth2.TokenSource, error) {
func NewOauth(ctx context.Context, name, device string, oc *oauth2.Config, opts ...oauthOption) (oauth2.TokenSource, error) {
if name == "" {
return nil, errors.New("instance name must not be empty")
}
@ -97,10 +99,13 @@ func NewOauth(ctx context.Context, name string, oc *oauth2.Config, opts ...oauth
// hash oauth2 config
h := sha256.Sum256(fmt.Append(nil, oc))
hash := hex.EncodeToString(h[:])[:8]
subject := name + " (" + hash + ")"
subject := oc.ClientID + "-" + hash
// reuse instance
if instance := getInstance(subject); instance != nil {
if device != "" {
instance.devices = append(instance.devices, device)
}
return instance, nil
}
@ -111,10 +116,15 @@ func NewOauth(ctx context.Context, name string, oc *oauth2.Config, opts ...oauth
}
o := &OAuth{
subject: subject,
oc: oc,
log: log,
ctx: ctx,
subject: subject,
name: name,
}
if device != "" {
o.devices = append(o.devices, device)
}
for _, opt := range opts {
@ -287,7 +297,10 @@ func (o *OAuth) Logout() error {
// DisplayName implements api.AuthProvider.
func (o *OAuth) DisplayName() string {
return o.subject
if len(o.devices) > 0 {
return fmt.Sprintf("%s (%s)", o.name, strings.Join(o.devices, ", "))
}
return o.name
}
// Authenticated implements api.AuthProvider.

View file

@ -34,6 +34,7 @@ func init() {
func NewViessmannFromConfig(ctx context.Context, other map[string]any) (oauth2.TokenSource, error) {
var cc struct {
ClientID string
Gateway string
}
if err := util.DecodeOther(other, &cc); err != nil {
@ -43,5 +44,5 @@ func NewViessmannFromConfig(ctx context.Context, other map[string]any) (oauth2.T
log := util.NewLogger("viessmann").Redact(cc.ClientID)
ctx = context.WithValue(ctx, oauth2.HTTPClient, request.NewClient(log))
return NewOauth(ctx, "Viessmann", oauth2Config(cc.ClientID))
return NewOauth(ctx, "Viessmann", cc.Gateway, oauth2Config(cc.ClientID))
}

View file

@ -150,6 +150,7 @@ render: |
auth:
source: viessmann
clientid: {{ .clientid }}
gateway: {{ .gateway_serial }}
jq: '.data.properties.active.value | if . == false then 2 elif . == true then 3 else . end'
# false -> oneTimeCharge is disabled -> normal mode -> 2
# true -> oneTimeCharge is enabled -> boost mode -> 3
@ -170,6 +171,7 @@ render: |
auth:
source: viessmann
clientid: {{ .clientid }}
gateway: {{ .gateway_serial }}
body: >
{ }
- case: 3 # boost
@ -182,6 +184,7 @@ render: |
auth:
source: viessmann
clientid: {{ .clientid }}
gateway: {{ .gateway_serial }}
body: >
{ }
- case: 1 # dimm

View file

@ -54,7 +54,7 @@ func NewCardataFromConfig(ctx context.Context, other map[string]interface{}) (ap
log := util.NewLogger("cardata").Redact(cc.ClientID, cc.VIN)
ts, err := auth.NewOauth(context.Background(), "BMW/Mini", &oc,
ts, err := auth.NewOauth(context.Background(), "BMW/Mini", cc.embed.GetTitle(), &oc,
auth.WithOauthDeviceFlowOption(),
auth.WithTokenRetrieverOption(func(data string, res *oauth2.Token) error {
var token cardata.Token

View file

@ -53,7 +53,7 @@ func NewVolvoConnectedFromConfig(ctx context.Context, other map[string]interface
log := util.NewLogger("volvo-connected").Redact(cc.VIN, cc.Credentials.ID, cc.Credentials.Secret, cc.VccApiKey)
oc := connected.Oauth2Config(cc.Credentials.ID, cc.Credentials.Secret, cc.RedirectUri)
ts, err := auth.NewOauth(ctx, "Volvo", oc)
ts, err := auth.NewOauth(ctx, "Volvo", cc.embed.GetTitle(), oc)
if err != nil {
return nil, err
}