2015-05-21 03:23:48 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-06-06 18:15:43 +00:00
|
|
|
"fmt"
|
2015-05-21 03:23:48 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-05-07 14:32:46 +00:00
|
|
|
"time"
|
2015-06-23 11:23:39 +00:00
|
|
|
|
2018-11-27 11:45:05 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/cookie"
|
2019-05-05 12:33:13 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/apis/sessions"
|
2015-05-21 03:23:48 +00:00
|
|
|
)
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// Redeem provides a default implementation of the OAuth2 token redemption process
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *ProviderData) Redeem(redirectURL, code string) (s *sessions.SessionState, err error) {
|
2015-05-21 03:23:48 +00:00
|
|
|
if code == "" {
|
|
|
|
err = errors.New("missing code")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
params := url.Values{}
|
2015-11-08 23:47:44 +00:00
|
|
|
params.Add("redirect_uri", redirectURL)
|
2015-05-21 03:23:48 +00:00
|
|
|
params.Add("client_id", p.ClientID)
|
|
|
|
params.Add("client_secret", p.ClientSecret)
|
|
|
|
params.Add("code", code)
|
|
|
|
params.Add("grant_type", "authorization_code")
|
2015-11-09 08:28:34 +00:00
|
|
|
if p.ProtectedResource != nil && p.ProtectedResource.String() != "" {
|
|
|
|
params.Add("resource", p.ProtectedResource.String())
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
var req *http.Request
|
2015-11-08 23:47:44 +00:00
|
|
|
req, err = http.NewRequest("POST", p.RedeemURL.String(), bytes.NewBufferString(params.Encode()))
|
2015-05-21 03:23:48 +00:00
|
|
|
if err != nil {
|
2015-06-23 11:23:39 +00:00
|
|
|
return
|
2015-05-21 03:23:48 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
var resp *http.Response
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
2015-05-21 03:23:48 +00:00
|
|
|
if err != nil {
|
2015-06-23 11:23:39 +00:00
|
|
|
return nil, err
|
2015-05-21 03:23:48 +00:00
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
var body []byte
|
2015-05-21 03:23:48 +00:00
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
2015-06-23 11:23:39 +00:00
|
|
|
return
|
2015-05-21 03:23:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 18:15:43 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2015-11-08 23:47:44 +00:00
|
|
|
err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemURL.String(), body)
|
2015-06-23 11:23:39 +00:00
|
|
|
return
|
2015-06-06 18:15:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 03:23:48 +00:00
|
|
|
// blindly try json and x-www-form-urlencoded
|
|
|
|
var jsonResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &jsonResponse)
|
|
|
|
if err == nil {
|
2019-05-05 12:33:13 +00:00
|
|
|
s = &sessions.SessionState{
|
2015-06-23 11:23:39 +00:00
|
|
|
AccessToken: jsonResponse.AccessToken,
|
|
|
|
}
|
|
|
|
return
|
2015-05-21 03:23:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
var v url.Values
|
|
|
|
v, err = url.ParseQuery(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if a := v.Get("access_token"); a != "" {
|
2019-05-07 14:32:46 +00:00
|
|
|
s = &sessions.SessionState{AccessToken: a, CreatedAt: time.Now()}
|
2015-06-23 11:23:39 +00:00
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("no access token found %s", body)
|
|
|
|
}
|
|
|
|
return
|
2015-05-21 03:23:48 +00:00
|
|
|
}
|
2015-06-06 18:15:43 +00:00
|
|
|
|
2015-06-23 17:23:47 +00:00
|
|
|
// GetLoginURL with typical oauth parameters
|
2017-03-28 01:14:38 +00:00
|
|
|
func (p *ProviderData) GetLoginURL(redirectURI, state string) string {
|
2015-06-23 17:23:47 +00:00
|
|
|
var a url.URL
|
2015-11-08 23:47:44 +00:00
|
|
|
a = *p.LoginURL
|
2015-06-23 17:23:47 +00:00
|
|
|
params, _ := url.ParseQuery(a.RawQuery)
|
|
|
|
params.Set("redirect_uri", redirectURI)
|
2015-07-25 23:27:49 +00:00
|
|
|
params.Set("approval_prompt", p.ApprovalPrompt)
|
2015-06-06 18:15:43 +00:00
|
|
|
params.Add("scope", p.Scope)
|
2015-06-23 17:23:47 +00:00
|
|
|
params.Set("client_id", p.ClientID)
|
|
|
|
params.Set("response_type", "code")
|
2017-03-28 01:14:38 +00:00
|
|
|
params.Add("state", state)
|
2015-06-23 17:23:47 +00:00
|
|
|
a.RawQuery = params.Encode()
|
|
|
|
return a.String()
|
2015-06-06 18:15:43 +00:00
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
|
|
|
|
// CookieForSession serializes a session state for storage in a cookie
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *ProviderData) CookieForSession(s *sessions.SessionState, c *cookie.Cipher) (string, error) {
|
2015-06-23 11:23:39 +00:00
|
|
|
return s.EncodeSessionState(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionFromCookie deserializes a session from a cookie value
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *ProviderData) SessionFromCookie(v string, c *cookie.Cipher) (s *sessions.SessionState, err error) {
|
|
|
|
return sessions.DecodeSessionState(v, c)
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// GetEmailAddress returns the Account email address
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *ProviderData) GetEmailAddress(s *sessions.SessionState) (string, error) {
|
2015-06-23 11:23:39 +00:00
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-09-26 21:31:27 +00:00
|
|
|
// GetUserName returns the Account username
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *ProviderData) GetUserName(s *sessions.SessionState) (string, error) {
|
2017-09-26 21:31:27 +00:00
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2015-08-20 10:07:02 +00:00
|
|
|
// ValidateGroup validates that the provided email exists in the configured provider
|
|
|
|
// email group(s).
|
|
|
|
func (p *ProviderData) ValidateGroup(email string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// ValidateSessionState validates the AccessToken
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *ProviderData) ValidateSessionState(s *sessions.SessionState) bool {
|
2015-06-23 11:23:39 +00:00
|
|
|
return validateToken(p, s.AccessToken, nil)
|
|
|
|
}
|
|
|
|
|
2018-11-29 14:26:41 +00:00
|
|
|
// RefreshSessionIfNeeded should refresh the user's session if required and
|
|
|
|
// do nothing if a refresh is not required
|
2019-05-05 12:33:13 +00:00
|
|
|
func (p *ProviderData) RefreshSessionIfNeeded(s *sessions.SessionState) (bool, error) {
|
2015-06-23 11:23:39 +00:00
|
|
|
return false, nil
|
|
|
|
}
|