Mercedes: update oauth2 config (#4742)
Some checks failed
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Hassio Addon (push) Has been cancelled

This commit is contained in:
andig 2022-11-02 11:33:48 +01:00 • committed by GitHub
parent a35010edd9
commit cc04229768
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 10 deletions

View file

@ -20,6 +20,7 @@ import (
// https://github.com/TA2k/ioBroker.smart-eq
// https://id.mercedes-benz.com/.well-known/openid-configuration
const OAuthURI = "https://id.mercedes-benz.com"
type Identity struct {

View file

@ -41,7 +41,11 @@ func NewMercedesFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return nil, errors.New("missing credentials")
}
var options []mercedes.IdentityOptions
if cc.VIN == "" {
return nil, errors.New("missing vin")
}
var options []mercedes.IdentityOption
// TODO Load tokens from a persistence storage and use those during startup
// e.g. persistence.Load("key")

View file

@ -15,10 +15,13 @@ import (
"golang.org/x/oauth2"
)
type IdentityOptions func(c *Identity) error
// https://ssoalpha.dvb.corpinter.net/v1/.well-known/openid-configuration
const OAuthURI = "https://ssoalpha.dvb.corpinter.net/v1"
type IdentityOption func(c *Identity) error
// WithToken provides an oauth2.Token to the client for auth.
func WithToken(t *oauth2.Token) IdentityOptions {
func WithToken(t *oauth2.Token) IdentityOption {
return func(v *Identity) error {
v.ReuseTokenSource.Apply(t)
return nil
@ -34,8 +37,8 @@ type Identity struct {
}
// TODO SessionSecret from config/persistence
func NewIdentity(log *util.Logger, id, secret string, options ...IdentityOptions) (*Identity, error) {
provider, err := oidc.NewProvider(context.Background(), "https://id.mercedes-benz.com")
func NewIdentity(log *util.Logger, id, secret string, options ...IdentityOption) (*Identity, error) {
provider, err := oidc.NewProvider(context.Background(), OAuthURI)
if err != nil {
return nil, fmt.Errorf("failed to initialize OIDC provider: %s", err)
}
@ -44,7 +47,11 @@ func NewIdentity(log *util.Logger, id, secret string, options ...IdentityOptions
ClientID: id,
ClientSecret: secret,
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOfflineAccess, "mb:vehicle:mbdata:evstatus"},
Scopes: []string{
oidc.ScopeOpenID,
oidc.ScopeOfflineAccess,
"mb:vehicle:mbdata:evstatus",
},
}
v := &Identity{
@ -61,12 +68,12 @@ func NewIdentity(log *util.Logger, id, secret string, options ...IdentityOptions
v.ReuseTokenSource = ts
for _, o := range options {
if err == nil {
err = o(v)
if err := o(v); err != nil {
return v, err
}
}
return v, err
return v, nil
}
// invalidToken is the callback for the token source when token expires

View file

@ -15,7 +15,6 @@ import (
const ApiURI = "https://oneapp.microservice.smart.com/seqc/v0"
// https://id.mercedes-benz.com/.well-known/openid-configuration
var OAuth2Config = &oauth2.Config{
ClientID: "70d89501-938c-4bec-82d0-6abb550b0825",
RedirectURL: "https://oneapp.microservice.smart.com",