forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentity.go
215 lines (180 loc) Β· 5.26 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
package fiat
import (
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/aws/aws-sdk-go/service/cognitoidentity"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/samber/lo"
)
const (
LoginURI = "https://loginmyuconnect.fiat.com"
TokenURI = "https://authz.sdpr-01.fcagcv.com/v2/cognito/identity/token"
Region = "eu-west-1"
)
type Identity struct {
*request.Helper
user, password string
uid string
creds *cognitoidentity.Credentials
}
// NewIdentity creates Fiat identity
func NewIdentity(log *util.Logger, user, password string) *Identity {
return &Identity{
Helper: request.NewHelper(log),
user: user,
password: password,
}
}
// Login authenticates with username/password to get new aws credentials
func (v *Identity) Login() error {
v.Client.Jar, _ = cookiejar.New(nil)
uri := fmt.Sprintf("%s/accounts.webSdkBootstrap", LoginURI)
data := url.Values{
"APIKey": {ApiKey},
"pageURL": {"https://myuconnect.fiat.com/de/de/vehicle-services"},
"sdk": {"js_latest"},
"sdkBuild": {"12234"},
"format": {"json"},
}
headers := map[string]string{
"Accept": "*/*",
}
req, err := request.New(http.MethodGet, uri, nil, headers)
if err == nil {
req.URL.RawQuery = data.Encode()
var resp *http.Response
if resp, err = v.Do(req); err == nil {
resp.Body.Close()
}
}
var res struct {
ErrorInfo
UID string
StatusReason string
SessionInfo struct {
LoginToken string `json:"login_token"`
ExpiresIn string `json:"expires_in"`
}
}
if err == nil {
uri = fmt.Sprintf("%s/accounts.login", LoginURI)
data := url.Values{
"loginID": {v.user},
"password": {v.password},
"sessionExpiration": {"7776000"},
"APIKey": {ApiKey},
"pageURL": {"https://myuconnect.fiat.com/de/de/login"},
"sdk": {"js_latest"},
"sdkBuild": {"12234"},
"format": {"json"},
"targetEnv": {"jssdk"},
"include": {"profile,data,emails"}, // subscriptions,preferences
"includeUserInfo": {"true"},
"loginMode": {"standard"},
"lang": {"de0de"},
"source": {"showScreenSet"},
"authMode": {"cookie"},
}
headers := map[string]string{
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded",
}
if req, err = request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), headers); err == nil {
if err = v.DoJSON(req, &res); err == nil {
err = res.ErrorInfo.Error()
v.uid = res.UID
}
}
}
var token struct {
ErrorInfo
StatusReason string
IDToken string `json:"id_token"`
}
if err == nil {
uri = fmt.Sprintf("%s/accounts.getJWT", LoginURI)
data := url.Values{
"fields": {"profile.firstName,profile.lastName,profile.email,country,locale,data.disclaimerCodeGSDP"}, // data.GSDPisVerified
"APIKey": {ApiKey},
"pageURL": {"https://myuconnect.fiat.com/de/de/dashboard"},
"sdk": {"js_latest"},
"sdkBuild": {"12234"},
"format": {"json"},
"login_token": {res.SessionInfo.LoginToken},
"authMode": {"cookie"},
}
headers := map[string]string{
"Accept": "*/*",
}
if req, err = request.New(http.MethodGet, uri, nil, headers); err == nil {
req.URL.RawQuery = data.Encode()
if err = v.DoJSON(req, &token); err == nil {
err = token.ErrorInfo.Error()
}
}
}
var identity struct {
Token, IdentityID string
}
if err == nil {
data := struct {
GigyaToken string `json:"gigya_token"`
}{
GigyaToken: token.IDToken,
}
headers := map[string]string{
"Content-Type": "application/json",
"X-Clientapp-Version": "1.0",
"ClientRequestId": lo.RandomString(16, lo.LettersCharset),
"X-Api-Key": XApiKey,
"X-Originator-Type": "web",
}
if req, err = request.New(http.MethodPost, TokenURI, request.MarshalJSON(data), headers); err == nil {
err = v.DoJSON(req, &identity)
}
}
var credRes *cognitoidentity.GetCredentialsForIdentityOutput
if err == nil {
session := session.Must(session.NewSession(&aws.Config{Region: aws.String(Region)}))
svc := cognitoidentity.New(session)
credRes, err = svc.GetCredentialsForIdentity(&cognitoidentity.GetCredentialsForIdentityInput{
IdentityId: &identity.IdentityID,
Logins: map[string]*string{
"cognito-identity.amazonaws.com": &identity.Token,
},
})
}
if err == nil {
v.creds = credRes.Credentials
}
return err
}
// UID returns the logged in users uid
func (v *Identity) UID() string {
return v.uid
}
// Sign signs an AWS request using identity's credentials
func (v *Identity) Sign(req *http.Request, body io.ReadSeeker) error {
// refresh credentials
if v.creds.Expiration.Before(time.Now().Add(-time.Minute)) {
if err := v.Login(); err != nil {
return fmt.Errorf("login failed: %w", err)
}
}
// sign request
signer := v4.NewSigner(credentials.NewStaticCredentials(
*v.creds.AccessKeyId, *v.creds.SecretKey, *v.creds.SessionToken,
))
_, err := signer.Sign(req, body, "execute-api", Region, time.Now())
return err
}