-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
118 lines (93 loc) · 2.99 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
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
package mercadolibre
import (
"errors"
"fmt"
"net/url"
"time"
)
const (
AuthURLMLA = "https://auth.mercadolibre.com.ar" // Argentina
AuthURLMLB = "https://auth.mercadolivre.com.br" // Brasil
AuthURLMCO = "https://auth.mercadolibre.com.co" // Colombia
AuthURLMCR = "https://auth.mercadolibre.com.cr" // Costa Rica
AuthURLMEC = "https://auth.mercadolibre.com.ec" // Ecuador
AuthURLMLC = "https://auth.mercadolibre.cl" // Chile
AuthURLMLM = "https://auth.mercadolibre.com.mx" // Mexico
AuthURLMLU = "https://auth.mercadolibre.com.uy" // Uruguay
AuthURLMLV = "https://auth.mercadolibre.com.ve" // Venezuela
AuthURLMPA = "https://auth.mercadolibre.com.pa" // Panama
AuthURLMPE = "https://auth.mercadolibre.com.pe" // Peru
AuthURLMPT = "https://auth.mercadolivre.pt" // Portugal
AuthURLMRD = "https://auth.mercadolibre.com.do" // Dominicana
authorizationCode = "authorization_code"
refreshToken = "refresh_token"
)
func (client *Client) GetAuthURL(site, redirectUri string) (string, error) {
if site != "" {
u := url.URL{}
query := u.Query()
query.Add("response_type", "code")
query.Add("client_id", client.clientId)
query.Add("redirect_uri", redirectUri)
authUrl := fmt.Sprintf("%s/authorization?%s", site, query.Encode())
return authUrl, nil
} else {
return "", errors.New("site URL is required")
}
}
func (client *Client) Authorize(code, redirectUri string) (*AuthResponse, error) {
authReq := authRequest{
GrantType: authorizationCode,
ClientID: client.clientId,
ClientSecret: client.clientSecret,
Code: code,
RedirectURI: redirectUri,
}
authRes := new(AuthResponse)
authErr := new(authError)
_, err := client.sling.New().Post("/oauth/token").QueryStruct(authReq).Receive(authRes, authErr)
if err != nil {
return nil, err
}
if authErr.Status != 0 {
return nil, errors.New(fmt.Sprintf("%s - %s", authErr.Error, authErr.Message))
}
authRes.ReceivedAt = time.Now().Unix()
client.Auth = *authRes
return authRes, nil
}
func (client *Client) RefreshAccessToken() (*AuthResponse, error) {
authReq := authRequest{
GrantType: refreshToken,
ClientID: client.clientId,
ClientSecret: client.clientSecret,
RefreshToken: client.Auth.RefreshToken,
}
authRes := new(AuthResponse)
authErr := new(authError)
_, err := client.sling.New().Post("/oauth/token").QueryStruct(authReq).Receive(authRes, authErr)
if err != nil {
return nil, err
}
if authErr.Status != 0 {
return nil, errors.New(fmt.Sprintf("%s - %s", authErr.Error, authErr.Message))
}
authRes.ReceivedAt = time.Now().Unix()
client.Auth = *authRes
return authRes, nil
}
func (client *Client) IsExpired() bool {
return client.Auth.IsExpired()
}
func (auth AuthResponse) IsExpired() bool {
return (auth.ReceivedAt + int64(auth.ExpiresIn)) <= (time.Now().Unix() + 60)
}
func (auth AuthResponse) getParams() requestParams {
params := requestParams{
AccessToken: auth.AccessToken,
}
return params
}
func (client *Client) SetAuth(auth *AuthResponse) {
client.Auth = *auth
}