Skip to content

Commit

Permalink
Porsche: remove deprecated mobile api (#8349)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored and naltatis committed Jun 14, 2023
1 parent 11895fb commit fdb6bd9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 276 deletions.
10 changes: 1 addition & 9 deletions vehicle/porsche.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,8 @@ func NewPorscheFromConfig(other map[string]interface{}) (api.Vehicle, error) {
}

api := porsche.NewAPI(log, identity.DefaultSource)
mobile := porsche.NewMobileAPI(log, identity.MobileSource)

cc.VIN, err = ensureVehicle(cc.VIN, func() ([]string, error) {
mobileVehicles, err := mobile.Vehicles()
if err == nil {
return lo.Map(mobileVehicles, func(v porsche.StatusResponseMobile, _ int) string {
return v.VIN
}), err
}

vehicles, err := api.Vehicles()
return lo.Map(vehicles, func(v porsche.Vehicle, _ int) string {
return v.VIN
Expand All @@ -80,7 +72,7 @@ func NewPorscheFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return nil, err
}

provider := porsche.NewProvider(log, api, emobility, mobile, cc.VIN, capabilities.CarModel, cc.Cache)
provider := porsche.NewProvider(log, api, emobility, cc.VIN, capabilities.CarModel, cc.Cache)

v := &Porsche{
embed: &cc.embed,
Expand Down
3 changes: 1 addition & 2 deletions vehicle/porsche/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func NewAPI(log *util.Logger, identity oauth2.TokenSource) *API {
Base: v.Client.Transport,
},
Decorator: transport.DecorateHeaders(map[string]string{
"apikey": OAuth2Config.ClientID,
"x-client-id": "52064df8-6daa-46f7-bc9e-e3232622ab26",
"apikey": OAuth2Config.ClientID,
}),
}

Expand Down
4 changes: 2 additions & 2 deletions vehicle/porsche/api_emobility.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewEmobilityAPI(log *util.Logger, identity oauth2.TokenSource) *EmobilityAP

func (v *EmobilityAPI) Capabilities(vin string) (CapabilitiesResponse, error) {
var res CapabilitiesResponse
uri := fmt.Sprintf("https://api.porsche.com/e-mobility/vcs/capabilities/%s", vin)
uri := fmt.Sprintf("%s/e-mobility/vcs/capabilities/%s", ApiURI, vin)
err := v.GetJSON(uri, &res)
return res, err
}
Expand All @@ -45,7 +45,7 @@ func (v *EmobilityAPI) Capabilities(vin string) (CapabilitiesResponse, error) {
func (v *EmobilityAPI) Status(vin, model string) (EmobilityResponse, error) {
var res EmobilityResponse

uri := fmt.Sprintf("https://api.porsche.com/e-mobility/de/de_DE/%s/%s?timezone=Europe/Berlin", model, vin)
uri := fmt.Sprintf("%s/e-mobility/de/de_DE/%s/%s?timezone=Europe/Berlin", ApiURI, model, vin)
err := v.GetJSON(uri, &res)
if err != nil && res.PcckErrorMessage != "" {
err = errors.New(res.PcckErrorMessage)
Expand Down
121 changes: 0 additions & 121 deletions vehicle/porsche/api_mobile.go

This file was deleted.

70 changes: 21 additions & 49 deletions vehicle/porsche/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"strings"

"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/oauth"
Expand Down Expand Up @@ -39,21 +38,14 @@ var (
Endpoint: OAuth2Config.Endpoint,
Scopes: []string{"openid"},
}

MobileOAuth2Config = &oauth2.Config{
ClientID: "L20OiZ0kBgWt958NWbuCB8gb970y6V6U",
RedirectURL: "One-Product-App://porsche-id/oauth2redirect",
Endpoint: OAuth2Config.Endpoint,
Scopes: []string{"openid", "magiclink", "mbb"},
}
)

// Identity is the Porsche Identity client
type Identity struct {
*request.Helper
user, password string
defaultToken, emobilityToken, mobileToken *oauth2.Token
DefaultSource, EmobilitySource, MobileSource oauth2.TokenSource
user, password string
defaultToken, emobilityToken *oauth2.Token
DefaultSource, EmobilitySource oauth2.TokenSource
}

// NewIdentity creates Porsche identity
Expand All @@ -73,7 +65,6 @@ func (v *Identity) Login() error {
if err == nil {
v.DefaultSource = oauth.RefreshTokenSource(v.defaultToken, v)
v.EmobilitySource = oauth.RefreshTokenSource(v.emobilityToken, &emobilityAdapter{v})
v.MobileSource = oauth.RefreshTokenSource(v.mobileToken, &mobileAdapter{v})
}

return err
Expand All @@ -98,9 +89,19 @@ func (v *Identity) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
v.Client.CheckRedirect = nil
}()

preLogin := url.Values{
"sec": []string{""},
"resume": []string{""},
"thirdPartyId": []string{""},
"state": []string{""},
"username": []string{v.user},
"password": []string{v.password},
"keeploggedin": []string{"false"},
}

// get the login page
uri := fmt.Sprintf("%s/auth/api/v1/de/de_DE/public/login", OAuthURI)
resp, err := v.Get(uri)
resp, err := v.PostForm(uri, preLogin)
if err != nil {
return nil, err
}
Expand All @@ -111,28 +112,19 @@ func (v *Identity) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
return nil, err
}

sec := query.Get("sec")
resume := query.Get("resume")
state := query.Get("state")
thirdPartyID := query.Get("thirdPartyId")

dataLoginAuth := url.Values{
"sec": []string{sec},
"resume": []string{resume},
"thirdPartyId": []string{thirdPartyID},
"state": []string{state},
"sec": []string{query.Get("sec")},
"resume": []string{query.Get("resume")},
"thirdPartyId": []string{query.Get("thirdPartyID")},
"state": []string{query.Get("state")},
"username": []string{v.user},
"password": []string{v.password},
"keeploggedin": []string{"false"},
}

req, err := request.New(http.MethodPost, uri, strings.NewReader(dataLoginAuth.Encode()), request.URLEncoding)
if err != nil {
return nil, err
}

// process the auth so the session is authenticated
if resp, err = v.Client.Do(req); err != nil {
resp, err = v.PostForm(uri, dataLoginAuth)
if err != nil {
return nil, err
}
resp.Body.Close()
Expand All @@ -145,10 +137,6 @@ func (v *Identity) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
if token, err = v.fetchToken(EmobilityOAuth2Config); err == nil {
v.emobilityToken = token
}

if token, err = v.fetchToken(MobileOAuth2Config); err == nil {
v.mobileToken = token
}
}

return v.defaultToken, err
Expand Down Expand Up @@ -180,11 +168,7 @@ func (v *Identity) fetchToken(oc *oauth2.Config) (*oauth2.Token, error) {
}
resp.Body.Close()

rawQuery := resp.Request.URL.RawQuery
if location, ok := strings.CutPrefix(resp.Header.Get("Location"), "One-Product-App://porsche-id/oauth2redirect?"); ok {
rawQuery = location
}
query, err := url.ParseQuery(rawQuery)
query, err := url.ParseQuery(resp.Request.URL.RawQuery)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,15 +202,3 @@ func (v *emobilityAdapter) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error)
}
return token, err
}

type mobileAdapter struct {
tr *Identity
}

func (v *mobileAdapter) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
token, err := v.tr.RefreshToken(nil)
if err == nil {
token = v.tr.mobileToken
}
return token, err
}
Loading

0 comments on commit fdb6bd9

Please sign in to comment.