-
Notifications
You must be signed in to change notification settings - Fork 1
/
authorization.go
executable file
·164 lines (136 loc) · 5.46 KB
/
authorization.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
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// AuthToken is Authorization token flow
type AuthToken struct {
// The client ID of your application that was generated when you [registered it](https://developer.sumup.com/docs/register-app).
ClientId string `json:"client_id"`
// The client secret of your application that was generated when you [registered it](https://developer.sumup.com/docs/register-app).
ClientSecret string `json:"client_secret"`
// The authorization code that you received from requesting an authorization code.
Code string `json:"code"`
// The grant type used for obtaining an access token.
GrantType AuthTokenGrantType `json:"grant_type"`
// A **required** parameter when generating a refresh token.
RefreshToken *string `json:"refresh_token,omitempty"`
}
// The grant type used for obtaining an access token.
type AuthTokenGrantType string
const (
AuthTokenGrantTypeAuthorizationCode AuthTokenGrantType = "authorization_code"
AuthTokenGrantTypeRefreshToken AuthTokenGrantType = "refresh_token"
)
// CreateToken request body.
type CreateTokenBody struct {
// The client ID of your application that was generated when you [registered it](https://developer.sumup.com/docs/register-app).
ClientId string `json:"client_id"`
// The client secret of your application that was generated when you [registered it](https://developer.sumup.com/docs/register-app).
ClientSecret string `json:"client_secret"`
// The authorization code that you received from requesting an authorization code.
Code string `json:"code"`
// The grant type used for obtaining an access token.
GrantType CreateTokenBodyGrantType `json:"grant_type"`
// A **required** parameter when generating a refresh token.
RefreshToken *string `json:"refresh_token,omitempty"`
}
// The grant type used for obtaining an access token.
type CreateTokenBodyGrantType string
const (
CreateTokenBodyGrantTypeAuthorizationCode CreateTokenBodyGrantType = "authorization_code"
CreateTokenBodyGrantTypeRefreshToken CreateTokenBodyGrantType = "refresh_token"
)
// CreateTokenResponse is the type definition for a CreateTokenResponse.
type CreateTokenResponse struct {
// The access token that you need to use in your requests to the SumUp API.
AccessToken *string `json:"access_token,omitempty"`
// The validity of the access token in seconds.
ExpiresIn *int `json:"expires_in,omitempty"`
// The refresh token provided in the request call
RefreshToken *string `json:"refresh_token,omitempty"`
// List of authorization scopes granted to your access token.
Scope *string `json:"scope,omitempty"`
// The type of the token. The value is always `Bearer`.
TokenType *string `json:"token_type,omitempty"`
}
// AuthorizeParams are query parameters for Authorize
type AuthorizeParams struct {
ClientId *string `json:"client_id,omitempty"`
RedirectUri *string `json:"redirect_uri,omitempty"`
ResponseType *string `json:"response_type,omitempty"`
Scope *string `json:"scope,omitempty"`
State *string `json:"state,omitempty"`
}
// AuthorizeResponse is the type definition for a AuthorizeResponse.
type AuthorizeResponse struct {
}
type AuthorizationService service
// CreateToken: Generate a token
// Generate a token or a refresh token
func (s *AuthorizationService) CreateToken(ctx context.Context, body CreateTokenBody) (*CreateTokenResponse, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/token")
req, err := s.client.NewRequest(ctx, http.MethodPost, path, buf)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v CreateTokenResponse
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// Authorize: Request authorization from users
// Request authorization from users and grant your application access to resources associated with the user's profile.
func (s *AuthorizationService) Authorize(ctx context.Context, params AuthorizeParams) (*AuthorizeResponse, error) {
path := fmt.Sprintf("/authorize")
req, err := s.client.NewRequest(ctx, http.MethodGet, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v AuthorizeResponse
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}