Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9d1c76c

Browse files
committed
use consts for source labels
1 parentbfa427f commit9d1c76c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

‎coderd/externalauth/externalauth.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (c *Config) ValidateToken(ctx context.Context, token string) (bool, *coders
188188
}
189189

190190
req.Header.Set("Authorization",fmt.Sprintf("Bearer %s",token))
191-
res,err:=c.InstrumentedOAuth2Config.Do(ctx,"ValidateToken",req)
191+
res,err:=c.InstrumentedOAuth2Config.Do(ctx,promoauth.SourceValidateToken,req)
192192
iferr!=nil {
193193
returnfalse,nil,err
194194
}
@@ -238,7 +238,7 @@ func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk
238238
returnnil,false,err
239239
}
240240
req.Header.Set("Authorization",fmt.Sprintf("Bearer %s",token))
241-
res,err:=c.InstrumentedOAuth2Config.Do(ctx,"AppInstallations",req)
241+
res,err:=c.InstrumentedOAuth2Config.Do(ctx,promoauth.SourceAppInstallations,req)
242242
iferr!=nil {
243243
returnnil,false,err
244244
}
@@ -305,7 +305,7 @@ func (c *DeviceAuth) AuthorizeDevice(ctx context.Context) (*codersdk.ExternalAut
305305
ifc.Cfg!=nil {
306306
// The cfg can be nil in unit tests.
307307
do=func(req*http.Request) (*http.Response,error) {
308-
returnc.Cfg.Do(ctx,"AuthorizeDevice",req)
308+
returnc.Cfg.Do(ctx,promoauth.SourceAuthorizeDevice,req)
309309
}
310310
}
311311

‎coderd/promoauth/oauth2.go‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import (
1010
"golang.org/x/oauth2"
1111
)
1212

13+
typeOauth2Sourcestring
14+
15+
const (
16+
SourceValidateTokenOauth2Source="ValidateToken"
17+
SourceExchangeOauth2Source="Exchange"
18+
SourceTokenSourceOauth2Source="TokenSource"
19+
SourceAppInstallationsOauth2Source="AppInstallations"
20+
SourceAuthorizeDeviceOauth2Source="AuthorizeDevice"
21+
)
22+
1323
// OAuth2Config exposes a subset of *oauth2.Config functions for easier testing.
1424
// *oauth2.Config should be used instead of implementing this in production.
1525
typeOAuth2Configinterface {
@@ -27,7 +37,7 @@ type InstrumentedOAuth2Config interface {
2737

2838
// Do is provided as a convenience method to make a request with the oauth2 client.
2939
// It mirrors `http.Client.Do`.
30-
Do(ctx context.Context,sourcestring,req*http.Request) (*http.Response,error)
40+
Do(ctx context.Context,sourceOauth2Source,req*http.Request) (*http.Response,error)
3141
}
3242

3343
var_OAuth2Config= (*Config)(nil)
@@ -79,7 +89,7 @@ type Config struct {
7989
metrics*metrics
8090
}
8191

82-
func (c*Config)Do(ctx context.Context,sourcestring,req*http.Request) (*http.Response,error) {
92+
func (c*Config)Do(ctx context.Context,sourceOauth2Source,req*http.Request) (*http.Response,error) {
8393
cli:=c.oauthHTTPClient(ctx,source)
8494
returncli.Do(req)
8595
}
@@ -90,11 +100,11 @@ func (c *Config) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
90100
}
91101

92102
func (c*Config)Exchange(ctx context.Context,codestring,opts...oauth2.AuthCodeOption) (*oauth2.Token,error) {
93-
returnc.underlying.Exchange(c.wrapClient(ctx,"Exchange"),code,opts...)
103+
returnc.underlying.Exchange(c.wrapClient(ctx,SourceExchange),code,opts...)
94104
}
95105

96106
func (c*Config)TokenSource(ctx context.Context,token*oauth2.Token) oauth2.TokenSource {
97-
returnc.underlying.TokenSource(c.wrapClient(ctx,"TokenSource"),token)
107+
returnc.underlying.TokenSource(c.wrapClient(ctx,SourceTokenSource),token)
98108
}
99109

100110
// wrapClient is the only way we can accurately instrument the oauth2 client.
@@ -104,12 +114,12 @@ func (c *Config) TokenSource(ctx context.Context, token *oauth2.Token) oauth2.To
104114
// For example, the 'TokenSource' method will return a token
105115
// source that will make a network request when the 'Token' method is called on
106116
// it if the token is expired.
107-
func (c*Config)wrapClient(ctx context.Context,sourcestring) context.Context {
117+
func (c*Config)wrapClient(ctx context.Context,sourceOauth2Source) context.Context {
108118
returncontext.WithValue(ctx,oauth2.HTTPClient,c.oauthHTTPClient(ctx,source))
109119
}
110120

111121
// oauthHTTPClient returns an http client that will instrument every request made.
112-
func (c*Config)oauthHTTPClient(ctx context.Context,sourcestring)*http.Client {
122+
func (c*Config)oauthHTTPClient(ctx context.Context,sourceOauth2Source)*http.Client {
113123
cli:=&http.Client{}
114124

115125
// Check if the context has a http client already.
@@ -124,13 +134,13 @@ func (c *Config) oauthHTTPClient(ctx context.Context, source string) *http.Clien
124134

125135
typeinstrumentedTripperstruct {
126136
c*Config
127-
sourcestring
137+
sourceOauth2Source
128138
underlying http.RoundTripper
129139
}
130140

131141
// newInstrumentedTripper intercepts a http request, and increments the
132142
// externalRequestCount metric.
133-
funcnewInstrumentedTripper(c*Config,sourcestring,under http.RoundTripper)*instrumentedTripper {
143+
funcnewInstrumentedTripper(c*Config,sourceOauth2Source,under http.RoundTripper)*instrumentedTripper {
134144
ifunder==nil {
135145
under=http.DefaultTransport
136146
}
@@ -156,7 +166,7 @@ func (i *instrumentedTripper) RoundTrip(r *http.Request) (*http.Response, error)
156166
}
157167
i.c.metrics.externalRequestCount.With(prometheus.Labels{
158168
"name":i.c.name,
159-
"source":i.source,
169+
"source":string(i.source),
160170
"status_code":fmt.Sprintf("%d",statusCode),
161171
}).Inc()
162172
returnresp,err

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp