2015-03-30 21:30:27 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2020-02-15 15:44:39 +02:00
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
2015-03-30 21:30:27 +02:00
|
|
|
"net/url"
|
2020-03-14 11:58:29 +02:00
|
|
|
|
2020-11-16 04:57:48 +02:00
|
|
|
"github.com/coreos/go-oidc"
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
2015-03-30 21:30:27 +02:00
|
|
|
)
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// ProviderData contains information required to configure all implementations
|
|
|
|
// of OAuth2 providers
|
2015-03-30 21:30:27 +02:00
|
|
|
type ProviderData struct {
|
2015-11-09 10:28:34 +02:00
|
|
|
ProviderName string
|
|
|
|
LoginURL *url.URL
|
|
|
|
RedeemURL *url.URL
|
|
|
|
ProfileURL *url.URL
|
|
|
|
ProtectedResource *url.URL
|
|
|
|
ValidateURL *url.URL
|
2020-03-17 19:57:33 +02:00
|
|
|
// Auth request params & related, see
|
|
|
|
//https://openid.net/specs/openid-connect-basic-1_0.html#rfc.section.2.1.1.1
|
|
|
|
AcrValues string
|
|
|
|
ApprovalPrompt string // NOTE: Renamed to "prompt" in OAuth2
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
ClientSecretFile string
|
|
|
|
Scope string
|
|
|
|
Prompt string
|
2020-11-16 04:57:48 +02:00
|
|
|
Verifier *oidc.IDTokenVerifier
|
2020-09-27 04:00:44 +02:00
|
|
|
|
|
|
|
// Universal Group authorization data structure
|
|
|
|
// any provider can set to consume
|
|
|
|
AllowedGroups map[string]struct{}
|
2015-03-30 21:30:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-20 12:37:59 +02:00
|
|
|
// Data returns the ProviderData
|
2015-03-30 21:30:27 +02:00
|
|
|
func (p *ProviderData) Data() *ProviderData { return p }
|
2020-02-15 15:44:39 +02:00
|
|
|
|
2020-04-14 10:36:44 +02:00
|
|
|
func (p *ProviderData) GetClientSecret() (clientSecret string, err error) {
|
2020-02-15 15:44:39 +02:00
|
|
|
if p.ClientSecret != "" || p.ClientSecretFile == "" {
|
|
|
|
return p.ClientSecret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getting ClientSecret can fail in runtime so we need to report it without returning the file name to the user
|
|
|
|
fileClientSecret, err := ioutil.ReadFile(p.ClientSecretFile)
|
|
|
|
if err != nil {
|
2020-08-10 12:44:08 +02:00
|
|
|
logger.Errorf("error reading client secret file %s: %s", p.ClientSecretFile, err)
|
2020-02-15 15:44:39 +02:00
|
|
|
return "", errors.New("could not read client secret file")
|
|
|
|
}
|
|
|
|
return string(fileClientSecret), nil
|
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
|
2020-09-27 04:00:44 +02:00
|
|
|
// SetAllowedGroups organizes a group list into the AllowedGroups map
|
|
|
|
// to be consumed by Authorize implementations
|
|
|
|
func (p *ProviderData) SetAllowedGroups(groups []string) {
|
2020-09-27 04:24:06 +02:00
|
|
|
p.AllowedGroups = make(map[string]struct{}, len(groups))
|
2020-09-27 04:00:44 +02:00
|
|
|
for _, group := range groups {
|
|
|
|
p.AllowedGroups[group] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
type providerDefaults struct {
|
|
|
|
name string
|
|
|
|
loginURL *url.URL
|
|
|
|
redeemURL *url.URL
|
|
|
|
profileURL *url.URL
|
|
|
|
validateURL *url.URL
|
|
|
|
scope string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProviderData) setProviderDefaults(defaults providerDefaults) {
|
|
|
|
p.ProviderName = defaults.name
|
|
|
|
p.LoginURL = defaultURL(p.LoginURL, defaults.loginURL)
|
|
|
|
p.RedeemURL = defaultURL(p.RedeemURL, defaults.redeemURL)
|
|
|
|
p.ProfileURL = defaultURL(p.ProfileURL, defaults.profileURL)
|
|
|
|
p.ValidateURL = defaultURL(p.ValidateURL, defaults.validateURL)
|
|
|
|
|
|
|
|
if p.Scope == "" {
|
|
|
|
p.Scope = defaults.scope
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultURL will set return a default value if the given value is not set.
|
|
|
|
func defaultURL(u *url.URL, d *url.URL) *url.URL {
|
|
|
|
if u != nil && u.String() != "" {
|
|
|
|
// The value is already set
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the default is given, return that
|
|
|
|
if d != nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
return &url.URL{}
|
|
|
|
}
|