Cardata: fix oauth login and streaming data access (#24138)

This commit is contained in:
andig 2025-10-05 19:05:29 +02:00 • committed by GitHub
parent 42a0e226f4
commit 18176ee61d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 17 additions and 15 deletions

View file

@ -61,14 +61,10 @@ var (
)
func getInstance(subject string) *OAuth {
oauthMu.Lock()
defer oauthMu.Unlock()
return identities[subject]
}
func addInstance(subject string, identity *OAuth) {
oauthMu.Lock()
defer oauthMu.Unlock()
identities[subject] = identity
}
@ -99,6 +95,9 @@ func NewOauth(ctx context.Context, name string, oc *oauth2.Config, opts ...oauth
return nil, errors.New("instance name must not be empty")
}
oauthMu.Lock()
defer oauthMu.Unlock()
// hash oauth2 config
h := sha256.Sum256(fmt.Append(nil, oc))
hash := hex.EncodeToString(h[:])[:8]
@ -185,6 +184,8 @@ func (o *OAuth) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
err = o.updateToken(token)
o.onlineC <- token.Valid()
return token, err
}
@ -197,13 +198,7 @@ func (o *OAuth) updateToken(token *oauth2.Token) error {
store = o.tokenStorer(token)
}
if err := settings.SetJson(o.subject, store); err != nil {
return err
}
o.onlineC <- token.Valid()
return nil
return settings.SetJson(o.subject, store)
}
// updateTokenSource must only be called when lock is held
@ -214,6 +209,8 @@ func (o *OAuth) updateTokenSource(token *oauth2.Token) {
}
o.TokenSource = oauth.RefreshTokenSource(token, o)
o.onlineC <- token.Valid()
}
// HandleCallback implements api.AuthProvider.
@ -285,9 +282,10 @@ func (o *OAuth) Logout() error {
o.mu.Lock()
defer o.mu.Unlock()
o.onlineC <- false
o.TokenSource = oauth.RefreshTokenSource(nil, o)
o.onlineC <- false
return nil
}
@ -298,6 +296,9 @@ func (o *OAuth) DisplayName() string {
// Authenticated implements api.AuthProvider.
func (o *OAuth) Authenticated() bool {
o.mu.Lock()
defer o.mu.Unlock()
token, err := o.Token()
return err == nil && token.Valid()
}

View file

@ -28,7 +28,7 @@ type Handler struct {
// TODO get status from update channel
func (a *Handler) run(paramC chan<- util.Param) {
for range <-a.updateC {
for range a.updateC {
a.mu.Lock()
res := make(map[string]*AuthProvider)

View file

@ -18,6 +18,7 @@ var Config = oauth2.Config{
Endpoint: oauth2.Endpoint{
DeviceAuthURL: "https://customer.bmwgroup.com/gcdm/oauth/device/code",
TokenURL: "https://customer.bmwgroup.com/gcdm/oauth/token",
AuthStyle: oauth2.AuthStyleInParams,
},
}

View file

@ -124,7 +124,7 @@ func (v *Provider) any(key string) (any, error) {
defer v.mu.Unlock()
if a, ok := v.streaming[key]; ok {
return a, nil
return a.Value, nil
}
if v.initial == nil {

View file

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