This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
forked from omniboost/xerogolang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xerogolang.go
443 lines (378 loc) · 13.3 KB
/
xerogolang.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package xerogolang
import (
"bytes"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"crypto"
"github.com/XeroAPI/xerogolang/auth"
"github.com/XeroAPI/xerogolang/helpers"
"github.com/markbates/goth"
"github.com/mrjones/oauth"
"golang.org/x/oauth2"
)
var (
requestURL = "https://api.xero.com/oauth/RequestToken"
authorizeURL = "https://api.xero.com/oauth/Authorize"
tokenURL = "https://api.xero.com/oauth/AccessToken"
endpointProfile = "https://api.xero.com/api.xro/2.0/"
//userAgentString should match the name of your Application
userAgentString = os.Getenv("XERO_USER_AGENT") + " (xerogolang 0.1.2) " + os.Getenv("XERO_KEY")
//privateKeyFilePath is a file path to your .pem private/public key file
//You only need this for private and partner Applications
//more details here: https://developer.xero.com/documentation/api-guides/create-publicprivate-key
privateKeyFilePath = os.Getenv("XERO_PRIVATE_KEY_PATH")
)
// Provider is the implementation of `goth.Provider` for accessing Xero.
type Provider struct {
ClientKey string
Secret string
CallbackURL string
HTTPClient *http.Client
Method string
UserAgentString string
PrivateKey string
debug bool
consumer *oauth.Consumer
providerName string
}
//newPublicConsumer creates a consumer capable of communicating with a Public application: https://developer.xero.com/documentation/auth-and-limits/public-applications
func (p *Provider) newPublicConsumer(authURL string) *oauth.Consumer {
var c *oauth.Consumer
if p.HTTPClient != nil {
c = oauth.NewCustomHttpClientConsumer(
p.ClientKey,
p.Secret,
oauth.ServiceProvider{
RequestTokenUrl: requestURL,
AuthorizeTokenUrl: authURL,
AccessTokenUrl: tokenURL},
p.HTTPClient,
)
} else {
c = oauth.NewConsumer(
p.ClientKey,
p.Secret,
oauth.ServiceProvider{
RequestTokenUrl: requestURL,
AuthorizeTokenUrl: authURL,
AccessTokenUrl: tokenURL},
)
}
c.Debug(p.debug)
return c
}
//newPartnerConsumer creates a consumer capable of communicating with a Partner application: https://developer.xero.com/documentation/auth-and-limits/partner-applications
func (p *Provider) newPrivateOrPartnerConsumer(authURL string) *oauth.Consumer {
block, _ := pem.Decode([]byte(p.PrivateKey))
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatal(err)
}
var c *oauth.Consumer
if p.HTTPClient != nil {
c = oauth.NewCustomRSAConsumer(
p.ClientKey,
privateKey,
crypto.SHA1,
oauth.ServiceProvider{
RequestTokenUrl: requestURL,
AuthorizeTokenUrl: authURL,
AccessTokenUrl: tokenURL},
p.HTTPClient,
)
} else {
c = oauth.NewRSAConsumer(
p.ClientKey,
privateKey,
oauth.ServiceProvider{
RequestTokenUrl: requestURL,
AuthorizeTokenUrl: authURL,
AccessTokenUrl: tokenURL},
)
}
c.Debug(p.debug)
return c
}
// New creates a new Xero provider, and sets up important connection details.
// You should always call `xero.New` to get a new Provider. Never try to create
// one manually.
func New(clientKey, secret, callbackURL string) *Provider {
p := &Provider{
ClientKey: clientKey,
Secret: secret,
CallbackURL: callbackURL,
//Method determines how you will connect to Xero.
//Options are public, private, and partner
//Use public if this is your first time.
//More details here: https://developer.xero.com/documentation/getting-started/api-application-types
Method: os.Getenv("XERO_METHOD"),
PrivateKey: helpers.ReadPrivateKeyFromPath(privateKeyFilePath),
UserAgentString: userAgentString,
providerName: "xero",
}
return p
}
// New creates a new Xero provider, with a custom http client
func NewCustomHTTPClient(clientKey, secret, callbackURL string, httpClient *http.Client) *Provider {
p := &Provider{
ClientKey: clientKey,
Secret: secret,
CallbackURL: callbackURL,
Method: os.Getenv("XERO_METHOD"),
PrivateKey: helpers.ReadPrivateKeyFromPath(privateKeyFilePath),
UserAgentString: userAgentString,
providerName: "xero",
HTTPClient: httpClient,
}
return p
}
// Name is the name used to retrieve this provider later.
func (p *Provider) Name() string {
return p.providerName
}
// SetName is to update the name of the provider (needed in case of multiple providers of 1 type)
func (p *Provider) SetName(name string) {
p.providerName = name
}
// Client does pretty much everything
func (p *Provider) Client() *http.Client {
return goth.HTTPClientWithFallBack(p.HTTPClient)
}
// Debug sets the logging of the OAuth client to verbose.
func (p *Provider) Debug(debug bool) {
p.debug = debug
}
// BeginAuth asks Xero for an authentication end-point and a request token for a session.
// Xero does not support the "state" variable.
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
if p.consumer == nil {
p.initConsumer()
}
if p.Method == "private" {
accessToken := &oauth.AccessToken{
Token: p.ClientKey,
Secret: p.Secret,
}
privateSession := &Session{
AuthURL: authorizeURL,
RequestToken: nil,
AccessToken: accessToken,
AccessTokenExpires: time.Now().UTC().Add(87600 * time.Hour),
}
return privateSession, nil
}
requestToken, url, err := p.consumer.GetRequestTokenAndUrl(p.CallbackURL)
if err != nil {
return nil, err
}
session := &Session{
AuthURL: url,
RequestToken: requestToken,
}
return session, nil
}
//processRequest processes a request prior to it being sent to the API
func (p *Provider) processRequest(request *http.Request, session goth.Session, additionalHeaders map[string]string) ([]byte, error) {
sess := session.(*Session)
if p.consumer == nil {
p.initConsumer()
}
if sess.AccessToken == nil {
// data is not yet retrieved since accessToken is still empty
return nil, fmt.Errorf("%s cannot process request without accessToken", p.providerName)
}
request.Header.Add("User-Agent", p.UserAgentString)
for key, value := range additionalHeaders {
request.Header.Add(key, value)
}
var err error
var response *http.Response
if p.HTTPClient == nil {
client, _ := p.consumer.MakeHttpClient(sess.AccessToken)
response, err = client.Do(request)
} else {
transport, _ := p.consumer.MakeRoundTripper(sess.AccessToken)
response, err = transport.RoundTrip(request)
}
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
helpers.ReaderToString(response.Body),
)
}
responseBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("Could not read response: %s", err.Error())
}
if responseBytes == nil {
return nil, fmt.Errorf("Received no response: %s", err.Error())
}
return responseBytes, nil
}
//Find retrieves the requested data from an endpoint to be unmarshaled into the appropriate data type
func (p *Provider) Find(session goth.Session, endpoint string, additionalHeaders map[string]string, querystringParameters map[string]string) ([]byte, error) {
var querystring string
if querystringParameters != nil {
for key, value := range querystringParameters {
escapedValue := url.QueryEscape(value)
querystring = querystring + "&" + key + "=" + escapedValue
}
querystring = strings.TrimPrefix(querystring, "&")
querystring = "?" + querystring
}
request, err := http.NewRequest("GET", endpointProfile+endpoint+querystring, nil)
if err != nil {
return nil, err
}
return p.processRequest(request, session, additionalHeaders)
}
//Create sends data to an endpoint and returns a response to be unmarshaled into the appropriate data type
func (p *Provider) Create(session goth.Session, endpoint string, additionalHeaders map[string]string, body []byte) ([]byte, error) {
bodyReader := bytes.NewReader(body)
request, err := http.NewRequest("PUT", endpointProfile+endpoint, bodyReader)
if err != nil {
return nil, err
}
return p.processRequest(request, session, additionalHeaders)
}
//Update sends data to an endpoint and returns a response to be unmarshaled into the appropriate data type
func (p *Provider) Update(session goth.Session, endpoint string, additionalHeaders map[string]string, body []byte) ([]byte, error) {
bodyReader := bytes.NewReader(body)
request, err := http.NewRequest("POST", endpointProfile+endpoint, bodyReader)
if err != nil {
return nil, err
}
return p.processRequest(request, session, additionalHeaders)
}
//Remove deletes the specified data from an endpoint
func (p *Provider) Remove(session goth.Session, endpoint string, additionalHeaders map[string]string) ([]byte, error) {
request, err := http.NewRequest("DELETE", endpointProfile+endpoint, nil)
if err != nil {
return nil, err
}
return p.processRequest(request, session, additionalHeaders)
}
//Organisation is the expected response from the Organisation endpoint - this is not a complete schema
//and should only be used by FetchUser
type Organisation struct {
// Display name of organisation shown in Xero
Name string `json:"Name,omitempty"`
// Organisation name shown on Reports
LegalName string `json:"LegalName,omitempty"`
// Organisation Type
OrganisationType string `json:"OrganisationType,omitempty"`
// Country code for organisation. See ISO 3166-2 Country Codes
CountryCode string `json:"CountryCode,omitempty"`
// A unique identifier for the organisation.
ShortCode string `json:"ShortCode,omitempty"`
}
//OrganisationCollection is the Total response from the Xero API
type OrganisationCollection struct {
Organisations []Organisation `json:"Organisations,omitempty"`
}
// FetchUser will go to Xero and access basic information about the user.
func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
sess := session.(*Session)
user := goth.User{
Provider: p.Name(),
}
additionalHeaders := map[string]string{
"Accept": "application/json",
}
responseBytes, err := p.Find(sess, "Organisation", additionalHeaders, nil)
if err != nil {
return user, err
}
var organisationCollection OrganisationCollection
err = json.Unmarshal(responseBytes, &organisationCollection)
if err != nil {
return user, fmt.Errorf("Could not unmarshal response: %s", err.Error())
}
user.Name = organisationCollection.Organisations[0].Name
user.NickName = organisationCollection.Organisations[0].LegalName
user.Location = organisationCollection.Organisations[0].CountryCode
user.Description = organisationCollection.Organisations[0].OrganisationType
user.UserID = organisationCollection.Organisations[0].ShortCode
user.AccessToken = sess.AccessToken.Token
user.AccessTokenSecret = sess.AccessToken.Secret
user.ExpiresAt = sess.AccessTokenExpires
user.Email = p.Method
return user, err
}
//RefreshOAuth1Token should be used instead of RefeshToken which is not compliant with the Oauth1.0a standard
func (p *Provider) RefreshOAuth1Token(session *Session) error {
if p.consumer == nil {
p.initConsumer()
}
if session.AccessToken == nil {
return fmt.Errorf("Could not refresh token as last valid accessToken was not found")
}
newAccessToken, err := p.consumer.RefreshToken(session.AccessToken)
if err != nil {
return err
}
session.AccessToken = newAccessToken
session.AccessTokenExpires = time.Now().UTC().Add(30 * time.Minute)
return nil
}
//RefreshToken refresh token is not provided by the Xero Public or Private Application -
//only the Partner Application and you must use RefreshOAuth1Token instead
func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) {
return nil, errors.New("Refresh token is only provided by Xero for Partner Applications")
}
//RefreshTokenAvailable refresh token is not provided by the Xero Public or Private Application -
//only the Partner Application and you must use RefreshOAuth1Token instead
func (p *Provider) RefreshTokenAvailable() bool {
return false
}
//GetSessionFromStore returns a session for a given a request and a response
//This is an exaple of how you could get a session from a store - as long as you're
//supplying a goth.Session to the interactors it will work though so feel free to use your
//own method
func (p *Provider) GetSessionFromStore(request *http.Request, response http.ResponseWriter) (goth.Session, error) {
sessionMarshalled, _ := auth.Store.Get(request, "xero"+auth.SessionName)
value := sessionMarshalled.Values["xero"]
if value == nil {
return nil, errors.New("could not find a matching session for this request")
}
session, err := p.UnmarshalSession(value.(string))
if err != nil {
return nil, errors.New("could not unmarshal session for this request")
}
sess := session.(*Session)
if sess.AccessTokenExpires.Before(time.Now().UTC().Add(5 * time.Minute)) {
if p.Method == "partner" {
p.RefreshOAuth1Token(sess)
sessionMarshalled.Values["xero"] = sess.Marshal()
err = sessionMarshalled.Save(request, response)
return session, err
}
return nil, errors.New("access token has expired - please reconnect")
}
return session, err
}
func (p *Provider) initConsumer() {
switch p.Method {
case "private":
p.consumer = p.newPrivateOrPartnerConsumer(authorizeURL)
case "public":
p.consumer = p.newPublicConsumer(authorizeURL)
case "partner":
p.consumer = p.newPrivateOrPartnerConsumer(authorizeURL)
default:
p.consumer = p.newPublicConsumer(authorizeURL)
}
}