Skip to content

Commit f864530

Browse files
authored
oidc: Allow manually configured client, secret mutation (#37)
Allow the creation of a client from statically known values, rather than via discovery. Also allow the client secret to be changed as needed on an existing client.
1 parent cbeb86b commit f864530

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

client.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,33 @@ func WithACRValues(acrValues []string, enforce bool) ClientOpt {
5555
}
5656
}
5757

58+
// DiscoverClient will create a client based on the OIDC discovery of the given
59+
// issuer. It will use the returned information to configure the client, and
60+
// will use it to create a KeySource that discovers published keys as needed.
5861
func DiscoverClient(ctx context.Context, issuer, clientID, clientSecret, redirectURL string, opts ...ClientOpt) (*Client, error) {
5962
cl, err := discovery.NewClient(ctx, issuer)
6063
if err != nil {
6164
return nil, fmt.Errorf("creating discovery client: %v", err)
6265
}
6366

67+
return NewClient(cl.Metadata(), cl, clientID, clientSecret, redirectURL, opts...), nil
68+
}
69+
70+
// NewClient creates a client directly from the passed in information
71+
func NewClient(md *discovery.ProviderMetadata, ks KeySource, clientID, clientSecret, redirectURL string, opts ...ClientOpt) *Client {
6472
c := &Client{
6573
Verifier: Verifier{
66-
md: cl.Metadata(),
67-
ks: cl,
74+
md: md,
75+
ks: ks,
6876
},
69-
md: cl.Metadata(),
70-
ks: cl,
77+
md: md,
78+
ks: ks,
7179
o2cfg: oauth2.Config{
7280
ClientID: clientID,
7381
ClientSecret: clientSecret,
7482
Endpoint: oauth2.Endpoint{
75-
AuthURL: cl.Metadata().AuthorizationEndpoint,
76-
TokenURL: cl.Metadata().TokenEndpoint,
83+
AuthURL: md.AuthorizationEndpoint,
84+
TokenURL: md.TokenEndpoint,
7785
},
7886
Scopes: []string{"openid"},
7987
RedirectURL: redirectURL,
@@ -84,7 +92,7 @@ func DiscoverClient(ctx context.Context, issuer, clientID, clientSecret, redirec
8492
o(c)
8593
}
8694

87-
return c, nil
95+
return c
8896
}
8997

9098
type authCodeCfg struct {
@@ -162,6 +170,11 @@ func (c *Client) SetRedirectURL(redirectURL string) {
162170
c.o2cfg.RedirectURL = redirectURL
163171
}
164172

173+
// SetClientSecret updates the oauth2 client secret this client is configured for.
174+
func (c *Client) SetClientSecret(secret string) {
175+
c.o2cfg.ClientSecret = secret
176+
}
177+
165178
// Exchange the returned code for a set of tokens. If the exchange fails and
166179
// returns an oauth2 error response, the returned error will be an
167180
// `*github.com/parot/oidc/oauth2.TokenError`. If a HTTP error occurs, a

0 commit comments

Comments
 (0)