2015-04-18 00:33:17 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2020-05-05 17:53:33 +02:00
|
|
|
"context"
|
2015-04-18 00:33:17 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2020-03-29 15:54:36 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/requests"
|
2015-04-18 00:33:17 +02:00
|
|
|
)
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// LinkedInProvider represents an LinkedIn based Identity Provider
|
2015-04-18 00:33:17 +02:00
|
|
|
type LinkedInProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
var _ Provider = (*LinkedInProvider)(nil)
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
const (
|
|
|
|
linkedinProviderName = "LinkedIn"
|
|
|
|
linkedinDefaultScope = "r_emailaddress r_basicprofile"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Default Login URL for LinkedIn.
|
|
|
|
// Pre-parsed URL of https://www.linkedin.com/uas/oauth2/authorization.
|
|
|
|
linkedinDefaultLoginURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/uas/oauth2/authorization",
|
2015-04-18 00:33:17 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
|
|
|
|
// Default Redeem URL for LinkedIn.
|
|
|
|
// Pre-parsed URL of https://www.linkedin.com/uas/oauth2/accessToken.
|
|
|
|
linkedinDefaultRedeemURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/uas/oauth2/accessToken",
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
|
|
|
|
// Default Profile URL for LinkedIn.
|
|
|
|
// Pre-parsed URL of https://www.linkedin.com/v1/people/~/email-address.
|
|
|
|
linkedinDefaultProfileURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "www.linkedin.com",
|
|
|
|
Path: "/v1/people/~/email-address",
|
2015-04-18 00:33:17 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewLinkedInProvider initiates a new LinkedInProvider
|
|
|
|
func NewLinkedInProvider(p *ProviderData) *LinkedInProvider {
|
|
|
|
p.setProviderDefaults(providerDefaults{
|
|
|
|
name: linkedinProviderName,
|
|
|
|
loginURL: linkedinDefaultLoginURL,
|
|
|
|
redeemURL: linkedinDefaultRedeemURL,
|
|
|
|
profileURL: linkedinDefaultProfileURL,
|
|
|
|
validateURL: linkedinDefaultProfileURL,
|
|
|
|
scope: linkedinDefaultScope,
|
|
|
|
})
|
2015-04-18 00:33:17 +02:00
|
|
|
return &LinkedInProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:26:41 +02:00
|
|
|
func getLinkedInHeader(accessToken string) http.Header {
|
2015-05-13 03:48:13 +02:00
|
|
|
header := make(http.Header)
|
|
|
|
header.Set("Accept", "application/json")
|
|
|
|
header.Set("x-li-format", "json")
|
2018-11-29 16:26:41 +02:00
|
|
|
header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
|
2015-05-13 03:48:13 +02:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// GetEmailAddress returns the Account email address
|
2020-05-05 17:53:33 +02:00
|
|
|
func (p *LinkedInProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
|
2015-06-23 13:23:39 +02:00
|
|
|
if s.AccessToken == "" {
|
2015-04-18 00:33:17 +02:00
|
|
|
return "", errors.New("missing access token")
|
|
|
|
}
|
|
|
|
|
2020-07-03 20:27:25 +02:00
|
|
|
requestURL := p.ProfileURL.String() + "?format=json"
|
|
|
|
json, err := requests.New(requestURL).
|
|
|
|
WithContext(ctx).
|
|
|
|
WithHeaders(getLinkedInHeader(s.AccessToken)).
|
2020-07-06 18:42:26 +02:00
|
|
|
Do().
|
2020-07-03 20:27:25 +02:00
|
|
|
UnmarshalJSON()
|
2015-04-18 00:33:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
email, err := json.String()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
2015-05-13 03:48:13 +02:00
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// ValidateSessionState validates the AccessToken
|
2020-05-05 17:53:33 +02:00
|
|
|
func (p *LinkedInProvider) ValidateSessionState(ctx context.Context, s *sessions.SessionState) bool {
|
|
|
|
return validateToken(ctx, p, s.AccessToken, getLinkedInHeader(s.AccessToken))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|