forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathidentity.go
232 lines (195 loc) · 5.61 KB
/
identity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package porsche
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/oauth"
"github.com/evcc-io/evcc/util/request"
cv "github.com/nirasan/go-oauth-pkce-code-verifier"
"golang.org/x/net/publicsuffix"
"golang.org/x/oauth2"
)
const (
OAuthURI = "https://login.porsche.com"
)
// https://login.porsche.com/.well-known/openid-configuration
var (
OAuth2Config = &oauth2.Config{
ClientID: "4mPO3OE5Srjb1iaUGWsbqKBvvesya8oA",
RedirectURL: "https://my.porsche.com/core/de/de_DE/",
Endpoint: oauth2.Endpoint{
AuthURL: OAuthURI + "/as/authorization.oauth2",
TokenURL: OAuthURI + "/as/token.oauth2",
},
Scopes: []string{"openid"},
}
EmobilityOAuth2Config = &oauth2.Config{
ClientID: "NJOxLv4QQNrpZnYQbb7mCvdiMxQWkHDq",
RedirectURL: "https://my.porsche.com/myservices/auth/auth.html",
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
}
// NewIdentity creates Porsche identity
func NewIdentity(log *util.Logger, user, password string) *Identity {
v := &Identity{
Helper: request.NewHelper(log),
user: user,
password: password,
}
return v
}
func (v *Identity) Login() error {
_, err := v.RefreshToken(nil)
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
}
// RefreshToken performs new login and creates default and emobility tokens
func (v *Identity) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
return nil, err
}
// track cookies and follow all (>10) redirects
v.Client.Jar = jar
v.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return nil
}
defer func() {
v.Client.Jar = nil
v.Client.CheckRedirect = nil
}()
// get the login page
uri := fmt.Sprintf("%s/auth/api/v1/de/de_DE/public/login", OAuthURI)
resp, err := v.Get(uri)
if err != nil {
return nil, err
}
resp.Body.Close()
query, err := url.ParseQuery(resp.Request.URL.RawQuery)
if err != nil {
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},
"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 {
return nil, err
}
resp.Body.Close()
// get the token for the generic API
token, err := v.fetchToken(OAuth2Config)
if err == nil {
v.defaultToken = token
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
}
func (v *Identity) fetchToken(oc *oauth2.Config) (*oauth2.Token, error) {
cv, err := cv.CreateCodeVerifier()
if err != nil {
return nil, err
}
uri := oc.AuthCodeURL("uvobn7XJs1", oauth2.AccessTypeOffline,
oauth2.SetAuthURLParam("code_challenge", cv.CodeChallengeS256()),
oauth2.SetAuthURLParam("code_challenge_method", "S256"),
oauth2.SetAuthURLParam("country", "de"),
oauth2.SetAuthURLParam("locale", "de_DE"),
)
v.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if req.URL.Scheme != "https" {
return http.ErrUseLastResponse
}
return nil
}
resp, err := v.Client.Get(uri)
if err != nil {
return nil, err
}
resp.Body.Close()
rawQuery := resp.Request.URL.RawQuery
if strings.HasPrefix(resp.Header.Get("Location"), "One-Product-App") {
rawQuery = strings.Replace(resp.Header.Get("Location"), "One-Product-App://porsche-id/oauth2redirect?", "", 1)
}
query, err := url.ParseQuery(rawQuery)
if err != nil {
return nil, err
}
code := query.Get("code")
if code == "" {
return nil, errors.New("no auth code")
}
ctx, cancel := context.WithTimeout(
context.WithValue(context.Background(), oauth2.HTTPClient, v.Client),
request.Timeout,
)
defer cancel()
token, err := oc.Exchange(ctx, code,
oauth2.SetAuthURLParam("code_verifier", cv.CodeChallengePlain()),
)
return token, err
}
type emobilityAdapter struct {
tr *Identity
}
func (v *emobilityAdapter) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
token, err := v.tr.RefreshToken(nil)
if err == nil {
token = v.tr.emobilityToken
}
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
}