oauth2_proxy/providers/linkedin.go

80 lines
2.0 KiB
Go
Raw Normal View History

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"
2019-05-05 12:33:13 +00:00
"github.com/pusher/oauth2_proxy/pkg/apis/sessions"
2015-04-17 22:33:17 +00:00
)
// LinkedInProvider represents an LinkedIn based Identity Provider
2015-04-17 22:33:17 +00:00
type LinkedInProvider struct {
*ProviderData
}
// NewLinkedInProvider initiates a new LinkedInProvider
2015-04-17 22:33:17 +00:00
func NewLinkedInProvider(p *ProviderData) *LinkedInProvider {
p.ProviderName = "LinkedIn"
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"}
}
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"}
}
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"}
}
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
}
// GetEmailAddress returns the Account email address
2019-05-05 12:33:13 +00:00
func (p *LinkedInProvider) GetEmailAddress(s *sessions.SessionState) (string, error) {
if s.AccessToken == "" {
2015-04-17 22:33:17 +00:00
return "", errors.New("missing access token")
}
req, err := http.NewRequest("GET", p.ProfileURL.String()+"?format=json", nil)
2015-04-17 22:33:17 +00:00
if err != nil {
return "", err
}
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
// ValidateSessionState validates the AccessToken
2019-05-05 12:33:13 +00:00
func (p *LinkedInProvider) ValidateSessionState(s *sessions.SessionState) bool {
return validateToken(p, s.AccessToken, getLinkedInHeader(s.AccessToken))
2015-05-13 01:48:13 +00:00
}