Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions bin/remove-queued-jobs.sh

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing this because I don't think it adds value

This file was deleted.

15 changes: 10 additions & 5 deletions pkg/auth/auth.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simple sentinel...I almost removed this altogether, but left it as it seems we still have many auth0 users

Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func NewOAuthRetriever(oauths []OAuth) *OAuthRetriever {
}
}

var ErrNoOAuthForToken = errors.New("no oauth found for token")

func (o *OAuthRetriever) GetByProvider(provider entity.CredentialProvider) (OAuth, error) {
for _, oauth := range o.oauths {
if oauth.GetCredentialProvider() == provider {
Expand All @@ -90,7 +92,7 @@ func (o *OAuthRetriever) GetByToken(token string) (OAuth, error) {
return oauth, nil
}
}
return nil, fmt.Errorf("no oauth found for token")
return nil, ErrNoOAuthForToken
}

type Auth struct {
Expand Down Expand Up @@ -568,11 +570,14 @@ func StandardLogin(authProvider string, email string, tokens *entity.AuthTokens)
})

if tokens != nil && tokens.AccessToken != "" && tokens.APIKey == "" {
authenticatorFromToken, errr := authRetriever.GetByToken(tokens.AccessToken)
if errr != nil {
fmt.Printf("%v\n", errr)
} else {
authenticatorFromToken, err := authRetriever.GetByToken(tokens.AccessToken)
switch {
case err == nil:
authenticator = authenticatorFromToken
case errors.Is(err, ErrNoOAuthForToken):
// make user re-authenticate rather than printing an error.
default:
breverrors.GetDefaultErrorReporter().ReportError(err)
}
}

Expand Down
11 changes: 2 additions & 9 deletions pkg/auth/auth0.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanliness

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"net/url"
"slices"
"strings"
"time"

Expand All @@ -18,8 +19,6 @@ import (
const (
audiencePath = "/api/v2/"
waitThresholdInSeconds = 1
// namespace used to set/get values from the keychain.
SecNamespace = "auth0-cli"
)

var requiredScopes = []string{
Expand Down Expand Up @@ -321,13 +320,7 @@ func (a Auth0Authenticator) GetNewAuthTokensWithRefresh(refreshToken string) (*e
}

func ErrorIfBadHTTP(r *http.Response, exceptStatus ...int) error {
shouldExcept := false
for _, s := range exceptStatus {
if r.StatusCode == s {
shouldExcept = true
break
}
}
shouldExcept := slices.Contains(exceptStatus, r.StatusCode)

if IsError(r.StatusCode) && !shouldExcept {
return NewHTTPResponseError(r)
Expand Down
32 changes: 32 additions & 0 deletions pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,38 @@ func TestStandardLogin_APIKeyCredentialDoesNotProbeOAuthProviders(t *testing.T)
assert.Empty(t, string(out))
}

func TestStandardLogin_UnknownCredentialDoesNotPrint(t *testing.T) {
for _, token := range []string{BrevAPIKeyPrefix + "legacy-key", "auto-login"} {
t.Run(token, func(t *testing.T) {
oldStdout := os.Stdout
t.Cleanup(func() {
os.Stdout = oldStdout
})
readPipe, writePipe, err := os.Pipe()
require.NoError(t, err)
os.Stdout = writePipe

authenticator := StandardLogin("", "", &entity.AuthTokens{
AccessToken: token,
RefreshToken: "refresh-token",
})

assert.NoError(t, writePipe.Close())
os.Stdout = oldStdout
out, err := io.ReadAll(readPipe)
assert.NoError(t, err)
assert.Empty(t, string(out))
_, ok := authenticator.(KasAuthenticator)
assert.True(t, ok, "unrecognized credential must fall back to the default provider")
})
}
}

func TestOAuthRetrieverGetByToken_NoMatchReturnsSentinel(t *testing.T) {
_, err := NewOAuthRetriever(nil).GetByToken("not-a-jwt")
assert.ErrorIs(t, err, ErrNoOAuthForToken)
}

func TestSuccessNoRefreshGetFreshAccessTokenOrLogin(t *testing.T) {
s := MockAuthStore{authTokens: &entity.AuthTokens{
AccessToken: validToken,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func NewBrevCommand() *cobra.Command { //nolint:funlen,gocognit,gocyclo // defin
}
}
if apiKeyFlag != "" {
os.Setenv(auth.APIKeyEnvVar, apiKeyFlag)
_ = os.Setenv(auth.APIKeyEnvVar, apiKeyFlag)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanliness

}
if userFlag != "" {
_, err := noLoginCmdStore.WithUserID(userFlag)
Expand Down
5 changes: 0 additions & 5 deletions pkg/store/user.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanliness

Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,8 @@ func (s AuthHTTPStore) UpdateUser(userID string, updatedUser *entity.UpdateUser)
return &result, nil
}

// userIDParamName = "userID"
// userIDParamStr = fmt.Sprintf("{%s}", userIDParamName)

var usersIDPathPattern = fmt.Sprintf("%s/%s", usersPath, "%s")

// usersIDPath = fmt.Sprintf(usersIDPathPattern, fmt.Sprintf("{%s}", userIDParamStr))

func (s AuthHTTPStore) GetUserByID(userID string) (*entity.User, error) {
var result entity.User
res, err := s.authHTTPClient.restyClient.R().
Expand Down
Loading