Skip to content

Commit 5c8a6d6

Browse files
committed
OAuth transport and use when available in api client
1 parent b1cbe42 commit 5c8a6d6

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

cmd/src/main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/sourcegraph/sourcegraph/lib/errors"
1616

1717
"github.com/sourcegraph/src-cli/internal/api"
18+
"github.com/sourcegraph/src-cli/internal/keyring"
19+
"github.com/sourcegraph/src-cli/internal/oauthdevice"
1820
)
1921

2022
const usageText = `src is a tool that provides access to Sourcegraph instances.
@@ -122,15 +124,25 @@ type config struct {
122124

123125
// apiClient returns an api.Client built from the configuration.
124126
func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
125-
return api.NewClient(api.ClientOpts{
127+
opts := api.ClientOpts{
126128
Endpoint: c.Endpoint,
127129
AccessToken: c.AccessToken,
128130
AdditionalHeaders: c.AdditionalHeaders,
129131
Flags: flags,
130132
Out: out,
131133
ProxyURL: c.ProxyURL,
132134
ProxyPath: c.ProxyPath,
133-
})
135+
}
136+
store, err := keyring.Open()
137+
if err != nil {
138+
panic("HALP")
139+
}
140+
141+
if t, err := oauthdevice.LoadToken(store, c.Endpoint); err == nil {
142+
opts.OAuthToken = t
143+
}
144+
145+
return api.NewClient(opts)
134146
}
135147

136148
// readConfig reads the config file from the given path.

internal/api/api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/kballard/go-shellquote"
1919
"github.com/mattn/go-isatty"
2020

21+
"github.com/sourcegraph/src-cli/internal/oauthdevice"
2122
"github.com/sourcegraph/src-cli/internal/version"
2223
)
2324

@@ -85,6 +86,8 @@ type ClientOpts struct {
8586

8687
ProxyURL *url.URL
8788
ProxyPath string
89+
90+
OAuthToken *oauthdevice.Token
8891
}
8992

9093
func buildTransport(opts ClientOpts, flags *Flags) *http.Transport {
@@ -102,6 +105,13 @@ func buildTransport(opts ClientOpts, flags *Flags) *http.Transport {
102105
transport = withProxyTransport(transport, opts.ProxyURL, opts.ProxyPath)
103106
}
104107

108+
if opt.AccessToken == "" && opt.OAuthToken != nil {
109+
transport = &oauthdevice.Transport{
110+
Base: transport,
111+
Token: opts.OAuthToken
112+
}
113+
}
114+
105115
return transport
106116
}
107117

internal/oauthdevice/http_transport.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ var _ http.RoundTripper = (*Transport)(nil)
1212

1313
type Transport struct {
1414
Base http.RoundTripper
15-
token *Token
15+
Token *Token
1616
}
1717

1818
// RoundTrip implements http.RoundTripper.
1919
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
2020
ctx := req.Context()
21-
token, err := maybeRefresh(ctx, t.token)
21+
token, err := maybeRefresh(ctx, t.Token)
2222
if err != nil {
2323
return nil, err
2424
}
25-
t.token = token
25+
t.Token = token
2626

2727
req2 := req.Clone(req.Context())
28-
req2.Header.Set("Authorization", "Bearer "+t.token.AccessToken)
28+
req2.Header.Set("Authorization", "Bearer "+t.Token.AccessToken)
2929

3030
if t.Base != nil {
3131
return t.Base.RoundTrip(req2)

0 commit comments

Comments
 (0)