83 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
| package providers
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"errors"
 | |
| 	"fmt"
 | |
| 	"net/http"
 | |
| 	"net/url"
 | |
| 
 | |
| 	"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
 | |
| 	"github.com/oauth2-proxy/oauth2-proxy/pkg/requests"
 | |
| )
 | |
| 
 | |
| // LinkedInProvider represents an LinkedIn based Identity Provider
 | |
| type LinkedInProvider struct {
 | |
| 	*ProviderData
 | |
| }
 | |
| 
 | |
| var _ Provider = (*LinkedInProvider)(nil)
 | |
| 
 | |
| // NewLinkedInProvider initiates a new LinkedInProvider
 | |
| 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"}
 | |
| 	}
 | |
| 	if p.ValidateURL.String() == "" {
 | |
| 		p.ValidateURL = p.ProfileURL
 | |
| 	}
 | |
| 	if p.Scope == "" {
 | |
| 		p.Scope = "r_emailaddress r_basicprofile"
 | |
| 	}
 | |
| 	return &LinkedInProvider{ProviderData: p}
 | |
| }
 | |
| 
 | |
| func getLinkedInHeader(accessToken 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", accessToken))
 | |
| 	return header
 | |
| }
 | |
| 
 | |
| // GetEmailAddress returns the Account email address
 | |
| func (p *LinkedInProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
 | |
| 	if s.AccessToken == "" {
 | |
| 		return "", errors.New("missing access token")
 | |
| 	}
 | |
| 
 | |
| 	requestURL := p.ProfileURL.String() + "?format=json"
 | |
| 	json, err := requests.New(requestURL).
 | |
| 		WithContext(ctx).
 | |
| 		WithHeaders(getLinkedInHeader(s.AccessToken)).
 | |
| 		Do().
 | |
| 		UnmarshalJSON()
 | |
| 	if err != nil {
 | |
| 		return "", err
 | |
| 	}
 | |
| 
 | |
| 	email, err := json.String()
 | |
| 	if err != nil {
 | |
| 		return "", err
 | |
| 	}
 | |
| 	return email, nil
 | |
| }
 | |
| 
 | |
| // ValidateSessionState validates the AccessToken
 | |
| func (p *LinkedInProvider) ValidateSessionState(ctx context.Context, s *sessions.SessionState) bool {
 | |
| 	return validateToken(ctx, p, s.AccessToken, getLinkedInHeader(s.AccessToken))
 | |
| }
 |