Remove the custom constrtouctor and replace with an optional argument to override the discovery endpoit

This commit is contained in:
ydris 2022-02-16 08:50:05 +01:00
parent 4d2d193d1c
commit 713a082da1
2 changed files with 20 additions and 43 deletions

View file

@ -26,8 +26,13 @@ var (
) )
//Discover calls the discovery endpoint of the provided issuer and returns its configuration //Discover calls the discovery endpoint of the provided issuer and returns its configuration
func Discover(issuer string, httpClient *http.Client) (*oidc.DiscoveryConfiguration, error) { //It accepts an optional argument "wellknownUrl" which can be used to overide the dicovery endpoint url
func Discover(issuer string, httpClient *http.Client, wellKnownUrl ...string) (*oidc.DiscoveryConfiguration, error) {
wellKnown := strings.TrimSuffix(issuer, "/") + oidc.DiscoveryEndpoint wellKnown := strings.TrimSuffix(issuer, "/") + oidc.DiscoveryEndpoint
if len(wellKnownUrl) == 1 && wellKnownUrl[0] != "" {
wellKnown = wellKnownUrl[0]
}
req, err := http.NewRequest("GET", wellKnown, nil) req, err := http.NewRequest("GET", wellKnown, nil)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -69,11 +69,12 @@ var (
) )
type relyingParty struct { type relyingParty struct {
issuer string issuer string
endpoints Endpoints DiscoveryEndpoint string
oauthConfig *oauth2.Config endpoints Endpoints
oauth2Only bool oauthConfig *oauth2.Config
pkce bool oauth2Only bool
pkce bool
httpClient *http.Client httpClient *http.Client
cookieHandler *httphelper.CookieHandler cookieHandler *httphelper.CookieHandler
@ -170,7 +171,7 @@ func NewRelyingPartyOIDC(issuer, clientID, clientSecret, redirectURI string, sco
return nil, err return nil, err
} }
} }
discoveryConfiguration, err := client.Discover(rp.issuer, rp.httpClient) discoveryConfiguration, err := client.Discover(rp.issuer, rp.httpClient, rp.DiscoveryEndpoint)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -181,45 +182,16 @@ func NewRelyingPartyOIDC(issuer, clientID, clientSecret, redirectURI string, sco
return rp, nil return rp, nil
} }
//NewRelyingPartyOIDCWithCustomEndpoints creates an (OIDC) RelyingParty with the given
//discoveryConfiguration, clientID, clientSecret, redirectURI, scopes and other possible configOptions
//it will use the provided end points
//This is usefull when the server does not use standard endpoint paths
func NewRelyingPartyOIDCWithCustomEndpoints(
discoveryConfiguration *oidc.DiscoveryConfiguration,
clientID,
clientSecret,
redirectURI string,
scopes []string,
options ...Option) (RelyingParty, error) {
rp := &relyingParty{
issuer: discoveryConfiguration.Issuer,
oauthConfig: &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirectURI,
Scopes: scopes,
},
httpClient: httphelper.DefaultHTTPClient,
oauth2Only: false,
}
for _, optFunc := range options {
if err := optFunc(rp); err != nil {
return nil, err
}
}
endpoints := GetEndpoints(discoveryConfiguration)
rp.oauthConfig.Endpoint = endpoints.Endpoint
rp.endpoints = endpoints
return rp, nil
}
//Option is the type for providing dynamic options to the relyingParty //Option is the type for providing dynamic options to the relyingParty
type Option func(*relyingParty) error type Option func(*relyingParty) error
func WithCustomDiscoveryUrl(url string) Option {
return func(rp *relyingParty) error {
rp.DiscoveryEndpoint = url
return nil
}
}
//WithCookieHandler set a `CookieHandler` for securing the various redirects //WithCookieHandler set a `CookieHandler` for securing the various redirects
func WithCookieHandler(cookieHandler *httphelper.CookieHandler) Option { func WithCookieHandler(cookieHandler *httphelper.CookieHandler) Option {
return func(rp *relyingParty) error { return func(rp *relyingParty) error {