Skip to content

Commit

Permalink
notify user when creds expired, instead of just shitting to trace
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 26, 2024
1 parent b764355 commit 6c37d8a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
19 changes: 18 additions & 1 deletion auth/auth_error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package auth

import "fmt"
import (
"errors"
"fmt"

"github.com/slack-go/slack"
)

// Error is the error returned by New, the underlying Err contains
// an API error returned by slack.AuthTest call.
Expand All @@ -24,3 +29,15 @@ func (ae *Error) Unwrap() error {
func (ae *Error) Is(target error) bool {
return target == ae.Err
}

func IsInvalidAuthErr(err error) bool {
var e *Error
if !errors.As(err, &e) {
return false
}
var ser slack.SlackErrorResponse
if !errors.As(e.Err, &ser) {
return false
}
return ser.Err == "invalid_auth"
}
39 changes: 39 additions & 0 deletions auth/auth_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"testing"

"github.com/slack-go/slack"
)

var errSample = errors.New("test error")
Expand Down Expand Up @@ -77,3 +79,40 @@ func TestAuthError_Is(t *testing.T) {
})
}
}

func TestIsInvalidAuthErr(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
want bool
}{
{
"not an auth error",
args{
errors.New("not me bro"),
},
false,
},
{
"auth error",
args{
&Error{
Err: slack.SlackErrorResponse{
Err: "invalid_auth",
},
},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsInvalidAuthErr(tt.args.err); got != tt.want {
t.Errorf("IsInvalidAuthErr() = %v, want %v", got, tt.want)
}
})
}
}
15 changes: 11 additions & 4 deletions internal/cache/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/rusq/encio"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/logger"
)

const ezLogin = "EZ-Login 3000"
Expand Down Expand Up @@ -66,7 +67,6 @@ func (c SlackCreds) Type(ctx context.Context) (AuthType, error) {
return ATRod, ErrNotTested
}
return ATRod, nil

}

func (c SlackCreds) IsEmpty() bool {
Expand Down Expand Up @@ -143,18 +143,25 @@ func initProvider(ctx context.Context, cacheDir string, filename string, workspa

credsFile := filename
if cacheDir != "" {
if err := os.MkdirAll(cacheDir, 0700); err != nil {
if err := os.MkdirAll(cacheDir, 0o700); err != nil {
return nil, fmt.Errorf("failed to create cache directory: %w", err)
}
credsFile = filepath.Join(cacheDir, filename)
}

// try to load the existing credentials, if saved earlier.
lg := logger.FromContext(ctx)
if creds == nil || creds.IsEmpty() {
if prov, err := tryLoad(ctx, credsFile); err != nil {
trace.Logf(ctx, "warn", "no saved credentials: %s", err)
msg := fmt.Sprintf("failed to load saved credentials: %s", err)
trace.Logf(ctx, "warn", msg)
if auth.IsInvalidAuthErr(err) {
lg.Println("authentication details expired, relogin is necessary")
}
} else {
trace.Log(ctx, "info", "loaded saved credentials")
msg := "loaded saved credentials"
lg.Debug(msg)
trace.Log(ctx, "info", msg)
return prov, nil
}
}
Expand Down

0 comments on commit 6c37d8a

Please sign in to comment.