Skip to content

Commit

Permalink
chore(http): revert to use http header callback for Authorization (#193)
Browse files Browse the repository at this point in the history
Because

- refresh token isn't refreshed when access token is expired due to the
http header `Authorization` callback was overridden

This commit

- revert back to the previous logic
- enable non-blocking status checks
  • Loading branch information
pinglin committed Oct 6, 2023
1 parent bbffb8b commit 81f6573
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .github/.codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
informational: true
patch:
default:
informational: true
2 changes: 0 additions & 2 deletions codecov.yml

This file was deleted.

25 changes: 2 additions & 23 deletions pkg/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -49,21 +48,8 @@ type ApiOptions struct {
HTTPClient func() (*http.Client, error)
}

var logger *slog.Logger

func init() {
var lvl = new(slog.LevelVar)
if os.Getenv("DEBUG") != "" {
lvl.Set(slog.LevelDebug)
} else {
lvl.Set(slog.LevelError + 1)
}
logger = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: lvl,
}))
}

func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command {
// NewCmdAPI creates a new command
func NewCmdAPI(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command {
opts := ApiOptions{
IO: f.IOStreams,
Config: f.Config,
Expand Down Expand Up @@ -244,13 +230,6 @@ func apiRun(opts *ApiOptions) error {
defer opts.IO.StopPager()
}

if host.AccessToken != "" {
requestHeaders = append(requestHeaders, "Authorization: Bearer "+host.AccessToken)
}

logger.Debug("api request", "host", host.APIHostname, "path", requestPath)

// http request & output
template := export.NewTemplate(opts.IO, opts.Template)
resp, err := httpRequest(httpClient, host.APIHostname, method, requestPath, requestBody, requestHeaders)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Test_NewCmdApi(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var opts *ApiOptions
cmd := NewCmdApi(f, func(o *ApiOptions) error {
cmd := NewCmdAPI(f, func(o *ApiOptions) error {
opts = o
return nil
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/factory/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg configHTTPClient, appVersion str
api.AddHeaderFunc("Authorization", func(req *http.Request) (string, error) {
hostname := getHost(req)
if accessToken, err := cfg.Get(hostname, "access_token"); err == nil && accessToken != "" {
// Refresh access token everytime
// Refresh access token every time
if accessToken, err = oauth2.RefreshToken(cfg, hostname); err == nil && accessToken != "" {
return fmt.Sprintf("bearer %s", accessToken), nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
bareHTTPCmdFactory := *f
bareHTTPCmdFactory.HTTPClient = bareHTTPClient(f, version)

cmd.AddCommand(apiCmd.NewCmdApi(&bareHTTPCmdFactory, nil))
cmd.AddCommand(apiCmd.NewCmdAPI(&bareHTTPCmdFactory, nil))

// Help topics
cmd.AddCommand(NewHelpTopic("environment"))
Expand Down

0 comments on commit 81f6573

Please sign in to comment.