2015-04-17 22:33:17 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2018-11-27 11:45:05 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/api"
|
2015-04-17 22:33:17 +00:00
|
|
|
)
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// LinkedInProvider represents an LinkedIn based Identity Provider
|
2015-04-17 22:33:17 +00:00
|
|
|
type LinkedInProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// NewLinkedInProvider initiates a new LinkedInProvider
|
2015-04-17 22:33:17 +00:00
|
|
|
func NewLinkedInProvider(p *ProviderData) *LinkedInProvider {
|
|
|
|
p.ProviderName = "LinkedIn"
|
2015-11-08 23:47:44 +00:00
|
|
|
if p.LoginURL.String() == "" {
|
|
|
|
p.LoginURL = &url.URL{Scheme: "https",
|
2015-04-17 22:33:17 +00:00
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/uas/oauth2/authorization"}
|
|
|
|
}
|
2015-11-08 23:47:44 +00:00
|
|
|
if p.RedeemURL.String() == "" {
|
|
|
|
p.RedeemURL = &url.URL{Scheme: "https",
|
2015-04-17 22:33:17 +00:00
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/uas/oauth2/accessToken"}
|
|
|
|
}
|
2015-11-08 23:47:44 +00:00
|
|
|
if p.ProfileURL.String() == "" {
|
|
|
|
p.ProfileURL = &url.URL{Scheme: "https",
|
2015-04-17 22:33:17 +00:00
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/v1/people/~/email-address"}
|
|
|
|
}
|
2015-11-08 23:47:44 +00:00
|
|
|
if p.ValidateURL.String() == "" {
|
|
|
|
p.ValidateURL = p.ProfileURL
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
2015-04-17 22:33:17 +00:00
|
|
|
if p.Scope == "" {
|
|
|
|
p.Scope = "r_emailaddress r_basicprofile"
|
|
|
|
}
|
|
|
|
return &LinkedInProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:26:41 +00:00
|
|
|
func getLinkedInHeader(accessToken string) http.Header {
|
2015-05-13 01:48:13 +00:00
|
|
|
header := make(http.Header)
|
|
|
|
header.Set("Accept", "application/json")
|
|
|
|
header.Set("x-li-format", "json")
|
2018-11-29 14:26:41 +00:00
|
|
|
header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
|
2015-05-13 01:48:13 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// GetEmailAddress returns the Account email address
|
2015-06-23 11:23:39 +00:00
|
|
|
func (p *LinkedInProvider) GetEmailAddress(s *SessionState) (string, error) {
|
|
|
|
if s.AccessToken == "" {
|
2015-04-17 22:33:17 +00:00
|
|
|
return "", errors.New("missing access token")
|
|
|
|
}
|
2015-11-08 23:47:44 +00:00
|
|
|
req, err := http.NewRequest("GET", p.ProfileURL.String()+"?format=json", nil)
|
2015-04-17 22:33:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
req.Header = getLinkedInHeader(s.AccessToken)
|
2015-04-17 22:33:17 +00:00
|
|
|
|
|
|
|
json, err := api.Request(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
email, err := json.String()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
2015-05-13 01:48:13 +00:00
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// ValidateSessionState validates the AccessToken
|
2015-06-23 11:23:39 +00:00
|
|
|
func (p *LinkedInProvider) ValidateSessionState(s *SessionState) bool {
|
|
|
|
return validateToken(p, s.AccessToken, getLinkedInHeader(s.AccessToken))
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|