Skip to content

Commit

Permalink
chore: update access token error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Dec 15, 2023
1 parent 5f0e617 commit 6788d18
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions internal/utils/access_token.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package utils

import (
"errors"
"os"
"path/filepath"
"regexp"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils/credentials"
"github.com/zalando/go-keyring"
Expand All @@ -14,7 +14,7 @@ import (
var (
AccessTokenPattern = regexp.MustCompile(`^sbp_[a-f0-9]{40}$`)
ErrInvalidToken = errors.New("Invalid access token format. Must be like `sbp_0102...1920`.")
ErrMissingToken = errors.New("Access token not provided. Supply an access token by running " + Aqua("supabase login") + " or setting the SUPABASE_ACCESS_TOKEN environment variable.")
ErrMissingToken = errors.Errorf("Access token not provided. Supply an access token by running %s or setting the SUPABASE_ACCESS_TOKEN environment variable.", Aqua("supabase login"))
ErrNotLoggedIn = errors.New("You were not logged in, nothing to do.")
)

Expand All @@ -30,9 +30,9 @@ func LoadAccessTokenFS(fsys afero.Fs) (string, error) {
return "", err
}
if !AccessTokenPattern.MatchString(accessToken) {
return "", ErrInvalidToken
return "", errors.New(ErrInvalidToken)
}
return accessToken, err
return accessToken, nil
}

func loadAccessToken(fsys afero.Fs) (string, error) {
Expand All @@ -55,17 +55,17 @@ func fallbackLoadToken(fsys afero.Fs) (string, error) {
}
accessToken, err := afero.ReadFile(fsys, path)
if errors.Is(err, os.ErrNotExist) {
return "", ErrMissingToken
return "", errors.New(ErrMissingToken)
} else if err != nil {
return "", err
return "", errors.Errorf("failed to read access token file: %w", err)
}
return string(accessToken), nil
}

func SaveAccessToken(accessToken string, fsys afero.Fs) error {
// Validate access token
if !AccessTokenPattern.MatchString(accessToken) {
return ErrInvalidToken
return errors.New(ErrInvalidToken)
}
// Save to native credentials store
if err := credentials.Set(AccessTokenKey, accessToken); err == nil {
Expand All @@ -83,7 +83,10 @@ func fallbackSaveToken(accessToken string, fsys afero.Fs) error {
if err := MkdirIfNotExistFS(fsys, filepath.Dir(path)); err != nil {
return err
}
return afero.WriteFile(fsys, path, []byte(accessToken), 0600)
if err := afero.WriteFile(fsys, path, []byte(accessToken), 0600); err != nil {
return errors.Errorf("failed to save access token file: %w", err)
}
return nil
}

func DeleteAccessToken(fsys afero.Fs) error {
Expand All @@ -99,23 +102,28 @@ func DeleteAccessToken(fsys afero.Fs) error {
// Fallback not found, delete from native credentials store
err := credentials.Delete(AccessTokenKey)
if errors.Is(err, credentials.ErrNotSupported) || errors.Is(err, keyring.ErrNotFound) {
return ErrNotLoggedIn
return errors.New(ErrNotLoggedIn)
} else if err != nil {
return errors.Errorf("failed to delete access token from keyring: %w", err)
}
return err
return nil
}

func fallbackDeleteToken(fsys afero.Fs) error {
path, err := getAccessTokenPath()
if err != nil {
return err
}
return fsys.Remove(path)
if err := fsys.Remove(path); err != nil {
return errors.Errorf("failed to remove access token file: %w", err)
}
return nil
}

func getAccessTokenPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
return "", errors.Errorf("failed to get $HOME directory: %w", err)
}
// TODO: fallback to workdir
return filepath.Join(home, ".supabase", AccessTokenKey), nil
Expand Down

0 comments on commit 6788d18

Please sign in to comment.