forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 12
/
oauth_gateway.go
52 lines (46 loc) · 1.43 KB
/
oauth_gateway.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
package braintree
import (
"context"
"encoding/xml"
)
type OauthGateway struct {
*Braintree
}
type OAuthCredentialRequest struct {
XMLName xml.Name `xml:"credentials"`
Code string `xml:"code"`
Scope string `xml:"scope"`
GrantType string `xml:"grantType"`
RefreshToken string `xml:"refreshToken"`
}
type OAuthCredentials struct {
AccessToken string `xml:"access-token"`
RefreshToken string `xml:"refresh-token"`
TokenType string `xml:"token-type"`
Scope string `xml:"scope"`
ExpiresAt string `xml:"expires-at"`
}
func (g *OauthGateway) CreateTokenFromCode(ctx context.Context, oAuthCredentialRequest *OAuthCredentialRequest) (*OAuthCredentials, error) {
oAuthCredentialRequest.GrantType = "authorization_code"
resp, err := g.executeVersion(ctx, "POST", "oauth/access_tokens", oAuthCredentialRequest, apiVersion4)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.oauth()
}
return nil, &invalidResponseError{resp}
}
func (g *OauthGateway) CreateTokenFromRefreshToken(ctx context.Context, oAuthCredentialRequest *OAuthCredentialRequest) (*OAuthCredentials, error) {
oAuthCredentialRequest.GrantType = "refresh_token"
resp, err := g.executeVersion(ctx, "POST", "oauth/access_tokens", oAuthCredentialRequest, apiVersion4)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.oauth()
}
return nil, &invalidResponseError{resp}
}