-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
336 lines (284 loc) · 9.63 KB
/
session.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
package linkedin
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// Session holds a LinkedIn session with an access token.
// Session should be created by App.Session.
type Session struct {
HTTPClient HTTPClient // HTTP client to send requests
BaseURL string // set to override API base URL
accessToken string // linkedIn access token, can be empty
app *App // linkedIn app
LinkedInVersion string // e.g. 202404
useAuthorizationHeader bool // pass accessToken in headers
context context.Context // session context
}
// HTTPClient is an interface to send http request.
// It is compatible with type `*http.Client`.
type HTTPClient interface {
Do(req *http.Request) (resp *http.Response, err error)
Get(url string) (resp *http.Response, err error)
Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error)
}
// Params to construct the request payload.
type Params map[string]interface{}
// default LinkedIn session
var (
defaultSession = &Session{}
)
// App returns associated App.
func (session *Session) App() *App {
return session.app
}
// AccessToken returns current access token.
func (session *Session) AccessToken() string {
return session.accessToken
}
// SetAccessToken sets a new access token.
func (session *Session) SetAccessToken(token string) {
if token != session.accessToken {
session.accessToken = token
}
}
// UseAuthorizationHeader passes `access_token` in HTTP Authorization header.
func (session *Session) UseAuthorizationHeader() {
session.useAuthorizationHeader = true
}
// RefreshToken redeems refresh token for new access and refresh tokens.
//
// See: https://learn.microsoft.com/en-us/linkedin/shared/authentication/programmatic-refresh-tokens?toc=%2Flinkedin%2Fmarketing%2Ftoc.json&bc=%2Flinkedin%2Fbreadcrumb%2Ftoc.json&view=li-lms-2024-04
func (session *Session) RefreshToken(refreshToken string) (Token, error) {
refreshToken = strings.TrimSpace(refreshToken)
if refreshToken == "" {
err := fmt.Errorf("linkedIn: refresh token is empty")
return Token{}, err
}
token, err := session.sendAuthRequest("/accessToken", Params{
"grant_type": "refresh_token",
"client_id": session.App().ClientID,
"client_secret": session.App().ClientSecret,
"refresh_token": refreshToken,
})
return token, err
}
// Get sends a GET request to LinkedIn API and returns the response.
func (session *Session) Get(uri string) (response *http.Response, data []byte, err error) {
// uri must start with `/`
if !strings.HasPrefix(uri, "/") {
uri = "/" + uri
}
url := session.BaseURL + uri
// create a new HTTP request
request, err := http.NewRequest("GET", url, nil)
if err != nil {
err = fmt.Errorf("linkedIn: cannot create new request; %w", err)
return nil, nil, err
}
// set headers
request.Header.Set(string(ContentType), string(JSON))
request.Header.Set(string(RestLiProtocolVersion), "2.0.0")
request.Header.Set(string(LinkedInVersion), session.LinkedInVersion)
// send the request
response, data, err = session.sendRequest(request)
return
}
// sendAuthRequest sends an auth request to LinkedIn and returns new tokens.
func (session *Session) sendAuthRequest(uri string, params Params) (Token, error) {
if params == nil {
return Token{}, fmt.Errorf("linkedIn: required params are missing")
}
if params["grant_type"] == nil {
return Token{}, fmt.Errorf("linkedIn: grant_type is missing")
}
grantType := params["grant_type"].(string)
if grantType == "" {
return Token{}, fmt.Errorf("linkedIn: grant_type is required to receive new tokens")
}
if params["client_id"] == nil {
return Token{}, fmt.Errorf("linkedIn: client_id is missing")
}
clientID := params["client_id"].(string)
if clientID == "" {
return Token{}, fmt.Errorf("linkedIn: client_id is required to receive new tokens")
}
if params["client_secret"] == nil {
return Token{}, fmt.Errorf("linkedIn: client_secret is missing")
}
clientSecret := params["client_secret"].(string)
if clientSecret == "" {
return Token{}, fmt.Errorf("linkedIn: client_secret is required to receive new tokens")
}
redirectURI := ""
code := ""
if grantType == "authorization_code" {
if params["redirect_uri"] == nil {
return Token{}, fmt.Errorf("linkedIn: redirect_uri is missing")
}
redirectURI = params["redirect_uri"].(string)
if redirectURI == "" {
return Token{}, fmt.Errorf("linkedIn: redirect_uri is required to redeem auth code")
}
if params["code"] == nil {
return Token{}, fmt.Errorf("linkedIn: auth code is missing")
}
code = params["code"].(string)
if code == "" {
return Token{}, fmt.Errorf("linkedIn: auth code is required to receive new tokens")
}
}
refToken := ""
if grantType == "refresh_token" {
if params["refresh_token"] == nil {
return Token{}, fmt.Errorf("linkedIn: refresh_token is missing")
}
refToken = params["refresh_token"].(string)
if refToken == "" {
return Token{}, fmt.Errorf("linkedIn: refresh_token is required to refresh tokens")
}
}
oauthURL := OauthBaseURL + uri
// data to be sent in the body (x-www-form-urlencoded)
data := url.Values{}
data.Set("grant_type", grantType)
data.Add("client_id", clientID)
data.Add("client_secret", clientSecret)
if grantType == "authorization_code" {
data.Add("redirect_uri", redirectURI)
data.Add("code", code)
}
if grantType == "refresh_token" {
data.Add("refresh_token", refToken)
}
// encode data into appropriate format
requestBody := bytes.NewBufferString(data.Encode())
// create a new HTTP request
request, err := http.NewRequest("POST", oauthURL, requestBody)
if err != nil {
return Token{}, err
}
// set headers
request.Header.Set(string(ContentType), string(URLEncoded))
// send the request
response, responseData, err := session.sendRequest(request)
if err != nil {
return Token{}, err
}
if response.StatusCode != http.StatusOK {
return Token{}, fmt.Errorf("linkedIn: failed to receive tokens with status %d", response.StatusCode)
}
// parse the response body
var token Token
err = json.Unmarshal(responseData, &token)
if err != nil {
return Token{}, err
}
return token, nil
}
// Introspect checks the Time to Live (TTL) and status (active/expired) for the given token.
//
// See: https://learn.microsoft.com/en-us/linkedin/shared/authentication/token-introspection?toc=%2Flinkedin%2Fmarketing%2Ftoc.json&bc=%2Flinkedin%2Fbreadcrumb%2Ftoc.json&view=li-lms-2024-04&tabs=http
func (session *Session) Introspect(token string) (TokenData, error) {
token = strings.TrimSpace(token)
if token == "" {
err := fmt.Errorf("linkedIn: token is empty")
return TokenData{}, err
}
tokenData, err := session.introspect("/introspectToken", Params{
"token": token,
})
return tokenData, err
}
// token introspection
func (session *Session) introspect(uri string, params Params) (TokenData, error) {
if params == nil {
return TokenData{}, fmt.Errorf("linkedIn: required params are missing")
}
if params["token"] == nil {
return TokenData{}, fmt.Errorf("linkedIn: token is missing")
}
token := params["token"].(string)
if token == "" {
return TokenData{}, fmt.Errorf("linkedIn: token is required for introspection")
}
oauthURL := OauthBaseURL + uri
// data to be sent in the body (x-www-form-urlencoded)
data := url.Values{}
data.Set("client_id", session.App().ClientID)
data.Add("client_secret", session.App().ClientSecret)
data.Add("token", token)
// encode data into appropriate format
requestBody := bytes.NewBufferString(data.Encode())
// create a new HTTP request
request, err := http.NewRequest("POST", oauthURL, requestBody)
if err != nil {
return TokenData{}, err
}
// set headers
request.Header.Set(string(ContentType), string(URLEncoded))
// send the request
response, responseData, err := session.sendRequest(request)
if err != nil {
return TokenData{}, err
}
if response.StatusCode != http.StatusOK {
return TokenData{}, fmt.Errorf("linkedIn: failed to introspect token with status %d", response.StatusCode)
}
// parse the response body
var tokenData TokenData
err = json.Unmarshal(responseData, &tokenData)
if err != nil {
return TokenData{}, err
}
return tokenData, nil
}
// sendRequest sends an API request and returns the response.
func (session *Session) sendRequest(request *http.Request) (response *http.Response, data []byte, err error) {
if session.context != nil {
request = request.WithContext(session.context)
}
if session.useAuthorizationHeader {
request.Header.Set(string(Authorization), "Bearer "+session.accessToken)
}
if session.HTTPClient == nil {
response, err = http.DefaultClient.Do(request)
} else {
response, err = session.HTTPClient.Do(request)
}
if err != nil {
err = fmt.Errorf("linkedIn: cannot reach linkedIn server; %w", err)
return
}
buf := &bytes.Buffer{}
_, err = io.Copy(buf, response.Body)
response.Body.Close()
if err != nil {
err = fmt.Errorf("linkedIn: cannot read linkedIn response; %w", err)
}
data = buf.Bytes()
return
}
// Context returns the session's context.
// To change the context, use `Session#WithContext`.
//
// The returned context is always non-nil; it defaults to the background context.
// For outgoing LinkedIn API requests, the context controls timeout/deadline and cancellation.
func (session *Session) Context() context.Context {
if session.context != nil {
return session.context
}
return context.Background()
}
// WithContext returns a shallow copy of session with its context changed to ctx.
// The provided ctx must be non-nil.
func (session *Session) WithContext(ctx context.Context) *Session {
s := *session
s.context = ctx
return &s
}