2015-04-18 00:33:17 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2015-05-21 08:50:21 +02:00
|
|
|
"github.com/bitly/oauth2_proxy/api"
|
2015-04-18 00:33:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type LinkedInProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLinkedInProvider(p *ProviderData) *LinkedInProvider {
|
|
|
|
p.ProviderName = "LinkedIn"
|
|
|
|
if p.LoginUrl.String() == "" {
|
|
|
|
p.LoginUrl = &url.URL{Scheme: "https",
|
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/uas/oauth2/authorization"}
|
|
|
|
}
|
|
|
|
if p.RedeemUrl.String() == "" {
|
|
|
|
p.RedeemUrl = &url.URL{Scheme: "https",
|
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/uas/oauth2/accessToken"}
|
|
|
|
}
|
|
|
|
if p.ProfileUrl.String() == "" {
|
|
|
|
p.ProfileUrl = &url.URL{Scheme: "https",
|
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/v1/people/~/email-address"}
|
|
|
|
}
|
2015-05-13 03:48:13 +02:00
|
|
|
if p.ValidateUrl.String() == "" {
|
|
|
|
p.ValidateUrl = p.ProfileUrl
|
|
|
|
}
|
2015-04-18 00:33:17 +02:00
|
|
|
if p.Scope == "" {
|
|
|
|
p.Scope = "r_emailaddress r_basicprofile"
|
|
|
|
}
|
|
|
|
return &LinkedInProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
2015-05-13 03:48:13 +02:00
|
|
|
func getLinkedInHeader(access_token string) http.Header {
|
|
|
|
header := make(http.Header)
|
|
|
|
header.Set("Accept", "application/json")
|
|
|
|
header.Set("x-li-format", "json")
|
|
|
|
header.Set("Authorization", fmt.Sprintf("Bearer %s", access_token))
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func (p *LinkedInProvider) GetEmailAddress(s *SessionState) (string, error) {
|
|
|
|
if s.AccessToken == "" {
|
2015-04-18 00:33:17 +02:00
|
|
|
return "", errors.New("missing access token")
|
|
|
|
}
|
2015-06-23 13:23:39 +02:00
|
|
|
req, err := http.NewRequest("GET", p.ProfileUrl.String()+"?format=json", nil)
|
2015-04-18 00:33:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-06-23 13:23:39 +02:00
|
|
|
req.Header = getLinkedInHeader(s.AccessToken)
|
2015-04-18 00:33:17 +02:00
|
|
|
|
|
|
|
json, err := api.Request(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed making request %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
email, err := json.String()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed making request %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
2015-05-13 03:48:13 +02:00
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func (p *LinkedInProvider) ValidateSessionState(s *SessionState) bool {
|
|
|
|
return validateToken(p, s.AccessToken, getLinkedInHeader(s.AccessToken))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|