X Tutup
Skip to content

Commit 33fd6b1

Browse files
committed
Change API authentication to allow asset downloads
We install an HTTP middleware that adds the "Authorization" header on every HTTP request. However, our asset download process might redirect to a 3rd-party host (Amazon S3) and we want to allow those requests but not require that they are authenticated. Furthermore, we need the ability to specify the `Accept` request header without it being overwritten by middleware, so now middleware only adds headers that are not present in a request.
1 parent a00d927 commit 33fd6b1

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

api/client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ func NewClientFromHTTP(httpClient *http.Client) *Client {
4545
func AddHeader(name, value string) ClientOption {
4646
return func(tr http.RoundTripper) http.RoundTripper {
4747
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
48-
req.Header.Add(name, value)
48+
if len(req.Header.Values(name)) == 0 {
49+
req.Header.Add(name, value)
50+
}
4951
return tr.RoundTrip(req)
5052
}}
5153
}
@@ -55,11 +57,16 @@ func AddHeader(name, value string) ClientOption {
5557
func AddHeaderFunc(name string, getValue func(*http.Request) (string, error)) ClientOption {
5658
return func(tr http.RoundTripper) http.RoundTripper {
5759
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
60+
if len(req.Header.Values(name)) > 0 {
61+
return tr.RoundTrip(req)
62+
}
5863
value, err := getValue(req)
5964
if err != nil {
6065
return nil, err
6166
}
62-
req.Header.Add(name, value)
67+
if value != "" {
68+
req.Header.Add(name, value)
69+
}
6370
return tr.RoundTrip(req)
6471
}}
6572
}

pkg/cmd/factory/http.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package factory
22

33
import (
4-
"errors"
54
"fmt"
65
"net/http"
76
"os"
@@ -30,20 +29,8 @@ func httpClient(io *iostreams.IOStreams, cfg config.Config, appVersion string, s
3029

3130
hostname := ghinstance.NormalizeHostname(req.URL.Hostname())
3231
token, err := cfg.Get(hostname, "oauth_token")
33-
if token == "" {
34-
var notFound *config.NotFoundError
35-
// TODO: check if stdout is TTY too
36-
if errors.As(err, &notFound) && io.IsStdinTTY() {
37-
// interactive OAuth flow
38-
token, err = config.AuthFlowWithConfig(cfg, hostname, "Notice: authentication required", nil)
39-
}
40-
if err != nil {
41-
return "", err
42-
}
43-
if token == "" {
44-
// TODO: instruct user how to manually authenticate
45-
return "", fmt.Errorf("authentication required for %s", hostname)
46-
}
32+
if err != nil || token == "" {
33+
return "", nil
4734
}
4835

4936
return fmt.Sprintf("token %s", token), nil

0 commit comments

Comments
 (0)
X Tutup