-
Notifications
You must be signed in to change notification settings - Fork 1
/
identity.go
205 lines (160 loc) · 4.83 KB
/
identity.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
package gopaypal
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
type IdentityAccessTokenResponse struct {
baseURL string `json:"-"`
TokenType string `json:"token_type"`
Expires string `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
}
type IdentityUserInfoResponse struct {
UserID string `json:"user_id"`
Sub string `json:"sub"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
MiddleName string `json:"middle_name"`
Picture string `json:"picture"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
Gender string `json:"gender"`
BirthDate string `json:"birthdate"`
ZoneInfo string `json:"zoneinfo"`
Locale string `json:"locale"`
PhoneNumber string `json:"phone_number"`
AccountVerified bool `json:"verified_account"`
Address IdentityAddress `json:"address"`
AccountType string `json:"account_type"`
AgeRange string `json:"age_range"`
PayerID string `json:"payer_id"`
}
type IdentityAddress struct {
StreetAddress string `json:"street_address"`
Locality string `json:"locality"`
Region string `json:"region"`
PostalCode string `json:"postal_code"`
Country string `json:"country"`
}
// GenerateIdentityURL creates and returns an identity URL used to log-in into the PayPal services
func (c Client) GenerateIdentityURL(state string, ret string, scope []string) (*url.URL, error) {
// Change client base URL to meet identity one
b := c.baseURL
if c.baseURL == SandBoxURL {
c.baseURL = IdentitySandBoxURL
} else {
c.baseURL = IdentityLiveURL
}
// Create base URL
idenURL, err := url.Parse(c.baseURL + IdentityURL)
if err != nil {
return nil, err
}
c.baseURL = b
// Get URL query
query := idenURL.Query()
// Set client ID
query.Add("client_id", c.clientID)
// Set response type
query.Add("response_type", "token")
// Set scope
query.Add("scope", strings.Join(scope, "+"))
// Set state
query.Add("state", state)
// Set nonce value
query.Add("nonce", CreateNonce())
// Set return URL
query.Add("redirect_uri", ret)
// Set URL query
idenURL.RawQuery = query.Encode()
return idenURL, nil
}
// GetTokenFromRefreshToken returns the access token from the given identity refresh token
func (c *Client) GetTokenFromRefreshToken(refresh string) (*IdentityAccessTokenResponse, error) {
// Set grant_type
buff := []byte(fmt.Sprintf(
"grant_type=%v&refresh_token=%v",
"refresh_token",
refresh,
))
// Create new gopaypal basic request
req, err := c.BasicRequest(IdentityTokenURL, buff, http.MethodPost)
if err != nil {
return nil, err
}
// Set basic HTTP authentication
req.SetBasicAuth(c.clientID, c.secret)
// Execute request
res, err := c.Execute(req)
if err != nil {
return nil, err
}
// Convert response to JSON object
resp := IdentityAccessTokenResponse{
baseURL: c.baseURL,
}
// Unmarshal response
if err := json.Unmarshal(res, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// GetTokenFromIdentityCode returns the access token from the given identity login code
func (c *Client) GetTokenFromIdentityCode(code, ret string) (*IdentityAccessTokenResponse, error) {
// Set grant_type
buff := []byte(fmt.Sprintf(
"grant_type=%v&code=%v&redirect_uri=%v",
"authorization_code",
code,
ret,
))
// Create new gopaypal basic request
req, err := c.BasicRequest(IdentityTokenURL, buff, http.MethodPost)
if err != nil {
return nil, err
}
// Set basic HTTP authentication
req.SetBasicAuth(c.clientID, c.secret)
// Execute request
res, err := c.Execute(req)
if err != nil {
return nil, err
}
// Convert response to JSON object
resp := IdentityAccessTokenResponse{
baseURL: c.baseURL,
}
// Unmarshal response
if err := json.Unmarshal(res, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// GetUserInfo gets user profile attributes by the given access token
func (c Client) GetUserInfo(tkn string) (*IdentityUserInfoResponse, error) {
// Create new gopaypal basic request
req, err := c.BasicRequest(IdentityUserInfoURL, nil, http.MethodPost)
if err != nil {
return nil, err
}
// Set content type header
req.Header.Set("Content-Type", "application/json")
// Set authorization header
req.Header.Set("Authorization", "Bearer "+tkn)
// Execute request
res, err := c.Execute(req)
if err != nil {
return nil, err
}
resUserInfo := IdentityUserInfoResponse{}
// Unmarshal response
if err := json.Unmarshal(res, &resUserInfo); err != nil {
return nil, err
}
return &resUserInfo, nil
}