VAG: refactor identity management (#1897)
This commit is contained in:
parent
a35eff7c7b
commit
f9c2220516
20 changed files with 383 additions and 388 deletions
|
|
@ -2,13 +2,13 @@ package vehicle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/vehicle/audi"
|
||||
"github.com/evcc-io/evcc/vehicle/vw"
|
||||
)
|
||||
|
||||
|
|
@ -47,19 +47,9 @@ func NewAudiFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("audi").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := vw.NewIdentity(log)
|
||||
|
||||
query := url.Values(map[string][]string{
|
||||
"response_type": {"id_token token"},
|
||||
"client_id": {"09b6cbec-cd19-4589-82fd-363dfa8c24da@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"myaudi:///"},
|
||||
"scope": {"openid profile mbb"}, // vin badge birthdate nickname email address phone name picture
|
||||
"prompt": {"login"},
|
||||
"ui_locales": {"de-DE"},
|
||||
})
|
||||
|
||||
ts := vw.NewTokenSource(log, identity, "77869e21-e30a-4a92-b016-48ab7d3db1d8", query, cc.User, cc.Password)
|
||||
err := identity.Login(ts)
|
||||
identity := vw.NewIdentity(log, audi.AuthClientID, audi.AuthParams, cc.User, cc.Password)
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ type API struct {
|
|||
}
|
||||
|
||||
// NewAPI creates a new api client
|
||||
func NewAPI(log *util.Logger, identity oauth2.TokenSource, brand, country string) *API {
|
||||
func NewAPI(log *util.Logger, ts oauth2.TokenSource, brand, country string) *API {
|
||||
v := &API{
|
||||
Helper: request.NewHelper(log),
|
||||
brand: brand,
|
||||
|
|
@ -31,7 +31,7 @@ func NewAPI(log *util.Logger, identity oauth2.TokenSource, brand, country string
|
|||
}
|
||||
|
||||
v.Client.Transport = &oauth2.Transport{
|
||||
Source: identity,
|
||||
Source: ts,
|
||||
Base: v.Client.Transport,
|
||||
}
|
||||
|
||||
|
|
|
|||
21
vehicle/audi/params.go
Normal file
21
vehicle/audi/params.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package audi
|
||||
|
||||
import "net/url"
|
||||
|
||||
const (
|
||||
Brand = "Audi"
|
||||
Country = "DE"
|
||||
|
||||
// Authorization ClientID
|
||||
AuthClientID = "77869e21-e30a-4a92-b016-48ab7d3db1d8"
|
||||
)
|
||||
|
||||
// Authorization parameters
|
||||
var AuthParams = url.Values(map[string][]string{
|
||||
"response_type": {"id_token token"},
|
||||
"client_id": {"09b6cbec-cd19-4589-82fd-363dfa8c24da@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"myaudi:///"},
|
||||
"scope": {"openid profile mbb"}, // vin badge birthdate nickname email address phone name picture
|
||||
"prompt": {"login"},
|
||||
"ui_locales": {"de-DE"},
|
||||
})
|
||||
|
|
@ -2,7 +2,6 @@ package vehicle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -10,7 +9,6 @@ import (
|
|||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/vehicle/skoda"
|
||||
"github.com/evcc-io/evcc/vehicle/vw"
|
||||
)
|
||||
|
||||
// https://github.com/lendy007/skodaconnect
|
||||
|
|
@ -49,22 +47,12 @@ func NewEnyaqFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
log := util.NewLogger("enyaq").Redact(cc.User, cc.Password, cc.VIN)
|
||||
|
||||
if cc.VIN == "" {
|
||||
identity := vw.NewIdentity(log)
|
||||
|
||||
// Skoda native api
|
||||
query := url.Values(map[string][]string{
|
||||
"response_type": {"code id_token"},
|
||||
"redirect_uri": {"skodaconnect://oidc.login/"},
|
||||
"client_id": {"f9a2359a-b776-46d9-bd0c-db1904343117@apps_vw-dilab_com"},
|
||||
"scope": {"openid profile mbb"},
|
||||
})
|
||||
|
||||
ts := skoda.NewTokenSource(log, identity, query, cc.User, cc.Password)
|
||||
if err = identity.Login(ts); err != nil {
|
||||
ts := skoda.NewIdentity(log, skoda.AuthParams, cc.User, cc.Password)
|
||||
if err = ts.Login(); err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := skoda.NewAPI(log, identity)
|
||||
api := skoda.NewAPI(log, ts)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
cc.VIN, err = findVehicle(api.Vehicles())
|
||||
|
|
@ -74,22 +62,12 @@ func NewEnyaqFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
if err == nil {
|
||||
identity := vw.NewIdentity(log)
|
||||
|
||||
// Skoda connect api
|
||||
query := url.Values(map[string][]string{
|
||||
"response_type": {"code id_token"},
|
||||
"redirect_uri": {"skodaconnect://oidc.login/"},
|
||||
"client_id": {"7f045eee-7003-4379-9968-9355ed2adb06@apps_vw-dilab_com"},
|
||||
"scope": {"openid profile mbb"}, // phone address cars email birthdate badge dealers driversLicense
|
||||
})
|
||||
|
||||
ts := skoda.NewTokenSource(log, identity, query, cc.User, cc.Password)
|
||||
if err = identity.Login(ts); err != nil {
|
||||
ts := skoda.NewIdentity(log, skoda.ConnectAuthParams, cc.User, cc.Password)
|
||||
if err = ts.Login(); err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := skoda.NewAPI(log, identity)
|
||||
api := skoda.NewAPI(log, ts)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
v.Provider = skoda.NewProvider(api, strings.ToUpper(cc.VIN), cc.Cache)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package vehicle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -10,7 +9,6 @@ import (
|
|||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/vehicle/id"
|
||||
"github.com/evcc-io/evcc/vehicle/vw"
|
||||
)
|
||||
|
||||
// https://github.com/TA2k/ioBroker.vw-connect
|
||||
|
|
@ -46,22 +44,14 @@ func NewIDFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("id").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := vw.NewIdentity(log)
|
||||
|
||||
query := url.Values(map[string][]string{
|
||||
"response_type": {"code id_token token"},
|
||||
"client_id": {"a24fba63-34b3-4d43-b181-942111e6bda8@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"weconnect://authenticated"},
|
||||
"scope": {"openid profile badge cars dealers vin"},
|
||||
})
|
||||
|
||||
ts := id.NewTokenSource(log, identity, query, cc.User, cc.Password)
|
||||
err := identity.Login(ts)
|
||||
ts := id.NewIdentity(log, cc.User, cc.Password)
|
||||
err := ts.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := id.NewAPI(log, identity)
|
||||
api := id.NewAPI(log, ts)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
if cc.VIN == "" {
|
||||
|
|
|
|||
|
|
@ -34,13 +34,13 @@ const (
|
|||
)
|
||||
|
||||
// NewAPI creates a new vehicle
|
||||
func NewAPI(log *util.Logger, identity oauth2.TokenSource) *API {
|
||||
func NewAPI(log *util.Logger, ts oauth2.TokenSource) *API {
|
||||
v := &API{
|
||||
Helper: request.NewHelper(log),
|
||||
}
|
||||
|
||||
v.Client.Transport = &oauth2.Transport{
|
||||
Source: identity,
|
||||
Source: ts,
|
||||
Base: v.Client.Transport,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/oauth"
|
||||
|
|
@ -18,40 +17,34 @@ const (
|
|||
OauthTokenURI = "https://login.apps.emea.vwapps.io"
|
||||
)
|
||||
|
||||
type TokenSource struct {
|
||||
type Identity struct {
|
||||
*request.Helper
|
||||
identity vw.PlatformLogin
|
||||
query url.Values
|
||||
user string
|
||||
password string
|
||||
idtp *vw.IDTokenProvider
|
||||
oauth2.TokenSource
|
||||
}
|
||||
|
||||
var _ vw.TokenSourceProvider = (*TokenSource)(nil)
|
||||
func NewIdentity(log *util.Logger, user, password string) *Identity {
|
||||
uri := fmt.Sprintf("%s/authorize?%s", OauthTokenURI, AuthParams.Encode())
|
||||
|
||||
func NewTokenSource(log *util.Logger, identity vw.PlatformLogin, query url.Values, user, password string) *TokenSource {
|
||||
return &TokenSource{
|
||||
Helper: request.NewHelper(log),
|
||||
identity: identity,
|
||||
query: query,
|
||||
user: user,
|
||||
password: password,
|
||||
return &Identity{
|
||||
Helper: request.NewHelper(log),
|
||||
idtp: vw.NewIDTokenProvider(log, uri, user, password),
|
||||
}
|
||||
}
|
||||
|
||||
func (v *TokenSource) TokenSource() (oauth2.TokenSource, error) {
|
||||
func (v *Identity) Login() error {
|
||||
token, err := v.login()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
return oauth.RefreshTokenSource((*oauth2.Token)(&token), v), nil
|
||||
v.TokenSource = oauth.RefreshTokenSource((*oauth2.Token)(&token), v)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *TokenSource) login() (Token, error) {
|
||||
var token Token
|
||||
uri := fmt.Sprintf("%s/authorize?%s", OauthTokenURI, v.query.Encode())
|
||||
|
||||
q, err := v.identity.UserLogin(uri, v.user, v.password)
|
||||
func (v *Identity) login() (Token, error) {
|
||||
q, err := v.idtp.Login()
|
||||
|
||||
if err == nil {
|
||||
for _, k := range []string{"state", "id_token", "access_token", "code"} {
|
||||
|
|
@ -62,6 +55,7 @@ func (v *TokenSource) login() (Token, error) {
|
|||
}
|
||||
}
|
||||
|
||||
var token Token
|
||||
if err == nil {
|
||||
data := map[string]string{
|
||||
"state": q.Get("state"),
|
||||
|
|
@ -73,7 +67,7 @@ func (v *TokenSource) login() (Token, error) {
|
|||
}
|
||||
|
||||
var req *http.Request
|
||||
uri = fmt.Sprintf("%s/login/v1", OauthTokenURI)
|
||||
uri := fmt.Sprintf("%s/login/v1", OauthTokenURI)
|
||||
req, err = request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding)
|
||||
|
||||
if err == nil {
|
||||
|
|
@ -84,7 +78,7 @@ func (v *TokenSource) login() (Token, error) {
|
|||
return token, err
|
||||
}
|
||||
|
||||
func (v *TokenSource) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
||||
func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
||||
uri := fmt.Sprintf("%s/refresh/v1", OauthTokenURI)
|
||||
|
||||
req, err := request.New(http.MethodGet, uri, nil, map[string]string{
|
||||
10
vehicle/id/params.go
Normal file
10
vehicle/id/params.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package id
|
||||
|
||||
import "net/url"
|
||||
|
||||
var AuthParams = url.Values(map[string][]string{
|
||||
"response_type": {"code id_token token"},
|
||||
"client_id": {"a24fba63-34b3-4d43-b181-942111e6bda8@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"weconnect://authenticated"},
|
||||
"scope": {"openid profile badge cars vin"}, // dealers
|
||||
})
|
||||
|
|
@ -2,13 +2,13 @@ package vehicle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/vehicle/seat"
|
||||
"github.com/evcc-io/evcc/vehicle/vw"
|
||||
)
|
||||
|
||||
|
|
@ -46,22 +46,14 @@ func NewSeatFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("seat").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := vw.NewIdentity(log)
|
||||
|
||||
query := url.Values(map[string][]string{
|
||||
"response_type": {"code id_token"},
|
||||
"client_id": {"50f215ac-4444-4230-9fb1-fe15cd1a9bcc@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"seatconnect://identity-kit/login"},
|
||||
"scope": {"openid profile mbb"}, // cars birthdate nickname address phone
|
||||
})
|
||||
|
||||
ts := vw.NewTokenSource(log, identity, "9dcc70f0-8e79-423a-a3fa-4065d99088b4", query, cc.User, cc.Password)
|
||||
err := identity.Login(ts)
|
||||
identity := vw.NewIdentity(log, seat.AuthClientID, seat.AuthParams, cc.User, cc.Password)
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := vw.NewAPI(log, identity, "VW", "ES")
|
||||
api := vw.NewAPI(log, identity, seat.Brand, seat.Country)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
if cc.VIN == "" {
|
||||
|
|
|
|||
19
vehicle/seat/params.go
Normal file
19
vehicle/seat/params.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package seat
|
||||
|
||||
import "net/url"
|
||||
|
||||
const (
|
||||
Brand = "VW"
|
||||
Country = "ES"
|
||||
|
||||
// Authorization ClientID
|
||||
AuthClientID = "9dcc70f0-8e79-423a-a3fa-4065d99088b4"
|
||||
)
|
||||
|
||||
// Authorization parameters
|
||||
var AuthParams = url.Values(map[string][]string{
|
||||
"response_type": {"code id_token"},
|
||||
"client_id": {"50f215ac-4444-4230-9fb1-fe15cd1a9bcc@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"seatconnect://identity-kit/login"},
|
||||
"scope": {"openid profile mbb"}, // cars birthdate nickname address phone
|
||||
})
|
||||
|
|
@ -2,13 +2,13 @@ package vehicle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/vehicle/skoda"
|
||||
"github.com/evcc-io/evcc/vehicle/vw"
|
||||
)
|
||||
|
||||
|
|
@ -46,22 +46,14 @@ func NewSkodaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("skoda").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := vw.NewIdentity(log)
|
||||
|
||||
query := url.Values(map[string][]string{
|
||||
"response_type": {"code id_token"},
|
||||
"client_id": {"f9a2359a-b776-46d9-bd0c-db1904343117@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"skodaconnect://oidc.login/"},
|
||||
"scope": {"openid mbb profile"},
|
||||
})
|
||||
|
||||
ts := vw.NewTokenSource(log, identity, "afb0473b-6d82-42b8-bfea-cead338c46ef", query, cc.User, cc.Password)
|
||||
err := identity.Login(ts)
|
||||
identity := vw.NewIdentity(log, skoda.AuthClientID, skoda.AuthParams, cc.User, cc.Password)
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := vw.NewAPI(log, identity, "VW", "CZ")
|
||||
api := vw.NewAPI(log, identity, skoda.Brand, skoda.Country)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
if cc.VIN == "" {
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@ type API struct {
|
|||
}
|
||||
|
||||
// NewAPI creates a new api client
|
||||
func NewAPI(log *util.Logger, identity oauth2.TokenSource) *API {
|
||||
func NewAPI(log *util.Logger, ts oauth2.TokenSource) *API {
|
||||
v := &API{
|
||||
Helper: request.NewHelper(log),
|
||||
}
|
||||
|
||||
v.Client.Transport = &oauth2.Transport{
|
||||
Source: identity,
|
||||
Source: ts,
|
||||
Base: v.Client.Transport,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,40 +19,34 @@ const (
|
|||
OauthTokenURI = "https://tokenrefreshservice.apps.emea.vwapps.io"
|
||||
)
|
||||
|
||||
type TokenSource struct {
|
||||
type Identity struct {
|
||||
*request.Helper
|
||||
identity vw.PlatformLogin
|
||||
query url.Values
|
||||
user string
|
||||
password string
|
||||
idtp *vw.IDTokenProvider
|
||||
oauth2.TokenSource
|
||||
}
|
||||
|
||||
var _ vw.TokenSourceProvider = (*TokenSource)(nil)
|
||||
func NewIdentity(log *util.Logger, query url.Values, user, password string) *Identity {
|
||||
uri := fmt.Sprintf("%s/oidc/v1/authorize?%s", vw.IdentityURI, query.Encode())
|
||||
|
||||
func NewTokenSource(log *util.Logger, identity vw.PlatformLogin, query url.Values, user, password string) *TokenSource {
|
||||
return &TokenSource{
|
||||
Helper: request.NewHelper(log),
|
||||
identity: identity,
|
||||
query: query,
|
||||
user: user,
|
||||
password: password,
|
||||
return &Identity{
|
||||
Helper: request.NewHelper(log),
|
||||
idtp: vw.NewIDTokenProvider(log, uri, user, password),
|
||||
}
|
||||
}
|
||||
|
||||
func (v *TokenSource) TokenSource() (oauth2.TokenSource, error) {
|
||||
func (v *Identity) Login() error {
|
||||
token, err := v.login()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
return oauth.RefreshTokenSource(&token.Token, v), nil
|
||||
v.TokenSource = oauth.RefreshTokenSource(&token.Token, v)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *TokenSource) login() (vw.Token, error) {
|
||||
var token vw.Token
|
||||
uri := fmt.Sprintf("%s/oidc/v1/authorize?%s", vw.IdentityURI, v.query.Encode())
|
||||
|
||||
q, err := v.identity.UserLogin(uri, v.user, v.password)
|
||||
func (v *Identity) login() (vw.Token, error) {
|
||||
q, err := v.idtp.Login()
|
||||
|
||||
if err == nil {
|
||||
for _, k := range []string{"id_token", "code"} {
|
||||
|
|
@ -63,6 +57,7 @@ func (v *TokenSource) login() (vw.Token, error) {
|
|||
}
|
||||
}
|
||||
|
||||
var token vw.Token
|
||||
if err == nil {
|
||||
data := url.Values(map[string][]string{
|
||||
"auth_code": {q.Get("code")},
|
||||
|
|
@ -71,7 +66,7 @@ func (v *TokenSource) login() (vw.Token, error) {
|
|||
})
|
||||
|
||||
var req *http.Request
|
||||
uri = fmt.Sprintf("%s/exchangeAuthCode", OauthTokenURI)
|
||||
uri := fmt.Sprintf("%s/exchangeAuthCode", OauthTokenURI)
|
||||
req, err = request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), request.URLEncoding)
|
||||
|
||||
if err == nil {
|
||||
|
|
@ -88,7 +83,7 @@ func (v *TokenSource) login() (vw.Token, error) {
|
|||
}
|
||||
|
||||
// RefreshToken implements oauth.TokenRefresher
|
||||
func (v *TokenSource) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
||||
func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
||||
uri := fmt.Sprintf("%s/refreshTokens", OauthTokenURI)
|
||||
|
||||
data := url.Values(map[string][]string{
|
||||
27
vehicle/skoda/params.go
Normal file
27
vehicle/skoda/params.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package skoda
|
||||
|
||||
import "net/url"
|
||||
|
||||
const (
|
||||
Brand = "VW"
|
||||
Country = "CZ"
|
||||
|
||||
// Authorization ClientID
|
||||
AuthClientID = "afb0473b-6d82-42b8-bfea-cead338c46ef"
|
||||
)
|
||||
|
||||
// Skoda native api
|
||||
var AuthParams = url.Values(map[string][]string{
|
||||
"response_type": {"code id_token"},
|
||||
"client_id": {"f9a2359a-b776-46d9-bd0c-db1904343117@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"skodaconnect://oidc.login/"},
|
||||
"scope": {"openid mbb profile"},
|
||||
})
|
||||
|
||||
// Skoda connect api
|
||||
var ConnectAuthParams = url.Values(map[string][]string{
|
||||
"response_type": {"code id_token"},
|
||||
"redirect_uri": {"skodaconnect://oidc.login/"},
|
||||
"client_id": {"7f045eee-7003-4379-9968-9355ed2adb06@apps_vw-dilab_com"},
|
||||
"scope": {"openid profile mbb"}, // phone address cars email birthdate badge dealers driversLicense
|
||||
})
|
||||
|
|
@ -2,7 +2,6 @@ package vehicle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -46,22 +45,14 @@ func NewVWFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
log := util.NewLogger("vw").Redact(cc.User, cc.Password, cc.VIN)
|
||||
identity := vw.NewIdentity(log)
|
||||
|
||||
query := url.Values(map[string][]string{
|
||||
"response_type": {"id_token token"},
|
||||
"client_id": {"9496332b-ea03-4091-a224-8c746b885068@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"carnet://identity-kit/login"},
|
||||
"scope": {"openid profile mbb"}, // cars birthdate nickname address phone
|
||||
})
|
||||
|
||||
ts := vw.NewTokenSource(log, identity, "38761134-34d0-41f3-9a73-c4be88d7d337", query, cc.User, cc.Password)
|
||||
err := identity.Login(ts)
|
||||
identity := vw.NewIdentity(log, vw.AuthClientID, vw.AuthParams, cc.User, cc.Password)
|
||||
err := identity.Login()
|
||||
if err != nil {
|
||||
return v, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
api := vw.NewAPI(log, identity, "VW", "DE")
|
||||
api := vw.NewAPI(log, identity, vw.Brand, vw.Country)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
if cc.VIN == "" {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ type API struct {
|
|||
}
|
||||
|
||||
// NewAPI creates a new api client
|
||||
func NewAPI(log *util.Logger, identity oauth2.TokenSource, brand, country string) *API {
|
||||
func NewAPI(log *util.Logger, ts oauth2.TokenSource, brand, country string) *API {
|
||||
v := &API{
|
||||
Helper: request.NewHelper(log),
|
||||
brand: brand,
|
||||
|
|
@ -34,7 +34,7 @@ func NewAPI(log *util.Logger, identity oauth2.TokenSource, brand, country string
|
|||
}
|
||||
|
||||
v.Client.Transport = &oauth2.Transport{
|
||||
Source: identity,
|
||||
Source: ts,
|
||||
Base: v.Client.Transport,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,15 @@
|
|||
package vw
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/oauth"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"golang.org/x/net/publicsuffix"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
|
|
@ -25,155 +24,91 @@ const (
|
|||
OauthRevokeURI = "https://mbboauth-1d.prd.ece.vwg-connect.com/mbbcoauth/mobile/oauth2/v1/revoke"
|
||||
)
|
||||
|
||||
var _ PlatformLogin = (*Identity)(nil)
|
||||
|
||||
// PlatformLogin provides the user login to identity.vwgroup.io. It does not handle the token exchange.
|
||||
type PlatformLogin interface {
|
||||
UserLogin(uri, user, password string) (url.Values, error)
|
||||
}
|
||||
|
||||
// TokenSourceProvider provides the token source by exchanging the platform token for specific service tokens
|
||||
type TokenSourceProvider interface {
|
||||
TokenSource() (oauth2.TokenSource, error)
|
||||
}
|
||||
|
||||
// Identity provides the identity.vwgroup.io login token source
|
||||
type Identity struct {
|
||||
log *util.Logger
|
||||
*request.Helper
|
||||
idtp *IDTokenProvider
|
||||
clientID string
|
||||
oauth2.TokenSource
|
||||
}
|
||||
|
||||
// NewIdentity creates VW identity
|
||||
func NewIdentity(log *util.Logger) *Identity {
|
||||
v := &Identity{
|
||||
log: log,
|
||||
Helper: request.NewHelper(log),
|
||||
}
|
||||
func NewIdentity(log *util.Logger, clientID string, query url.Values, user, password string) *Identity {
|
||||
uri := fmt.Sprintf("%s/oidc/v1/authorize?%s", IdentityURI, query.Encode())
|
||||
|
||||
return v
|
||||
return &Identity{
|
||||
Helper: request.NewHelper(log),
|
||||
idtp: NewIDTokenProvider(log, uri, user, password),
|
||||
clientID: clientID,
|
||||
}
|
||||
}
|
||||
|
||||
// UserLogin performs the identity.vwgroup.io login
|
||||
func (v *Identity) UserLogin(uri, user, password string) (url.Values, error) {
|
||||
if user == "" || password == "" {
|
||||
return nil, api.ErrMissingCredentials
|
||||
func (v *Identity) Login() error {
|
||||
token, err := v.login()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// track cookies and don't follow redirects
|
||||
jar, _ := cookiejar.New(&cookiejar.Options{
|
||||
PublicSuffixList: publicsuffix.List,
|
||||
v.TokenSource = oauth.RefreshTokenSource(&token.Token, v)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// login performs the login using the brand-specific subflow for obtaining the
|
||||
// id token and finally exchanges the id token for access and refresh tokens
|
||||
func (v *Identity) login() (Token, error) {
|
||||
q, err := v.idtp.Login()
|
||||
|
||||
if err == nil && q.Get("id_token") == "" {
|
||||
err = errors.New("missing id_token")
|
||||
}
|
||||
|
||||
var token Token
|
||||
if err == nil {
|
||||
data := url.Values(map[string][]string{
|
||||
"grant_type": {"id_token"},
|
||||
"scope": {"sc2:fal"},
|
||||
"token": {q.Get("id_token")},
|
||||
})
|
||||
|
||||
var req *http.Request
|
||||
req, err = request.New(http.MethodPost, OauthTokenURI, strings.NewReader(data.Encode()), map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"X-Client-Id": v.clientID,
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
err = v.DoJSON(req, &token)
|
||||
}
|
||||
|
||||
// check if token response contained error
|
||||
if errT := token.Error(); err != nil && errT != nil {
|
||||
err = fmt.Errorf("token exchange: %w", errT)
|
||||
}
|
||||
}
|
||||
|
||||
return token, err
|
||||
}
|
||||
|
||||
// RefreshToken implements oauth.TokenRefresher
|
||||
func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
||||
data := url.Values(map[string][]string{
|
||||
"grant_type": {"refresh_token"},
|
||||
"refresh_token": {token.RefreshToken},
|
||||
"scope": {"sc2:fal"},
|
||||
})
|
||||
|
||||
v.Client.Jar = jar
|
||||
defer func() { v.Client.Jar = nil }()
|
||||
req, err := request.New(http.MethodPost, OauthTokenURI, strings.NewReader(data.Encode()), map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"X-Client-Id": v.clientID,
|
||||
})
|
||||
|
||||
v.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||
if req.URL.Scheme != "https" {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var vars FormVars
|
||||
|
||||
// add nonce and state
|
||||
query := url.Values{
|
||||
"nonce": []string{util.RandomString(43)},
|
||||
"state": []string{util.RandomString(43)},
|
||||
}
|
||||
uri += "&" + query.Encode()
|
||||
|
||||
// GET identity.vwgroup.io/signin-service/v1/signin/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com?relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa
|
||||
resp, err := v.Get(uri)
|
||||
var res Token
|
||||
if err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#emailPasswordForm")
|
||||
resp.Body.Close()
|
||||
err = v.DoJSON(req, &res)
|
||||
}
|
||||
|
||||
// POST identity.vwgroup.io/signin-service/v1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com/login/identifier
|
||||
if err == nil {
|
||||
data := url.Values(map[string][]string{
|
||||
"_csrf": {vars.Inputs["_csrf"]},
|
||||
"relayState": {vars.Inputs["relayState"]},
|
||||
"hmac": {vars.Inputs["hmac"]},
|
||||
"email": {user},
|
||||
})
|
||||
|
||||
uri = IdentityURI + vars.Action
|
||||
if resp, err = v.PostForm(uri, data); err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#credentialsForm")
|
||||
resp.Body.Close()
|
||||
}
|
||||
if se, ok := err.(request.StatusError); ok && se.HasStatus(http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden) {
|
||||
res, err = v.login()
|
||||
}
|
||||
|
||||
// POST identity.vwgroup.io/signin-service/v1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com/login/authenticate
|
||||
if err == nil {
|
||||
data := url.Values(map[string][]string{
|
||||
"_csrf": {vars.Inputs["_csrf"]},
|
||||
"relayState": {vars.Inputs["relayState"]},
|
||||
"hmac": {vars.Inputs["hmac"]},
|
||||
"email": {user},
|
||||
"password": {password},
|
||||
})
|
||||
|
||||
uri = IdentityURI + vars.Action
|
||||
if resp, err = v.PostForm(uri, data); err == nil {
|
||||
resp.Body.Close()
|
||||
|
||||
if e := resp.Request.URL.Query().Get("error"); e != "" {
|
||||
err = fmt.Errorf(e)
|
||||
}
|
||||
|
||||
if u := resp.Request.URL.Query().Get("updated"); err == nil && u != "" {
|
||||
v.log.WARN.Println("accepting updated tos", u)
|
||||
if resp, err = v.postTos(resp.Request.URL.String()); err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GET identity.vwgroup.io/oidc/v1/oauth/sso?clientId=b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com&relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa&userId=bca09cc0-8eba-4110-af71-7242868e1bf1&HMAC=2b01ce6a351fad4dd97dc8110d0967b46c95889ab5010c660a616462e66a83ca
|
||||
// GET identity.vwgroup.io/signin-service/v1/consent/users/bca09cc0-8eba-4110-af71-7242868e1bf1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com?scopes=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa&callback=https://identity.vwgroup.io/oidc/v1/oauth/client/callback&hmac=a590931ca3cd9dc3a27f1d1c0c162bf1e5c5c32c9f5b40fcb36d4c6edc631e03
|
||||
// GET identity.vwgroup.io/oidc/v1/oauth/client/callback/success?user_id=bca09cc0-8eba-4110-af71-7242868e1bf1&client_id=b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com&scopes=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&consentedScopes=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&relayState=f89a0b750c93e278a7ace170ce374e9cb9eb0a74&hmac=2b728f463c3cfe80f3271fbb35680e5e5218ca70025a46e7fadf7c7982decc2b
|
||||
|
||||
var location *url.URL
|
||||
if err == nil {
|
||||
loc := strings.ReplaceAll(resp.Header.Get("Location"), "#", "?") // convert to parseable url
|
||||
if location, err = url.Parse(loc); err == nil {
|
||||
return location.Query(), nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (v *Identity) postTos(uri string) (*http.Response, error) {
|
||||
var vars FormVars
|
||||
resp, err := v.Get(uri)
|
||||
if err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#emailPasswordForm")
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
data := make(url.Values)
|
||||
for k, v := range vars.Inputs {
|
||||
data.Set(k, v)
|
||||
}
|
||||
|
||||
uri := IdentityURI + vars.Action
|
||||
resp, err = v.PostForm(uri, data)
|
||||
}
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Login executes the login flow by obtaining the oauth token source from the provider
|
||||
func (v *Identity) Login(prov TokenSourceProvider) error {
|
||||
ts, err := prov.TokenSource()
|
||||
if err == nil {
|
||||
v.TokenSource = ts
|
||||
}
|
||||
return err
|
||||
return &res.Token, err
|
||||
}
|
||||
|
|
|
|||
19
vehicle/vw/params.go
Normal file
19
vehicle/vw/params.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package vw
|
||||
|
||||
import "net/url"
|
||||
|
||||
const (
|
||||
Brand = "VW"
|
||||
Country = "DE"
|
||||
|
||||
// Authorization ClientID
|
||||
AuthClientID = "38761134-34d0-41f3-9a73-c4be88d7d337"
|
||||
)
|
||||
|
||||
// Authorization parameters
|
||||
var AuthParams = url.Values(map[string][]string{
|
||||
"response_type": {"id_token token"},
|
||||
"client_id": {"9496332b-ea03-4091-a224-8c746b885068@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"carnet://identity-kit/login"},
|
||||
"scope": {"openid profile mbb"}, // cars birthdate nickname address phone
|
||||
})
|
||||
149
vehicle/vw/tokenprovider.go
Normal file
149
vehicle/vw/tokenprovider.go
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
package vw
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
||||
// IDTokenProvider provides the identity.vwgroup.io login token source
|
||||
type IDTokenProvider struct {
|
||||
log *util.Logger
|
||||
*request.Helper
|
||||
uri, user, password string
|
||||
}
|
||||
|
||||
// NewIDTokenProvider creates VW identity
|
||||
func NewIDTokenProvider(log *util.Logger, uri, user, password string) *IDTokenProvider {
|
||||
v := &IDTokenProvider{
|
||||
log: log,
|
||||
Helper: request.NewHelper(log),
|
||||
uri: uri,
|
||||
user: user,
|
||||
password: password,
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// Login performs the identity.vwgroup.io login
|
||||
func (v *IDTokenProvider) Login() (url.Values, error) {
|
||||
if v.user == "" || v.password == "" {
|
||||
return nil, api.ErrMissingCredentials
|
||||
}
|
||||
|
||||
// track cookies and don't follow redirects
|
||||
jar, _ := cookiejar.New(&cookiejar.Options{
|
||||
PublicSuffixList: publicsuffix.List,
|
||||
})
|
||||
|
||||
v.Client.Jar = jar
|
||||
defer func() { v.Client.Jar = nil }()
|
||||
|
||||
v.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||
if req.URL.Scheme != "https" {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var vars FormVars
|
||||
|
||||
// add nonce and state
|
||||
query := url.Values{
|
||||
"nonce": []string{util.RandomString(43)},
|
||||
"state": []string{util.RandomString(43)},
|
||||
}
|
||||
uri := v.uri + "&" + query.Encode()
|
||||
|
||||
// GET identity.vwgroup.io/signin-service/v1/signin/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com?relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa
|
||||
resp, err := v.Get(uri)
|
||||
if err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#emailPasswordForm")
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
// POST identity.vwgroup.io/signin-service/v1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com/login/identifier
|
||||
if err == nil {
|
||||
data := url.Values(map[string][]string{
|
||||
"_csrf": {vars.Inputs["_csrf"]},
|
||||
"relayState": {vars.Inputs["relayState"]},
|
||||
"hmac": {vars.Inputs["hmac"]},
|
||||
"email": {v.user},
|
||||
})
|
||||
|
||||
uri = IdentityURI + vars.Action
|
||||
if resp, err = v.PostForm(uri, data); err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#credentialsForm")
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// POST identity.vwgroup.io/signin-service/v1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com/login/authenticate
|
||||
if err == nil {
|
||||
data := url.Values(map[string][]string{
|
||||
"_csrf": {vars.Inputs["_csrf"]},
|
||||
"relayState": {vars.Inputs["relayState"]},
|
||||
"hmac": {vars.Inputs["hmac"]},
|
||||
"email": {v.user},
|
||||
"password": {v.password},
|
||||
})
|
||||
|
||||
uri = IdentityURI + vars.Action
|
||||
if resp, err = v.PostForm(uri, data); err == nil {
|
||||
resp.Body.Close()
|
||||
|
||||
if e := resp.Request.URL.Query().Get("error"); e != "" {
|
||||
err = fmt.Errorf(e)
|
||||
}
|
||||
|
||||
if u := resp.Request.URL.Query().Get("updated"); err == nil && u != "" {
|
||||
v.log.WARN.Println("accepting updated tos", u)
|
||||
if resp, err = v.postTos(resp.Request.URL.String()); err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GET identity.vwgroup.io/oidc/v1/oauth/sso?clientId=b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com&relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa&userId=bca09cc0-8eba-4110-af71-7242868e1bf1&HMAC=2b01ce6a351fad4dd97dc8110d0967b46c95889ab5010c660a616462e66a83ca
|
||||
// GET identity.vwgroup.io/signin-service/v1/consent/users/bca09cc0-8eba-4110-af71-7242868e1bf1/b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com?scopes=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&relayState=15404cb51c8b4cc5efeee1d2c2a73e5b41562faa&callback=https://identity.vwgroup.io/oidc/v1/oauth/client/callback&hmac=a590931ca3cd9dc3a27f1d1c0c162bf1e5c5c32c9f5b40fcb36d4c6edc631e03
|
||||
// GET identity.vwgroup.io/oidc/v1/oauth/client/callback/success?user_id=bca09cc0-8eba-4110-af71-7242868e1bf1&client_id=b7a5bb47-f875-47cf-ab83-2ba3bf6bb738@apps_vw-dilab_com&scopes=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&consentedScopes=openid%20profile%20birthdate%20nickname%20address%20phone%20cars%20mbb&relayState=f89a0b750c93e278a7ace170ce374e9cb9eb0a74&hmac=2b728f463c3cfe80f3271fbb35680e5e5218ca70025a46e7fadf7c7982decc2b
|
||||
|
||||
var location *url.URL
|
||||
if err == nil {
|
||||
loc := strings.ReplaceAll(resp.Header.Get("Location"), "#", "?") // convert to parseable url
|
||||
if location, err = url.Parse(loc); err == nil {
|
||||
return location.Query(), nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (v *IDTokenProvider) postTos(uri string) (*http.Response, error) {
|
||||
var vars FormVars
|
||||
resp, err := v.Get(uri)
|
||||
if err == nil {
|
||||
vars, err = FormValues(resp.Body, "form#emailPasswordForm")
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
data := make(url.Values)
|
||||
for k, v := range vars.Inputs {
|
||||
data.Set(k, v)
|
||||
}
|
||||
|
||||
uri := IdentityURI + vars.Action
|
||||
resp, err = v.PostForm(uri, data)
|
||||
}
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
|
@ -1,107 +0,0 @@
|
|||
package vw
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/oauth"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type TokenSource struct {
|
||||
*request.Helper
|
||||
identity PlatformLogin
|
||||
clientID string
|
||||
query url.Values
|
||||
user string
|
||||
password string
|
||||
}
|
||||
|
||||
var _ TokenSourceProvider = (*TokenSource)(nil)
|
||||
|
||||
func NewTokenSource(log *util.Logger, identity PlatformLogin, clientID string, query url.Values, user, password string) *TokenSource {
|
||||
return &TokenSource{
|
||||
Helper: request.NewHelper(log),
|
||||
identity: identity,
|
||||
clientID: clientID,
|
||||
query: query,
|
||||
user: user,
|
||||
password: password,
|
||||
}
|
||||
}
|
||||
|
||||
func (v *TokenSource) TokenSource() (oauth2.TokenSource, error) {
|
||||
token, err := v.login()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return oauth.RefreshTokenSource(&token.Token, v), nil
|
||||
}
|
||||
|
||||
// LoginVAG performs VAG login and finally exchanges id token for access and refresh tokens
|
||||
func (v *TokenSource) login() (Token, error) {
|
||||
var token Token
|
||||
uri := fmt.Sprintf("%s/oidc/v1/authorize?%s", IdentityURI, v.query.Encode())
|
||||
|
||||
q, err := v.identity.UserLogin(uri, v.user, v.password)
|
||||
|
||||
if err == nil && q.Get("id_token") == "" {
|
||||
err = errors.New("missing id_token")
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
data := url.Values(map[string][]string{
|
||||
"grant_type": {"id_token"},
|
||||
"scope": {"sc2:fal"},
|
||||
"token": {q.Get("id_token")},
|
||||
})
|
||||
|
||||
var req *http.Request
|
||||
req, err = request.New(http.MethodPost, OauthTokenURI, strings.NewReader(data.Encode()), map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"X-Client-Id": v.clientID,
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
err = v.DoJSON(req, &token)
|
||||
}
|
||||
|
||||
// check if token response contained error
|
||||
if errT := token.Error(); err != nil && errT != nil {
|
||||
err = fmt.Errorf("token exchange: %w", errT)
|
||||
}
|
||||
}
|
||||
|
||||
return token, err
|
||||
}
|
||||
|
||||
// RefreshToken implements oauth.TokenRefresher
|
||||
func (v *TokenSource) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
||||
data := url.Values(map[string][]string{
|
||||
"grant_type": {"refresh_token"},
|
||||
"refresh_token": {token.RefreshToken},
|
||||
"scope": {"sc2:fal"},
|
||||
})
|
||||
|
||||
req, err := request.New(http.MethodPost, OauthTokenURI, strings.NewReader(data.Encode()), map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"X-Client-Id": v.clientID,
|
||||
})
|
||||
|
||||
var res Token
|
||||
if err == nil {
|
||||
err = v.DoJSON(req, &res)
|
||||
}
|
||||
|
||||
if se, ok := err.(request.StatusError); ok && se.HasStatus(http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden) {
|
||||
res, err = v.login()
|
||||
}
|
||||
|
||||
return &res.Token, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue