forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth_gateway.go
More file actions
57 lines (46 loc) · 1.48 KB
/
oauth_gateway.go
File metadata and controls
57 lines (46 loc) · 1.48 KB
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
package braintree
import (
"context"
"encoding/xml"
)
type OAuthGateway struct {
*Braintree
}
type (
OAuthCreateTokenFromCodeRequest struct {
XMLName xml.Name `xml:"credentials"`
Code string `xml:"code"`
GrantType string `xml:"grant_type"`
}
OAuthRefreshCredentialsRequest struct {
XMLName xml.Name `xml:"credentials"`
RefreshToken string `xml:"refresh_token"`
GrantType string `xml:"grant_type"`
}
)
// CreateTokenFromCode creates an OAuth credentials from an authorization code.
func (g *OAuthGateway) CreateTokenFromCode(ctx context.Context, code string) (*OAuthCredentials, error) {
req := OAuthCreateTokenFromCodeRequest{Code: code, GrantType: "authorization_code"}
resp, err := g.executeBaseVersion(ctx, "POST", "oauth/access_tokens", &req, apiVersion6)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.oauthCredentials()
}
return nil, &invalidResponseError{resp}
}
// CreateTokenFromRefreshToken creates an OAuth credentials from a refresh token.
func (g *OAuthGateway) CreateTokenFromRefreshToken(ctx context.Context, refreshToken string) (*OAuthCredentials, error) {
req := OAuthRefreshCredentialsRequest{RefreshToken: refreshToken, GrantType: "refresh_token"}
resp, err := g.executeBaseVersion(ctx, "POST", "oauth/access_tokens", &req, apiVersion6)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.oauthCredentials()
}
return nil, &invalidResponseError{resp}
}