Debian Codegraph @

internal/utils/http.go

raw
1 package utils
2
3 import (
4 "errors"
5 "fmt"
6 "net"
7 "net/http"
8 "time"
9 )
10
11 const (
12 oAuth2HTTPClientTimeout = 30 * time.Second
13 oAuth2HTTPDialTimeout = 30 * time.Second
14 oAuth2HTTPKeepAlive = 30 * time.Second
15 oAuth2HTTPIdleConnTimeout = 90 * time.Second
16 oAuth2HTTPHandshakeTimeout = 10 * time.Second
17 oAuth2HTTPContinueTimeout = 1 * time.Second
18 oAuth2HTTPMaxIdleConns = 128
19 oAuth2HTTPMaxIdleConnsPerHost = 32
20 )
21
22 var (
23 ErrOAuth2ProviderRedirectHost = errors.New("oauth2 provider redirect changes host")
24 ErrOAuth2ProviderRedirectDowngrade = errors.New("oauth2 provider redirect downgrades HTTPS")
25 )
26
27 type UserAgentTransport struct {
28 rt http.RoundTripper
29 }
30
31 // NewUserAgentTransport returns a transport that sets a default User-Agent
32 // header for all requests. If rt is nil the http.DefaultTransport is used.
33 func NewUserAgentTransport(rt http.RoundTripper) *UserAgentTransport {
34 if rt == nil {
35 rt = http.DefaultTransport
36 }
37
38 return &UserAgentTransport{rt}
39 }
40
41 // NewOAuth2HTTPClient returns the outbound HTTP client used for provider calls.
42 func NewOAuth2HTTPClient(rt http.RoundTripper) *http.Client {
43 if rt == nil {
44 rt = newOAuth2HTTPTransport()
45 }
46
47 return &http.Client{
48 Transport: NewUserAgentTransport(rt),
49 CheckRedirect: CheckOAuth2ProviderRedirect,
50 Timeout: oAuth2HTTPClientTimeout,
51 }
52 }
53
54 func newOAuth2HTTPTransport() *http.Transport {
55 return &http.Transport{
56 Proxy: http.ProxyFromEnvironment,
57 DialContext: (&net.Dialer{
58 Timeout: oAuth2HTTPDialTimeout,
59 KeepAlive: oAuth2HTTPKeepAlive,
60 }).DialContext,
61 ForceAttemptHTTP2: true,
62 MaxIdleConns: oAuth2HTTPMaxIdleConns,
63 MaxIdleConnsPerHost: oAuth2HTTPMaxIdleConnsPerHost,
64 IdleConnTimeout: oAuth2HTTPIdleConnTimeout,
65 TLSHandshakeTimeout: oAuth2HTTPHandshakeTimeout,
66 ExpectContinueTimeout: oAuth2HTTPContinueTimeout,
67 }
68 }
69
70 // CheckOAuth2ProviderRedirect restricts provider redirects to the original host.
71 func CheckOAuth2ProviderRedirect(req *http.Request, via []*http.Request) error {
72 if len(via) == 0 {
73 return nil
74 }
75
76 originalURL := via[0].URL
77
78 if req.URL.Host != originalURL.Host {
79 return fmt.Errorf("%w: %s to %s", ErrOAuth2ProviderRedirectHost, originalURL.Host, req.URL.Host)
80 }
81
82 if originalURL.Scheme == "https" && req.URL.Scheme != "https" {
83 return fmt.Errorf("%w: %s to %s", ErrOAuth2ProviderRedirectDowngrade, originalURL.Scheme, req.URL.Scheme)
84 }
85
86 if len(via) >= 10 {
87 return errors.New("stopped after 10 redirects")
88 }
89
90 return nil
91 }
92
93 // RoundTrip implements http.RoundTripper, adding the User-Agent header before
94 // delegating to the wrapped RoundTripper.
95 func (adt *UserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
96 req.Header.Add("User-Agent", "openvpn-auth-oauth2")
97
98 return adt.rt.RoundTrip(req) //nolint: wrapcheck
99 }