-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.go
93 lines (77 loc) · 2.42 KB
/
auth.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
package tesla
import (
"fmt"
"net/http"
)
// Authenticate starts the initial authentication process via an OAuth 2.0 Password Grant with the
// same credentials used for tesla.com and the mobile apps.
//
// The current client ID and secret are available at https://pastebin.com/pS7Z6yyP.
//
// We will get back an access_token which is treated as an OAuth 2.0 Bearer Token. This token is
// passed along in an Authorization header with all future requests:
func (c *Conn) Authenticate(email, password string) error {
type request struct {
GrantType string `json:"grant_type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Email string `json:"email"`
Password string `json:"password"`
}
reqBody := request{
GrantType: "password",
ClientID: c.clientID,
ClientSecret: c.clientSecret,
Email: email,
Password: password,
}
type response struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
CreatedAt int `json:"created_at"`
}
var respBody response
err := c.doRequest(http.MethodPost, "/oauth/token", &reqBody, &respBody)
if err != nil {
return err
}
c.accessToken = respBody.AccessToken
c.refreshToken = respBody.RefreshToken
return nil
}
// UpdateRefreshToken will do an OAuth 2.0 Refresh Token Grant and obtain a new access token. Note:
// This will invalidate the previous access token.
func (c *Conn) UpdateRefreshToken() error {
if c.refreshToken == "" {
return fmt.Errorf("%w", ErrMissingRefreshToken)
}
type request struct {
GrantType string `json:"grant_type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RefreshToken string `json:"refresh_token"`
}
reqBody := request{
GrantType: "refresh_token",
ClientID: c.clientID,
ClientSecret: c.clientSecret,
RefreshToken: c.refreshToken,
}
type response struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
CreatedAt int `json:"created_at"`
}
var respBody response
err := c.doRequest(http.MethodPost, "/oauth/token", &reqBody, &respBody)
if err != nil {
return err
}
c.accessToken = respBody.AccessToken
c.refreshToken = respBody.RefreshToken
return nil
}