-
Notifications
You must be signed in to change notification settings - Fork 6
/
social.go
401 lines (346 loc) · 10.3 KB
/
social.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
package social
import (
"context"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
)
type AuthRequestOptions struct {
Nonce string
Prompt string
MaxAge int
UILocales string
BotPrompt string
}
// GetAcceessToken: Issues access token.
func (client *Client) GetAccessToken(redirectURL, code string) *GetAccessTokenCall {
return &GetAccessTokenCall{
c: client,
redirectURL: redirectURL,
code: code,
}
}
// GetAccessTokenCall type
type GetAccessTokenCall struct {
c *Client
ctx context.Context
redirectURL string
code string
}
// WithContext method
func (call *GetAccessTokenCall) WithContext(ctx context.Context) *GetAccessTokenCall {
call.ctx = ctx
return call
}
// Do method
func (call *GetAccessTokenCall) Do() (*TokenResponse, error) {
data := url.Values{}
// authorization_code. Specifies the grant type.
data.Set("grant_type", "authorization_code")
// Authorization code. Code returned in the authorization request.
data.Set("code", call.code)
data.Set("redirect_uri", call.redirectURL)
data.Set("client_id", call.c.channelID)
data.Set("client_secret", call.c.channelSecret)
res, err := call.c.post(call.ctx, APIEndpointToken, strings.NewReader(data.Encode()))
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToTokenResponse(res)
}
// GetWebLoinURL - LINE LOGIN 2.1 get LINE Login authorization request URL
func (client *Client) GetWebLoinURL(redirectURL string, state string, scope string, options AuthRequestOptions) string {
u, err := url.Parse(APIEndpointAuthBase)
if err != nil {
log.Print(err)
os.Exit(1)
}
u.Path = path.Join(u.Path, APIEndpointAuthorize)
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
log.Print(err)
os.Exit(1)
}
q := req.URL.Query()
q.Add("response_type", "code")
q.Add("redirect_uri", redirectURL)
q.Add("client_id", client.channelID)
q.Add("state", state)
q.Add("scope", scope)
if len(options.Nonce) > 0 {
q.Add("nonce", options.Nonce)
}
if len(options.Prompt) > 0 {
q.Add("prompt", options.Prompt)
}
if len(options.UILocales) > 0 {
q.Add("ui_locales", options.UILocales)
}
if len(options.BotPrompt) > 0 {
q.Add("bot_prompt", options.BotPrompt)
}
req.URL.RawQuery = q.Encode()
return req.URL.String()
}
// GetPKCEWebLoinURL - LINE LOGIN 2.1 get LINE Login authorization request URL by PKCE
func (client *Client) GetPKCEWebLoinURL(redirectURL string, state string, scope string, codeChallenge string, options AuthRequestOptions) string {
u, err := url.Parse(APIEndpointAuthBase)
if err != nil {
log.Print(err)
os.Exit(1)
}
u.Path = path.Join(u.Path, APIEndpointAuthorize)
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
log.Print(err)
os.Exit(1)
}
q := req.URL.Query()
q.Add("response_type", "code")
q.Add("redirect_uri", redirectURL)
q.Add("client_id", client.channelID)
q.Add("state", state)
q.Add("scope", scope)
q.Add("code_challenge", codeChallenge)
q.Add("code_challenge_method", "S256")
if len(options.Nonce) > 0 {
q.Add("nonce", options.Nonce)
}
if len(options.Prompt) > 0 {
q.Add("prompt", options.Prompt)
}
if len(options.UILocales) > 0 {
q.Add("ui_locales", options.UILocales)
}
if len(options.BotPrompt) > 0 {
q.Add("bot_prompt", options.BotPrompt)
}
req.URL.RawQuery = q.Encode()
return req.URL.String()
}
// TokenVerify: Verifies the access token.
// Note: This is the reference for the v2.1 endpoint. For the v2 reference, see Verify access token v2 (https://developers.line.biz/en/reference/social-api-v2/#verify-access-token)
func (client *Client) TokenVerify(accessToken string) *TokenVerifyCall {
return &TokenVerifyCall{
c: client,
accessToken: accessToken,
}
}
// Client type
type TokenVerifyCall struct {
c *Client
ctx context.Context
accessToken string
}
// WithContext method
func (call *TokenVerifyCall) WithContext(ctx context.Context) *TokenVerifyCall {
call.ctx = ctx
return call
}
// Do method
func (call *TokenVerifyCall) Do() (*TokenVerifyResponse, error) {
u, err := url.Parse(APIEndpointBase)
if err != nil {
log.Print(err)
os.Exit(1)
}
u.Path = path.Join(u.Path, APIEndpointTokenVerify)
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("access_token", call.accessToken)
req.URL.RawQuery = q.Encode()
res, err := call.c.do(call.ctx, req)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToTokenVerifyResponse(res)
}
// Refresh Token: Gets a new access token using a refresh token. Refresh tokens are returned with the access token when the user authorizes your app.
//Note: This is the reference for the v2.1 endpoint. For the v2 reference, see Refresh access token v2.
//Note: Cannot be used to refresh channel access tokens which are used for the Messaging API.
func (client *Client) RefreshToken(refreshToken string) *RefreshTokenCall {
return &RefreshTokenCall{
c: client,
refreshToken: refreshToken,
}
}
// RefreshTokenCall type
type RefreshTokenCall struct {
c *Client
ctx context.Context
refreshToken string
}
// WithContext method
func (call *RefreshTokenCall) WithContext(ctx context.Context) *RefreshTokenCall {
call.ctx = ctx
return call
}
// Do method
func (call *RefreshTokenCall) Do() (*TokenRefreshResponse, error) {
data := url.Values{}
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", call.refreshToken)
data.Set("client_id", call.c.channelID)
data.Set("client_secret", call.c.channelSecret)
res, err := call.c.post(call.ctx, APIEndpointToken, strings.NewReader(data.Encode()))
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToTokenRefreshResponse(res)
}
// RevokeToken: Invalidates the access token.
//Note: This is the reference for the v2.1 endpoint. For the v2 reference, see Revoke access token v2.
//Note: Cannot be used to invalidate channel access tokens which are used for the Messaging API.
func (client *Client) RevokeToken(accessToken string) *RevokeTokenCall {
return &RevokeTokenCall{
c: client,
accessToken: accessToken,
}
}
// RefreshTokenCall type
type RevokeTokenCall struct {
c *Client
ctx context.Context
accessToken string
}
// WithContext method
func (call *RevokeTokenCall) WithContext(ctx context.Context) *RevokeTokenCall {
call.ctx = ctx
return call
}
// Do method
func (call *RevokeTokenCall) Do() (*BasicResponse, error) {
data := url.Values{}
data.Set("access_token", call.accessToken)
data.Set("client_id", call.c.channelID)
data.Set("client_secret", call.c.channelSecret)
res, err := call.c.post(call.ctx, APIEndpointRevokeToken, strings.NewReader(data.Encode()))
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToBasicResponse(res)
}
// GetUserProfile: Gets a user's display name, profile image, and status message.
//Note: Requires an access token with the profile scope. For more information, see Making an authorization request and Scopes.
func (client *Client) GetUserProfile(accessToken string) *GetUserProfileCall {
return &GetUserProfileCall{
c: client,
accessToken: accessToken,
}
}
// GetUserProfileCall type
type GetUserProfileCall struct {
c *Client
ctx context.Context
accessToken string
}
// WithContext method
func (call *GetUserProfileCall) WithContext(ctx context.Context) *GetUserProfileCall {
call.ctx = ctx
return call
}
// Do method
func (call *GetUserProfileCall) Do() (*GetUserProfileResponse, error) {
urlQuery := url.Values{}
urlQuery.Set("access_token", call.accessToken)
res, err := call.c.getHeaderAuth(call.ctx, APIEndpointGetUserProfile, urlQuery)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToGetUserProfileResponse(res)
}
// GetFriendshipStatus: Gets the friendship status of the user and the bot linked to your LINE Login channel.
//Note: Requires an access token with the profile scope. For more information, see Making an authorization request and Scopes.
//Note: You must have a bot linked with your channel. For more information, see Linking a bot with your LINE Login channel.
func (client *Client) GetFriendshipStatus(accessToken string) *GetFriendshipStatusCall {
return &GetFriendshipStatusCall{
c: client,
accessToken: accessToken,
}
}
// GetUserProfileCall type
type GetFriendshipStatusCall struct {
c *Client
ctx context.Context
accessToken string
}
// WithContext method
func (call *GetFriendshipStatusCall) WithContext(ctx context.Context) *GetFriendshipStatusCall {
call.ctx = ctx
return call
}
// Do method
func (call *GetFriendshipStatusCall) Do() (*GetFriendshipStatusResponse, error) {
urlQuery := url.Values{}
urlQuery.Set("access_token", call.accessToken)
res, err := call.c.getHeaderAuth(call.ctx, APIEndpointGetFriendshipStratus, urlQuery)
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToGetFriendshipStatusResponse(res)
}
// GetAccessTokenPKCECall: Issues access token by PKCE.
func (client *Client) GetAccessTokenPKCE(redirectURL, code, codeVerifier string) *GetAccessTokenPKCECall {
return &GetAccessTokenPKCECall{
c: client,
redirectURL: redirectURL,
code: code,
codeVerifier: codeVerifier,
}
}
type GetAccessTokenPKCECall struct {
c *Client
ctx context.Context
codeVerifier string
redirectURL string
code string
}
// WithContext method
func (call *GetAccessTokenPKCECall) WithContext(ctx context.Context) *GetAccessTokenPKCECall {
call.ctx = ctx
return call
}
// Do method
func (call *GetAccessTokenPKCECall) Do() (*TokenResponse, error) {
data := url.Values{}
// authorization_code. Specifies the grant type.
data.Set("grant_type", "authorization_code")
// Authorization code. Code returned in the authorization request.
data.Set("code", call.code)
data.Set("redirect_uri", call.redirectURL)
data.Set("client_id", call.c.channelID)
data.Set("client_secret", call.c.channelSecret)
data.Set("code_verifier", call.codeVerifier)
res, err := call.c.post(call.ctx, APIEndpointToken, strings.NewReader(data.Encode()))
if res != nil && res.Body != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
return decodeToTokenResponse(res)
}