-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
241 lines (224 loc) · 9.79 KB
/
api.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
package golinkedinapi
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
"golang.org/x/oauth2/linkedin"
)
const (
fullRequestURL = "https://api.linkedin.com/v1/people/~:(id,first-name,email-address,last-name,headline,picture-url,industry,summary,specialties,positions:(id,title,summary,start-date,end-date,is-current,company:(id,name,type,size,industry,ticker)),educations:(id,school-name,field-of-study,start-date,end-date,degree,activities,notes),associations,interests,num-recommenders,date-of-birth,publications:(id,title,publisher:(name),authors:(id,name),date,url,summary),patents:(id,title,summary,number,status:(id,name),office:(name),inventors:(id,name),date,url),languages:(id,language:(name),proficiency:(level,name)),skills:(id,skill:(name)),certifications:(id,name,authority:(name),number,start-date,end-date),courses:(id,name,number),recommendations-received:(id,recommendation-type,recommendation-text,recommender),honors-awards,three-current-positions,three-past-positions,volunteer)?format=json"
basicRequestURL = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,picture-url,industry,summary,specialties,positions:(id,title,summary,start-date,end-date,is-current,company:(id,name,type,size,industry,ticker)),educations:(id,school-name,field-of-study,start-date,end-date,degree,activities,notes),associations,interests,num-recommenders,date-of-birth,publications:(id,title,publisher:(name),authors:(id,name),date,url,summary),patents:(id,title,summary,number,status:(id,name),office:(name),inventors:(id,name),date,url),languages:(id,language:(name),proficiency:(level,name)),skills:(id,skill:(name)),certifications:(id,name,authority:(name),number,start-date,end-date),courses:(id,name,number),recommendations-received:(id,recommendation-type,recommendation-text,recommender),honors-awards,three-current-positions,three-past-positions,volunteer)?format=json"
)
var (
validPermissions = map[string]bool{
"r_basicprofile": true,
"r_emailaddress": true,
"rw_company_admin": true,
"w_share": true,
}
authConf *oauth2.Config
store = sessions.NewCookieStore([]byte("golinkedinapi"))
requestedURL string
)
// LinkedinProfile is used within this package as it is less useful than native types.
type LinkedinProfile struct {
// ProfileID represents the Unique ID every Linkedin profile has.
ProfileID string `json:"id"`
// FirstName represents the user's first name.
FirstName string `json:"first_name"`
// LastName represents the user's last name.
LastName string `json:"last-name"`
// MaidenName represents the user's maiden name, if they have one.
MaidenName string `json:"maiden-name"`
// FormattedName represents the user's formatted name, based on locale.
FormattedName string `json:"formatted-name"`
// PhoneticFirstName represents the user's first name, spelled phonetically.
PhoneticFirstName string `json:"phonetic-first-name"`
// PhoneticFirstName represents the user's last name, spelled phonetically.
PhoneticLastName string `json:"phonetic-last-name"`
// Headline represents a short, attention grabbing description of the user.
Headline string `json:"headline"`
// Location represents where the user is located.
Location Location `json:"location"`
// Industry represents what industry the user is working in.
Industry string `json:"industry"`
// CurrentShare represents the user's last shared post.
CurrentShare string `json:"current-share"`
// NumConnections represents the user's number of connections, up to a maximum of 500.
// The user's connections may be over this, however it will not be shown. (i.e. 500+ connections)
NumConnections int `json:"num-connections"`
// IsConnectionsCapped represents whether or not the user's connections are capped.
IsConnectionsCapped bool `json:"num-connections-capped"`
// Summary represents a long-form text description of the user's capabilities.
Summary string `json:"summary"`
// Specialties is a short-form text description of the user's specialties.
Specialties string `json:"specialties"`
// Positions is a Positions struct that describes the user's previously held positions.
Positions Positions `json:"positions"`
// PictureURL represents a URL pointing to the user's profile picture.
PictureURL string `json:"picture-url"`
// EmailAddress represents the user's e-mail address, however you must specify 'r_emailaddress'
// to be able to retrieve this.
EmailAddress string `json:"email-address"`
}
// Positions represents the result given by json:"positions"
type Positions struct {
total int
values []Position
}
// Location specifies the users location
type Location struct {
UserLocation string
CountryCode string
}
// Position represents a job held by the authorized user.
type Position struct {
// ID represents a unique ID representing the position
ID string
// Title represents a user's position's title, for example Jeff Bezos's title would be 'CEO'
Title string
// Summary represents a short description of the user's position.
Summary string
// StartDate represents when the user's position started.
StartDate string
// EndDate represents the user's position's end date, if any.
EndDate string
// IsCurrent represents if the position is currently held or not.
// If this is false, EndDate will not be returned, and will therefore equal ""
IsCurrent bool
// Company represents the Company where the user is employed.
Company PositionCompany
}
// PositionCompany represents a company that is described within a user's Profile.
// This is different from Company, which fully represents a company's data.
type PositionCompany struct {
// ID represents a unique ID representing the company
ID string
// Name represents the name of the company
Name string
// Type represents the type of the company, either 'public' or 'private'
Type string
// Industry represents which industry the company is in.
Industry string
// Ticker represents the stock market ticker symbol of the company.
// This will be blank if the company is privately held.
Ticker string
}
// ParseJSON converts a JSON string to a pointer to a LinkedinProfile.
func parseJSON(s string) (*LinkedinProfile, error) {
linkedinProfile := &LinkedinProfile{}
bytes := bytes.NewBuffer([]byte(s))
err := json.NewDecoder(bytes).Decode(linkedinProfile)
if err != nil {
return nil, err
}
return linkedinProfile, nil
}
// generateState generates a random set of bytes to ensure state is preserved.
// This prevents such things as XSS occuring.
func generateState() string {
b := make([]byte, 32)
rand.Read(b)
return string(b)
}
// getSessionValue grabs the value of an interface in this case being the session.Values["string"]
// This will return "" if f is nil.
func getSessionValue(f interface{}) string {
if f != nil {
if foo, ok := f.(string); ok {
return foo
}
}
return ""
}
// validState validates the state stored client-side with the request's state,
// it returns false if the states are not equal to each other.
func validState(r *http.Request) bool {
// Retrieve state
session, _ := store.Get(r, "golinkedinapi")
// Compare state to header's state
retrievedState := session.Values["state"]
if getSessionValue(retrievedState) != r.Header.Get("state") {
return false
}
return true
}
// GetLoginURL provides a state-specific login URL for the user to login to.
// CAUTION: This must be called before GetProfileData() as this enforces state.
func GetLoginURL(w http.ResponseWriter, r *http.Request) string {
state := generateState()
// The only time this will error out is if the session cannot be decoded, however
// we don't care about that as we can simply change state.
session, _ := store.Get(r, "golinkedinapi")
session.Values["state"] = state
defer session.Save(r, w)
return authConf.AuthCodeURL(state)
}
// GetProfileData gather's the user's Linkedin profile data and returns it as a pointer to a LinkedinProfile struct.
// CAUTION: GetLoginURL must be called before this, as GetProfileData() has a state check.
func GetProfileData(w http.ResponseWriter, r *http.Request) (*LinkedinProfile, error) {
if validState(r) == false {
err := fmt.Errorf("State comparison failed")
return &LinkedinProfile{}, err
}
params := r.URL.Query()
// Authenticate
tok, err := authConf.Exchange(oauth2.NoContext, params.Get("code"))
if err != nil {
return &LinkedinProfile{}, err
}
client := authConf.Client(oauth2.NoContext, tok)
// Retrieve data
resp, err := client.Get(fullRequestURL)
if err != nil {
return &LinkedinProfile{}, err
}
// Store data to struct and return.
data, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
formattedData, err := parseJSON(string(data))
if err != nil {
return &LinkedinProfile{}, err
}
return formattedData, nil
}
// InitConfig initializes the config needed by the client.
// permissions is a string of all scopes desired by the user.
func InitConfig(permissions []string, clientID string, clientSecret string, redirectURL string) {
var isEmail, isBasic bool
if permissions == nil {
panic(fmt.Errorf("You must specify some scope to request"))
}
for _, elem := range permissions {
if elem == "r_emailaddress" {
isEmail = true
} else if elem == "r_basicprofile" {
isBasic = true
}
if validPermissions[elem] != true {
panic(fmt.Errorf("All elements of permissions must be valid Linkedin permissions as specified in the API docs"))
}
}
_, err := url.ParseRequestURI(redirectURL)
if err != nil {
panic(fmt.Errorf("redirectURL specified must be a valid FQDN. Please ensure you added https:// to the front"))
}
authConf = &oauth2.Config{ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: linkedin.Endpoint,
RedirectURL: redirectURL,
Scopes: permissions,
}
if isEmail && isBasic {
requestedURL = fullRequestURL
} else if isBasic {
requestedURL = basicRequestURL
}
}
// TODO: Config Struct