-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
77 lines (65 loc) · 1.87 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package auth
import (
"context"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
// Oauth2Transport wraps oauth2.Transport to suspend CancelRequest.
type Oauth2Transport struct {
Transport oauth2.Transport
}
func (t *Oauth2Transport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.Transport.RoundTrip(req)
}
type OAuth2Shared struct {
Source oauth2.TokenSource
}
// RoundTripper returns a new RoundTripper that adds an OAuth2 Transport.
//
// If Source is nil, returns transport as-is.
func (o OAuth2Shared) RoundTripper(_ context.Context, transport http.RoundTripper) (http.RoundTripper, error) {
if o.Source == nil {
return transport, nil
}
return &Oauth2Transport{
Transport: oauth2.Transport{
Source: o.Source,
Base: transport,
},
}, nil
}
func (p *ProviderExtra) NewOauth2Shared(ctx context.Context) (*OAuth2Shared, error) {
cfg, err := p.ClientConfig()
if err != nil {
return nil, err
}
return &OAuth2Shared{
Source: oauth2.ReuseTokenSource(nil, cfg.TokenSource(ctx)),
}, nil
}
// RoundTripper returns a new RoundTripper that adds an OAuth2 Transport.
//
// Uses provider's ClientConfig.
func (p *ProviderExtra) RoundTripper(ctx context.Context, transport http.RoundTripper) (http.RoundTripper, error) {
cfg, err := p.ClientConfig()
if err != nil {
return nil, err
}
return &Oauth2Transport{
Transport: oauth2.Transport{
Source: oauth2.ReuseTokenSource(nil, cfg.TokenSource(ctx)),
Base: transport,
},
}, nil
}
func (p *ProviderExtra) RoundTripperWrapper(cfg *clientcredentials.Config) func(ctx context.Context, transport http.RoundTripper) http.RoundTripper {
return func(ctx context.Context, transport http.RoundTripper) http.RoundTripper {
return &Oauth2Transport{
Transport: oauth2.Transport{
Source: oauth2.ReuseTokenSource(nil, cfg.TokenSource(ctx)),
Base: transport,
},
}
}
}