Skip to content

Commit 5222f66

Browse files
committed
Add comments
1 parent 7cb0e46 commit 5222f66

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

coderd/externalauth/externalauth.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,16 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.ExternalAut
300300
if err != nil {
301301
return nil, err
302302
}
303-
resp, err := c.Cfg.Do(ctx, "AuthorizeDevice", req)
303+
304+
do := http.DefaultClient.Do
305+
if c.Cfg != nil {
306+
// The cfg can be nil in unit tests.
307+
do = func(req *http.Request) (*http.Response, error) {
308+
return c.Cfg.Do(ctx, "AuthorizeDevice", req)
309+
}
310+
}
311+
312+
resp, err := do(req)
304313
req.Header.Set("Accept", "application/json")
305314
if err != nil {
306315
return nil, err

coderd/promoauth/oauth2.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ type OAuth2Config interface {
2121
// InstrumentedOAuth2Config extends OAuth2Config with a `Do` method that allows
2222
// external oauth related calls to be instrumented. This is to support
2323
// "ValidateToken" which is not an oauth2 specified method.
24+
// These calls still count against the api rate limit, and should be instrumented.
2425
type InstrumentedOAuth2Config interface {
2526
OAuth2Config
2627

2728
// Do is provided as a convenience method to make a request with the oauth2 client.
2829
// It mirrors `http.Client.Do`.
29-
// We need this because Coder adds some extra functionality to
30-
// oauth clients such as the `ValidateToken()` method.
3130
Do(ctx context.Context, source string, req *http.Request) (*http.Response, error)
3231
}
3332

3433
var _ OAuth2Config = (*Config)(nil)
3534

35+
// Factory allows us to have 1 set of metrics for all oauth2 providers.
36+
// Primarily to avoid any prometheus errors registering duplicate metrics.
3637
type Factory struct {
3738
metrics *metrics
3839
}
@@ -107,10 +108,11 @@ func (c *Config) wrapClient(ctx context.Context, source string) context.Context
107108
return context.WithValue(ctx, oauth2.HTTPClient, c.oauthHTTPClient(ctx, source))
108109
}
109110

111+
// oauthHTTPClient returns an http client that will instrument every request made.
110112
func (c *Config) oauthHTTPClient(ctx context.Context, source string) *http.Client {
111113
cli := &http.Client{}
112114

113-
// Check if the context has an http client already.
115+
// Check if the context has a http client already.
114116
if hc, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); ok {
115117
cli = hc
116118
}
@@ -126,6 +128,8 @@ type instrumentedTripper struct {
126128
underlying http.RoundTripper
127129
}
128130

131+
// newInstrumentedTripper intercepts a http request, and increments the
132+
// externalRequestCount metric.
129133
func newInstrumentedTripper(c *Config, source string, under http.RoundTripper) *instrumentedTripper {
130134
if under == nil {
131135
under = http.DefaultTransport

0 commit comments

Comments
 (0)