2015-05-20 23:23:48 -04:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-06 00:53:33 +09:00
|
|
|
"context"
|
2015-05-20 23:23:48 -04:00
|
|
|
"errors"
|
2015-06-06 14:15:43 -04:00
|
|
|
"fmt"
|
2015-05-20 23:23:48 -04:00
|
|
|
"net/url"
|
2019-05-07 15:32:46 +01:00
|
|
|
"time"
|
2015-06-23 07:23:39 -04:00
|
|
|
|
2020-04-28 08:46:46 +02:00
|
|
|
"github.com/coreos/go-oidc"
|
|
|
|
|
2020-09-30 01:44:42 +09:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
|
2015-05-20 23:23:48 -04:00
|
|
|
)
|
|
|
|
|
2020-09-26 14:06:52 -07:00
|
|
|
var (
|
|
|
|
// ErrNotImplemented is returned when a provider did not override a default
|
|
|
|
// implementation method that doesn't have sensible defaults
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
|
|
|
|
|
|
|
_ Provider = (*ProviderData)(nil)
|
|
|
|
)
|
2020-05-06 00:53:33 +09:00
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// Redeem provides a default implementation of the OAuth2 token redemption process
|
2020-05-06 00:53:33 +09:00
|
|
|
func (p *ProviderData) Redeem(ctx context.Context, redirectURL, code string) (s *sessions.SessionState, err error) {
|
2015-05-20 23:23:48 -04:00
|
|
|
if code == "" {
|
|
|
|
err = errors.New("missing code")
|
|
|
|
return
|
|
|
|
}
|
2020-02-15 14:44:39 +01:00
|
|
|
clientSecret, err := p.GetClientSecret()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-05-20 23:23:48 -04:00
|
|
|
|
|
|
|
params := url.Values{}
|
2015-11-09 00:47:44 +01:00
|
|
|
params.Add("redirect_uri", redirectURL)
|
2015-05-20 23:23:48 -04:00
|
|
|
params.Add("client_id", p.ClientID)
|
2020-02-15 14:44:39 +01:00
|
|
|
params.Add("client_secret", clientSecret)
|
2015-05-20 23:23:48 -04:00
|
|
|
params.Add("code", code)
|
|
|
|
params.Add("grant_type", "authorization_code")
|
2015-11-09 09:28:34 +01:00
|
|
|
if p.ProtectedResource != nil && p.ProtectedResource.String() != "" {
|
|
|
|
params.Add("resource", p.ProtectedResource.String())
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:42:26 +01:00
|
|
|
result := requests.New(p.RedeemURL.String()).
|
2020-07-03 19:27:25 +01:00
|
|
|
WithContext(ctx).
|
|
|
|
WithMethod("POST").
|
|
|
|
WithBody(bytes.NewBufferString(params.Encode())).
|
|
|
|
SetHeader("Content-Type", "application/x-www-form-urlencoded").
|
|
|
|
Do()
|
2020-07-06 17:42:26 +01:00
|
|
|
if result.Error() != nil {
|
|
|
|
return nil, result.Error()
|
2015-06-06 14:15:43 -04:00
|
|
|
}
|
|
|
|
|
2015-05-20 23:23:48 -04:00
|
|
|
// blindly try json and x-www-form-urlencoded
|
|
|
|
var jsonResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
}
|
2020-07-06 17:42:26 +01:00
|
|
|
err = result.UnmarshalInto(&jsonResponse)
|
2015-05-20 23:23:48 -04:00
|
|
|
if err == nil {
|
2019-05-05 13:33:13 +01:00
|
|
|
s = &sessions.SessionState{
|
2015-06-23 07:23:39 -04:00
|
|
|
AccessToken: jsonResponse.AccessToken,
|
|
|
|
}
|
|
|
|
return
|
2015-05-20 23:23:48 -04:00
|
|
|
}
|
|
|
|
|
2015-06-23 07:23:39 -04:00
|
|
|
var v url.Values
|
2020-07-06 17:42:26 +01:00
|
|
|
v, err = url.ParseQuery(string(result.Body()))
|
2015-06-23 07:23:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if a := v.Get("access_token"); a != "" {
|
2020-05-30 08:53:38 +01:00
|
|
|
created := time.Now()
|
|
|
|
s = &sessions.SessionState{AccessToken: a, CreatedAt: &created}
|
2015-06-23 07:23:39 -04:00
|
|
|
} else {
|
2020-07-06 17:42:26 +01:00
|
|
|
err = fmt.Errorf("no access token found %s", result.Body())
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
|
|
|
return
|
2015-05-20 23:23:48 -04:00
|
|
|
}
|
2015-06-06 14:15:43 -04:00
|
|
|
|
2020-09-14 13:22:53 +02:00
|
|
|
// GetLoginURL with typical oauth parameters
|
|
|
|
func (p *ProviderData) GetLoginURL(redirectURI, state string) string {
|
2020-09-15 10:20:10 +02:00
|
|
|
extraParams := url.Values{}
|
|
|
|
a := makeLoginURL(p, redirectURI, state, extraParams)
|
2015-06-23 13:23:47 -04:00
|
|
|
return a.String()
|
2015-06-06 14:15:43 -04:00
|
|
|
}
|
2015-06-23 07:23:39 -04:00
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// GetEmailAddress returns the Account email address
|
2020-09-27 11:46:29 -07:00
|
|
|
// DEPRECATED: Migrate to EnrichSessionState
|
|
|
|
func (p *ProviderData) GetEmailAddress(_ context.Context, _ *sessions.SessionState) (string, error) {
|
2020-09-26 14:06:52 -07:00
|
|
|
return "", ErrNotImplemented
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
|
|
|
|
2015-08-20 03:07:02 -07:00
|
|
|
// ValidateGroup validates that the provided email exists in the configured provider
|
|
|
|
// email group(s).
|
2020-09-27 11:46:29 -07:00
|
|
|
func (p *ProviderData) ValidateGroup(_ string) bool {
|
2015-08-20 03:07:02 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:46:29 -07:00
|
|
|
// EnrichSessionState is called after Redeem to allow providers to enrich session fields
|
|
|
|
// such as User, Email, Groups with provider specific API calls.
|
|
|
|
func (p *ProviderData) EnrichSessionState(_ context.Context, _ *sessions.SessionState) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// ValidateSessionState validates the AccessToken
|
2020-05-06 00:53:33 +09:00
|
|
|
func (p *ProviderData) ValidateSessionState(ctx context.Context, s *sessions.SessionState) bool {
|
|
|
|
return validateToken(ctx, p, s.AccessToken, nil)
|
2015-06-23 07:23:39 -04:00
|
|
|
}
|
|
|
|
|
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
|
2020-09-27 11:46:29 -07:00
|
|
|
func (p *ProviderData) RefreshSessionIfNeeded(_ context.Context, _ *sessions.SessionState) (bool, error) {
|
2015-06-23 07:23:39 -04:00
|
|
|
return false, nil
|
|
|
|
}
|
2020-04-28 08:46:46 +02:00
|
|
|
|
2020-07-18 00:42:51 +01:00
|
|
|
// CreateSessionStateFromBearerToken should be implemented to allow providers
|
|
|
|
// to convert ID tokens into sessions
|
2020-09-27 11:46:29 -07:00
|
|
|
func (p *ProviderData) CreateSessionStateFromBearerToken(_ context.Context, _ string, _ *oidc.IDToken) (*sessions.SessionState, error) {
|
2020-09-26 14:06:52 -07:00
|
|
|
return nil, ErrNotImplemented
|
2020-04-28 08:46:46 +02:00
|
|
|
}
|