2016-06-23 12:29:44 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2019-05-05 12:33:13 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/apis/sessions"
|
2019-05-24 15:55:12 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/requests"
|
2016-06-23 12:29:44 +00:00
|
|
|
)
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// FacebookProvider represents an Facebook based Identity Provider
|
2016-06-23 12:29:44 +00:00
|
|
|
type FacebookProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// NewFacebookProvider initiates a new FacebookProvider
|
2016-06-23 12:29:44 +00:00
|
|
|
func NewFacebookProvider(p *ProviderData) *FacebookProvider {
|
|
|
|
p.ProviderName = "Facebook"
|
|
|
|
if p.LoginURL.String() == "" {
|
|
|
|
p.LoginURL = &url.URL{Scheme: "https",
|
|
|
|
Host: "www.facebook.com",
|
|
|
|
Path: "/v2.5/dialog/oauth",
|
|
|
|
// ?granted_scopes=true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.RedeemURL.String() == "" {
|
|
|
|
p.RedeemURL = &url.URL{Scheme: "https",
|
|
|
|
Host: "graph.facebook.com",
|
|
|
|
Path: "/v2.5/oauth/access_token",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.ProfileURL.String() == "" {
|
|
|
|
p.ProfileURL = &url.URL{Scheme: "https",
|
|
|
|
Host: "graph.facebook.com",
|
|
|
|
Path: "/v2.5/me",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.ValidateURL.String() == "" {
|
|
|
|
p.ValidateURL = p.ProfileURL
|
|
|
|
}
|
|
|
|
if p.Scope == "" {
|
|
|
|
p.Scope = "public_profile email"
|
|
|
|
}
|
|
|
|
return &FacebookProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:26:41 +00:00
|
|
|
func getFacebookHeader(accessToken string) http.Header {
|
2016-06-23 12:29:44 +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))
|
2016-06-23 12:29:44 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// GetEmailAddress returns the Account email address
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *FacebookProvider) GetEmailAddress(s *sessions.SessionState) (string, error) {
|
2016-06-23 12:29:44 +00:00
|
|
|
if s.AccessToken == "" {
|
|
|
|
return "", errors.New("missing access token")
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", p.ProfileURL.String()+"?fields=name,email", nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
req.Header = getFacebookHeader(s.AccessToken)
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
var r result
|
2019-05-24 15:55:12 +00:00
|
|
|
err = requests.RequestJSON(req, &r)
|
2016-06-23 12:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if r.Email == "" {
|
|
|
|
return "", errors.New("no email")
|
|
|
|
}
|
|
|
|
return r.Email, nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// ValidateSessionState validates the AccessToken
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *FacebookProvider) ValidateSessionState(s *sessions.SessionState) bool {
|
2016-06-23 12:29:44 +00:00
|
|
|
return validateToken(p, s.AccessToken, getFacebookHeader(s.AccessToken))
|
|
|
|
}
|