1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-12-12 11:15:02 +02:00
oauth2-proxy/providers/provider_default.go

115 lines
3.3 KiB
Go
Raw Normal View History

2015-05-21 05:23:48 +02:00
package providers
import (
"bytes"
"context"
2015-05-21 05:23:48 +02:00
"errors"
2015-06-06 20:15:43 +02:00
"fmt"
2015-05-21 05:23:48 +02:00
"net/url"
2019-05-07 16:32:46 +02:00
"time"
"github.com/coreos/go-oidc"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
2015-05-21 05:23:48 +02:00
)
var _ Provider = (*ProviderData)(nil)
// Redeem provides a default implementation of the OAuth2 token redemption process
func (p *ProviderData) Redeem(ctx context.Context, redirectURL, code string) (s *sessions.SessionState, err error) {
2015-05-21 05:23:48 +02:00
if code == "" {
err = errors.New("missing code")
return
}
clientSecret, err := p.GetClientSecret()
if err != nil {
return
}
2015-05-21 05:23:48 +02:00
params := url.Values{}
params.Add("redirect_uri", redirectURL)
2015-05-21 05:23:48 +02:00
params.Add("client_id", p.ClientID)
params.Add("client_secret", clientSecret)
2015-05-21 05:23:48 +02:00
params.Add("code", code)
params.Add("grant_type", "authorization_code")
2015-11-09 10:28:34 +02:00
if p.ProtectedResource != nil && p.ProtectedResource.String() != "" {
params.Add("resource", p.ProtectedResource.String())
}
2020-07-06 18:42:26 +02:00
result := requests.New(p.RedeemURL.String()).
WithContext(ctx).
WithMethod("POST").
WithBody(bytes.NewBufferString(params.Encode())).
SetHeader("Content-Type", "application/x-www-form-urlencoded").
Do()
2020-07-06 18:42:26 +02:00
if result.Error() != nil {
return nil, result.Error()
2015-06-06 20:15:43 +02:00
}
2015-05-21 05:23:48 +02:00
// blindly try json and x-www-form-urlencoded
var jsonResponse struct {
AccessToken string `json:"access_token"`
}
2020-07-06 18:42:26 +02:00
err = result.UnmarshalInto(&jsonResponse)
2015-05-21 05:23:48 +02:00
if err == nil {
2019-05-05 14:33:13 +02:00
s = &sessions.SessionState{
AccessToken: jsonResponse.AccessToken,
}
return
2015-05-21 05:23:48 +02:00
}
var v url.Values
2020-07-06 18:42:26 +02:00
v, err = url.ParseQuery(string(result.Body()))
if err != nil {
return
}
if a := v.Get("access_token"); a != "" {
created := time.Now()
s = &sessions.SessionState{AccessToken: a, CreatedAt: &created}
} else {
2020-07-06 18:42:26 +02:00
err = fmt.Errorf("no access token found %s", result.Body())
}
return
2015-05-21 05:23:48 +02:00
}
2015-06-06 20:15:43 +02:00
// GetLoginURL with typical oauth parameters
func (p *ProviderData) GetLoginURL(redirectURI, state string) string {
extraParams := url.Values{}
a := makeLoginURL(p, redirectURI, state, extraParams)
return a.String()
2015-06-06 20:15:43 +02:00
}
// GetEmailAddress returns the Account email address
func (p *ProviderData) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
return "", errors.New("not implemented")
}
// GetUserName returns the Account username
func (p *ProviderData) GetUserName(ctx context.Context, s *sessions.SessionState) (string, error) {
return "", errors.New("not implemented")
}
// ValidateGroup validates that the provided email exists in the configured provider
// email group(s).
func (p *ProviderData) ValidateGroup(email string) bool {
return true
}
// ValidateSessionState validates the AccessToken
func (p *ProviderData) ValidateSessionState(ctx context.Context, s *sessions.SessionState) bool {
return validateToken(ctx, p, s.AccessToken, nil)
}
2018-11-29 16:26:41 +02:00
// RefreshSessionIfNeeded should refresh the user's session if required and
// do nothing if a refresh is not required
func (p *ProviderData) RefreshSessionIfNeeded(ctx context.Context, s *sessions.SessionState) (bool, error) {
return false, nil
}
2020-07-18 01:42:51 +02:00
// CreateSessionStateFromBearerToken should be implemented to allow providers
// to convert ID tokens into sessions
func (p *ProviderData) CreateSessionStateFromBearerToken(ctx context.Context, rawIDToken string, idToken *oidc.IDToken) (*sessions.SessionState, error) {
2020-07-18 01:42:51 +02:00
return nil, errors.New("not implemented")
}