Skip to content

Commit 17982da

Browse files
committed
chore(oauth): refactor OAuth handler to support functional configuration options
- Refactor NewOAuthHandler to accept functional options for configuration - Add support for injecting a custom HTTP client via WithOAuthHTTPClient option fix #440 Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 8f5b048 commit 17982da

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

client/transport/oauth.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,34 @@ type OAuthHandler struct {
118118
expectedState string // Expected state value for CSRF protection
119119
}
120120

121-
// NewOAuthHandler creates a new OAuth handler
122-
func NewOAuthHandler(config OAuthConfig) *OAuthHandler {
121+
type OAuthHandlerOption func(*OAuthHandler)
122+
123+
// WithOAuthHTTPClient allows setting a custom http.Client for the OAuthHandler.
124+
func WithOAuthHTTPClient(client *http.Client) OAuthHandlerOption {
125+
return func(h *OAuthHandler) {
126+
if client != nil {
127+
h.httpClient = client
128+
}
129+
}
130+
}
131+
132+
// NewOAuthHandler creates a new OAuth handler.
133+
// Optionally accepts functional options such as WithOAuthHTTPClient.
134+
func NewOAuthHandler(config OAuthConfig, opts ...OAuthHandlerOption) *OAuthHandler {
123135
if config.TokenStore == nil {
124136
config.TokenStore = NewMemoryTokenStore()
125137
}
126138

127-
return &OAuthHandler{
139+
handler := &OAuthHandler{
128140
config: config,
129141
httpClient: &http.Client{Timeout: 30 * time.Second},
130142
}
143+
144+
for _, opt := range opts {
145+
opt(handler)
146+
}
147+
148+
return handler
131149
}
132150

133151
// GetAuthorizationHeader returns the Authorization header value for a request

0 commit comments

Comments
 (0)