/
authentication.go
229 lines (212 loc) · 7.15 KB
/
authentication.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
package authentication
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
"github.com/arxdsilva/golang-ifood-sdk/adapters"
"github.com/kpango/glg"
)
const (
authRoot = "/authentication/v1.0"
authEndpoint = "/oauth/token"
userCodeEndpoint = "/oauth/userCode"
valueGrantType = "password"
)
// ErrUnauthorized API no auth error
var ErrUnauthorized = errors.New("Unauthorized")
var ErrGrantType = errors.New("Grant type is invalid, should be 'client_credentials', 'authorization_code' or 'refresh_token'")
var ErrNoRefreshToken = errors.New("Grant type 'refresh_token', should have a refresh token provided")
var ErrNoAuthCodeOrVerifier = errors.New("Grant type 'authorization_code', should have both 'authorizationCode' and 'authorizationCodeVerifier' provided")
type (
// Service describes the auth service abstraction
Service interface {
V2GetUserCode() (*UserCode, error)
Authenticate(username, password string) (*Credentials, error)
V2Authenticate(authType, authCode, authCodeVerifier, refreshToken string) (c *V2Credentials, err error)
Validate() error
GetToken() string
}
// Credentials describes the API credential type
Credentials struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
}
// V2Credentials describes the API credential type
V2Credentials struct {
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
Type string `json:"type"`
ExpiresIn int `json:"expires_in"`
}
authError struct {
Error struct {
Code string `json:"code"`
Message string `json:"error"`
} `json:"error"`
}
UserCode struct {
Usercode string `json:"userCode"`
AuthorizationCodeVerifier string `json:"authorizationCodeVerifier"`
VerificationURL string `json:"verificationUrl"`
VerificationURLComplete string `json:"verificationUrlComplete"`
ExpiresIn int `json:"expiresIn"`
}
authService struct {
adapter adapters.Http
clientId, clientSecret string
username, password string
currentExpiration time.Time
Token string
refreshToken string
v2 bool
}
)
// New returns an auth service implementation
func New(adapter adapters.Http, clientId, clientSecret string, v2 bool) Service {
return &authService{adapter: adapter, clientId: clientId, clientSecret: clientSecret, v2: v2}
}
func (a *authService) V2GetUserCode() (uc *UserCode, err error) {
params := url.Values{}
params.Add("clientId", a.clientId)
body := strings.NewReader(params.Encode())
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
resp, status, err := a.adapter.DoRequest(http.MethodPost, authRoot+userCodeEndpoint, body, headers)
if err != nil {
glg.Error("[SDK] (V2GetUserCode::DoRequest) error: ", err.Error())
return
}
if status != http.StatusOK {
glg.Debug("[SDK] (V2GetUserCode::status.check): resp ", string(resp))
glg.Warn("[SDK] (V2GetUserCode::status.check): status code ", status)
err = ErrUnauthorized
return
}
if err = json.Unmarshal(resp, &uc); err != nil {
glg.Error("[SDK] (V2GetUserCode::Unmarshal) error: ", err)
return
}
glg.Info("[SDK] V2GetUserCode success")
return
}
// V2Authenticate queries the iFood API for a credential
func (a *authService) V2Authenticate(authType, authCode, authCodeVerifier, refreshToken string) (c *V2Credentials, err error) {
if err = verifyV2Inputs(authType, authCode, authCodeVerifier, refreshToken); err != nil {
glg.Error("[SDK] (V2Authenticate::verifyV2Inputs) error: ", err.Error())
return
}
data := url.Values{}
data.Set("client_id", a.clientId)
data.Set("client_secret", a.clientSecret)
data.Set("grant_type", valueGrantType)
data.Set("authorizationCode", authCode)
data.Set("authorizationCodeVerifier", authCodeVerifier)
data.Set("refreshToken", refreshToken)
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
body := strings.NewReader(data.Encode())
resp, status, err := a.adapter.DoRequest(http.MethodPost, authRoot+authEndpoint, body, headers)
if err != nil {
glg.Error("[SDK] (V2Authenticate::DoRequest) error: ", err.Error())
return
}
if status != http.StatusOK {
glg.Warn("[SDK] V2Authenticate: status code ", status)
authErr := authError{}
json.Unmarshal(resp, &authErr)
warn := fmt.Sprintf("[SDK] (V2GetUserCode::status.code): code '%s' message '%s'",
authErr.Error.Code, authErr.Error.Message)
glg.Warn(warn)
err = ErrUnauthorized
return
}
if err = json.Unmarshal(resp, &c); err != nil {
glg.Error("[SDK] (V2Authenticate::Unmarshal) error: ", err)
return
}
glg.Info("[SDK] (V2Authenticate) success")
a.currentExpiration = time.Now().Add(time.Hour * 6)
a.Token = c.AccessToken
a.refreshToken = c.RefreshToken
return
}
// Authenticate queries the iFood API for a credential
func (a *authService) Authenticate(username, password string) (c *Credentials, err error) {
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
writer.WriteField("client_id", a.clientId)
writer.WriteField("client_secret", a.clientSecret)
writer.WriteField("grant_type", valueGrantType)
writer.WriteField("username", username)
writer.WriteField("password", password)
if err = writer.Close(); err != nil {
glg.Error("[SDK] (Auth::writer.Close) error: ", err.Error())
return
}
reader := bytes.NewReader(payload.Bytes())
headers := make(map[string]string)
headers["Content-Type"] = writer.FormDataContentType()
headers["Accept"] = "*/*"
resp, status, err := a.adapter.DoRequest(http.MethodPost, authEndpoint, reader, headers)
if err != nil {
glg.Error("[SDK] (Auth::DoRequest) error: ", err.Error())
return
}
if status != http.StatusOK {
glg.Warn("[SDK] (Auth::status.code): status code ", status)
err = ErrUnauthorized
return
}
if err = json.Unmarshal(resp, &c); err != nil {
glg.Error("[SDK] (Auth::Unmarshal) error: ", err)
return
}
glg.Info("[SDK] (Authenticate) success")
a.currentExpiration = time.Now().Add(time.Hour)
a.username = username
a.password = password
a.Token = c.AccessToken
return
}
// Validate validates or renews a token auth
func (a *authService) Validate() (err error) {
if !time.Now().After(a.currentExpiration) {
glg.Debug("[SDK] (auth::Validate) not time")
return
}
glg.Info("[SDK] (auth::Validate) Renewing Auth")
if a.v2 {
_, err = a.V2Authenticate("refresh_token", "", "", a.refreshToken)
return
}
_, err = a.Authenticate(a.username, a.password)
return
}
// GetToken returns the last valid token
func (a *authService) GetToken() (token string) {
return a.Token
}
func verifyV2Inputs(authType, authCode, authCodeVerifier, refreshToken string) (err error) {
if (authType != "client_credentials") && (authType != "authorization_code") && (authType != "refresh_token") {
return ErrGrantType
}
switch authType {
case "authorization_code":
if authCode == "" || authCodeVerifier == "" {
return ErrNoAuthCodeOrVerifier
}
case "refresh_token":
if refreshToken == "" {
return ErrNoRefreshToken
}
}
return
}