/
linkedin.go
204 lines (185 loc) · 7.21 KB
/
linkedin.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
package oauth2_client
import (
"encoding/json"
"errors"
"github.com/pomack/jsonhelper.go/jsonhelper"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type LinkedInRequestTokenResult interface {
AuthToken
RequestAuthUrl() string
ExpiresAt() time.Time
CallbackConfirmed() bool
}
type LinkedInAccessTokenResult interface {
AuthToken
ExpiresAt() time.Time
}
type linkedInRequestTokenResult struct {
stdAuthToken
requestAuthUrl string
expiresAt time.Time
callbackConfirmed bool
}
func (p *linkedInRequestTokenResult) RequestAuthUrl() string { return p.requestAuthUrl }
func (p *linkedInRequestTokenResult) ExpiresAt() time.Time { return p.expiresAt }
func (p *linkedInRequestTokenResult) CallbackConfirmed() bool { return p.callbackConfirmed }
type linkedInAccessTokenResult struct {
stdAuthToken
expiresAt time.Time
}
func (p *linkedInAccessTokenResult) ExpiresAt() time.Time { return p.expiresAt }
type linkedInUserInfoResult struct {
id string
firstName string
lastName string
publicProfileUrl string
}
func (p *linkedInUserInfoResult) Guid() string { return p.id }
func (p *linkedInUserInfoResult) Username() string {
parts := strings.Split(p.publicProfileUrl, "/")
return parts[len(parts)-1]
}
func (p *linkedInUserInfoResult) GivenName() string { return p.firstName }
func (p *linkedInUserInfoResult) FamilyName() string { return p.lastName }
func (p *linkedInUserInfoResult) DisplayName() string {
if len(p.firstName) > 0 && len(p.lastName) > 0 {
return p.firstName + " " + p.lastName
}
return p.firstName + p.lastName
}
func (p *linkedInUserInfoResult) Url() string { return p.publicProfileUrl }
func (p *linkedInUserInfoResult) Id() string { return p.id }
func (p *linkedInUserInfoResult) FirstName() string { return p.firstName }
func (p *linkedInUserInfoResult) LastName() string { return p.lastName }
func (p *linkedInUserInfoResult) PublicProfileUrl() string { return p.publicProfileUrl }
func (p *linkedInUserInfoResult) FromJSON(props jsonhelper.JSONObject) {
p.id = props.GetAsString("id")
p.firstName = props.GetAsString("firstName")
p.lastName = props.GetAsString("lastName")
p.publicProfileUrl = props.GetAsString("publicProfileUrl")
}
type linkedInClient struct {
stdOAuth1Client
}
func NewLinkedInClient() OAuth2Client {
return &linkedInClient{}
}
func (p *linkedInClient) RequestUrl() string { return _LINKEDIN_REQUEST_TOKEN_URL }
func (p *linkedInClient) RequestUrlMethod() string { return _LINKEDIN_REQUEST_TOKEN_METHOD }
func (p *linkedInClient) RequestUrlProtected() bool { return _LINKEDIN_REQUEST_TOKEN_PROTECTED }
func (p *linkedInClient) AccessUrl() string { return _LINKEDIN_ACCESS_TOKEN_URL }
func (p *linkedInClient) AccessUrlMethod() string { return _LINKEDIN_ACCESS_TOKEN_METHOD }
func (p *linkedInClient) AccessUrlProtected() bool { return _LINKEDIN_ACCESS_TOKEN_PROTECTED }
func (p *linkedInClient) AuthorizationUrl() string { return _LINKEDIN_AUTHORIZATION_PATH_URL }
func (p *linkedInClient) AuthorizedResourceProtected() bool {
return _LINKEDIN_AUTHORIZED_RESOURCE_PROTECTED
}
func (p *linkedInClient) ServiceId() string { return "linkedin.com" }
func (p *linkedInClient) Initialize(properties jsonhelper.JSONObject) {
if p.currentCredentials == nil {
p.currentCredentials = NewStandardAuthToken()
}
if properties == nil {
return
}
if v := properties.GetAsString("linkedin.api.key"); len(v) > 0 {
p.consumerKey = v
//p.Credentials.Token = v
}
if v := properties.GetAsString("linkedin.client.redirect_uri"); len(v) > 0 {
p.callbackUrl = v
}
if v := properties.GetAsString("linkedin.secret.key"); len(v) > 0 {
p.consumerSecret = v
//p.Credentials.Secret = v
}
if v := properties.GetAsString("linkedin.oauth1.scope"); len(v) > 0 {
//p.Scope = v
}
if v := properties.GetAsString("linkedin.client.token"); len(v) > 0 {
p.currentCredentials.SetToken(v)
}
if v := properties.GetAsString("linkedin.client.secret"); len(v) > 0 {
p.currentCredentials.SetSecret(v)
}
}
func (p *linkedInClient) GenerateRequestTokenUrl(properties jsonhelper.JSONObject) string {
return oauth1GenerateRequestTokenUrl(p, properties)
}
func (p *linkedInClient) RequestTokenGranted(req *http.Request) bool {
return oauth1RequestTokenGranted(p, req)
}
func (p *linkedInClient) ExchangeRequestTokenForAccess(req *http.Request) error {
return oauth1ExchangeRequestTokenForAccess(p, req)
}
func (p *linkedInClient) CreateAuthorizedRequest(method string, headers http.Header, uri string, query url.Values, r io.Reader) (*http.Request, error) {
if headers == nil {
headers = make(http.Header)
}
headers.Set("X-Li-Format", "json")
return oauth1CreateAuthorizedRequest(p, method, headers, uri, query, r)
}
func (p *linkedInClient) ParseRequestTokenResult(value string) (AuthToken, error) {
LogDebug("+++++++++++++++++++++++++++++++")
LogDebug("LinkedIn Client parsing request token result")
t := new(linkedInRequestTokenResult)
m, err := url.ParseQuery(value)
if m != nil {
t.token = m.Get("oauth_token")
t.secret = m.Get("oauth_token_secret")
t.requestAuthUrl = m.Get("xoauth_request_auth_url")
t.callbackConfirmed = m.Get("callback_confirmed") == "true"
strExpiresIn := m.Get("oauth_expires_in")
expiresIn, _ := strconv.ParseInt(strExpiresIn, 10, 64)
if expiresIn > 0 {
t.expiresAt = time.Now().Add(time.Second * time.Duration(expiresIn)).UTC()
}
if err == nil && len(m.Get("oauth_problem")) > 0 {
err = errors.New(m.Get("oauth_problem"))
}
}
LogDebug("+++++++++++++++++++++++++++++++")
return t, err
}
func (p *linkedInClient) ParseAccessTokenResult(value string) (AuthToken, error) {
LogDebug("+++++++++++++++++++++++++++++++")
LogDebug("LinkedIn Client parsing access token result")
t := new(linkedInAccessTokenResult)
m, err := url.ParseQuery(value)
if m != nil {
t.token = m.Get("oauth_token")
t.secret = m.Get("oauth_token_secret")
strExpiresIn := m.Get("xoauth_authorization_expires_in")
expiresIn, _ := strconv.ParseInt(strExpiresIn, 10, 64)
if expiresIn > 0 {
t.expiresAt = time.Now().Add(time.Second * time.Duration(expiresIn)).UTC()
}
if err == nil && len(m.Get("oauth_problem")) > 0 {
err = errors.New(m.Get("oauth_problem"))
}
}
LogDebug("+++++++++++++++++++++++++++++++")
return t, err
}
func (p *linkedInClient) RetrieveUserInfo() (UserInfo, error) {
req, err := p.CreateAuthorizedRequest(_LINKEDIN_USERINFO_METHOD, nil, _LINKEDIN_USERINFO_URL, nil, nil)
if err != nil {
return nil, err
}
result := new(linkedInUserInfoResult)
resp, _, err := MakeRequest(p, req)
if resp != nil && resp.Body != nil {
props := jsonhelper.NewJSONObject()
if err2 := json.NewDecoder(resp.Body).Decode(&props); err == nil {
err = err2
}
result.FromJSON(props)
}
return result, err
}