Skip to content

Commit 278fc77

Browse files
committed
OAuth transport and use when available in api client
1 parent 61e0172 commit 278fc77

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.
@@ -123,15 +125,25 @@ type config struct {
123125

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

137149
// 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)