github.com/supabase/cli@v1.168.1/internal/utils/credentials/store.go (about) 1 package credentials 2 3 import ( 4 "bytes" 5 "os" 6 "os/exec" 7 8 "github.com/go-errors/errors" 9 "github.com/zalando/go-keyring" 10 ) 11 12 const namespace = "Supabase CLI" 13 14 var ErrNotSupported = errors.New("Keyring is not supported on WSL") 15 16 // Retrieves the stored password of a project and username 17 func Get(project string) (string, error) { 18 if err := assertKeyringSupported(); err != nil { 19 return "", err 20 } 21 val, err := keyring.Get(namespace, project) 22 if errors.Is(err, exec.ErrNotFound) { 23 return "", errors.New(ErrNotSupported) 24 } else if err != nil { 25 return "", errors.Errorf("failed to load credentials: %w", err) 26 } 27 return val, nil 28 } 29 30 // Stores the password of a project and username 31 func Set(project, password string) error { 32 if err := assertKeyringSupported(); err != nil { 33 return err 34 } 35 if err := keyring.Set(namespace, project, password); errors.Is(err, exec.ErrNotFound) { 36 return errors.New(ErrNotSupported) 37 } else if err != nil { 38 return errors.Errorf("failed to set credentials: %w", err) 39 } 40 return nil 41 } 42 43 // Erases the stored password of a project and username 44 func Delete(project string) error { 45 if err := assertKeyringSupported(); err != nil { 46 return err 47 } 48 if err := keyring.Delete(namespace, project); errors.Is(err, exec.ErrNotFound) { 49 return errors.New(ErrNotSupported) 50 } else if err != nil { 51 return errors.Errorf("failed to delete credentials: %w", err) 52 } 53 return nil 54 } 55 56 func assertKeyringSupported() error { 57 // Suggested check: https://github.com/microsoft/WSL/issues/423 58 if f, err := os.ReadFile("/proc/sys/kernel/osrelease"); err == nil { 59 if bytes.Contains(f, []byte("WSL")) || bytes.Contains(f, []byte("Microsoft")) { 60 return errors.New(ErrNotSupported) 61 } 62 } 63 return nil 64 }