Skip to content

Commit

Permalink
Default to FileBackend keyring when other options are not available (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
majd authored Jun 3, 2023
1 parent 6a7adab commit e1141bd
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 50 deletions.
5 changes: 0 additions & 5 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/99designs/keyring"
"github.com/avast/retry-go"
"github.com/majd/ipatool/v2/pkg/appstore"
"github.com/majd/ipatool/v2/pkg/util"
Expand All @@ -22,10 +21,6 @@ func authCmd() *cobra.Command {
Short: "Authenticate with the App Store",
}

if keyringBackendType() == keyring.FileBackend {
cmd.PersistentFlags().StringVar(&keychainPassphrase, "keychain-passphrase", "", "passphrase for unlocking keychain")
}

cmd.AddCommand(loginCmd())
cmd.AddCommand(infoCmd())
cmd.AddCommand(revokeCmd())
Expand Down
39 changes: 11 additions & 28 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ import (
"github.com/majd/ipatool/v2/pkg/util/operatingsystem"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"golang.org/x/term"
)

var dependencies = Dependencies{}
var keychainPassphrase string

type Dependencies struct {
Logger log.Logger
OS operatingsystem.OperatingSystem
Machine machine.Machine
CookieJar http.CookieJar
Keychain keychain.Keychain
AppStore appstore.AppStore
KeyringBackendType keyring.BackendType
Logger log.Logger
OS operatingsystem.OperatingSystem
Machine machine.Machine
CookieJar http.CookieJar
Keychain keychain.Keychain
AppStore appstore.AppStore
}

// newLogger returns a new logger instance.
Expand Down Expand Up @@ -62,10 +60,12 @@ func newCookieJar(machine machine.Machine) http.CookieJar {
}

// newKeychain returns a new keychain instance.
func newKeychain(machine machine.Machine, logger log.Logger, backendType keyring.BackendType, interactive bool) keychain.Keychain {
func newKeychain(machine machine.Machine, logger log.Logger, interactive bool) keychain.Keychain {
ring := util.Must(keyring.Open(keyring.Config{
AllowedBackends: []keyring.BackendType{
backendType,
keyring.KeychainBackend,
keyring.SecretServiceBackend,
keyring.FileBackend,
},
ServiceName: KeychainServiceName,
FileDir: filepath.Join(machine.HomeDirectory(), ConfigDirectoryName),
Expand Down Expand Up @@ -96,22 +96,6 @@ func newKeychain(machine machine.Machine, logger log.Logger, backendType keyring
return keychain.New(keychain.Args{Keyring: ring})
}

// keyringBackendType returns the backend type for the keyring.
func keyringBackendType() keyring.BackendType {
allowedBackends := []keyring.BackendType{
keyring.KeychainBackend,
keyring.SecretServiceBackend,
}

for _, backend := range allowedBackends {
if slices.Contains(keyring.AvailableBackends(), backend) {
return backend
}
}

return keyring.FileBackend
}

// initWithCommand initializes the dependencies of the command.
func initWithCommand(cmd *cobra.Command) {
verbose := cmd.Flag("verbose").Value.String() == "true"
Expand All @@ -122,8 +106,7 @@ func initWithCommand(cmd *cobra.Command) {
dependencies.OS = operatingsystem.New()
dependencies.Machine = machine.New(machine.Args{OS: dependencies.OS})
dependencies.CookieJar = newCookieJar(dependencies.Machine)
dependencies.KeyringBackendType = keyringBackendType()
dependencies.Keychain = newKeychain(dependencies.Machine, dependencies.Logger, dependencies.KeyringBackendType, interactive)
dependencies.Keychain = newKeychain(dependencies.Machine, dependencies.Logger, interactive)
dependencies.AppStore = appstore.NewAppStore(appstore.Args{
CookieJar: dependencies.CookieJar,
OperatingSystem: dependencies.OS,
Expand Down
6 changes: 0 additions & 6 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"time"

"github.com/99designs/keyring"
"github.com/avast/retry-go"
"github.com/majd/ipatool/v2/pkg/appstore"
"github.com/schollz/progressbar/v3"
Expand Down Expand Up @@ -112,11 +111,6 @@ func downloadCmd() *cobra.Command {
cmd.Flags().StringVarP(&bundleID, "bundle-identifier", "b", "", "The bundle identifier of the target iOS app (required)")
cmd.Flags().StringVarP(&outputPath, "output", "o", "", "The destination path of the downloaded app package")
cmd.Flags().BoolVar(&acquireLicense, "purchase", false, "Obtain a license for the app if needed")

if keyringBackendType() == keyring.FileBackend {
cmd.Flags().StringVar(&keychainPassphrase, "keychain-passphrase", "", "passphrase for unlocking keychain")
}

_ = cmd.MarkFlagRequired("bundle-identifier")

return cmd
Expand Down
6 changes: 0 additions & 6 deletions cmd/purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"time"

"github.com/99designs/keyring"
"github.com/avast/retry-go"
"github.com/majd/ipatool/v2/pkg/appstore"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,11 +65,6 @@ func purchaseCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&bundleID, "bundle-identifier", "b", "", "Bundle identifier of the target iOS app (required)")

if keyringBackendType() == keyring.FileBackend {
cmd.Flags().StringVar(&keychainPassphrase, "keychain-passphrase", "", "passphrase for unlocking keychain")
}

_ = cmd.MarkFlagRequired("bundle-identifier")

return cmd
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func rootCmd() *cobra.Command {
}, enumflag.EnumCaseSensitive), "format", "", "sets output format for command; can be 'text', 'json'")
cmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "enables verbose logs")
cmd.PersistentFlags().BoolVarP(&nonInteractive, "non-interactive", "", false, "run in non-interactive session")
cmd.Flags().StringVar(&keychainPassphrase, "keychain-passphrase", "", "passphrase for unlocking keychain")

cmd.AddCommand(authCmd())
cmd.AddCommand(downloadCmd())
Expand Down
5 changes: 0 additions & 5 deletions cmd/search.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/99designs/keyring"
"github.com/majd/ipatool/v2/pkg/appstore"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -40,9 +39,5 @@ func searchCmd() *cobra.Command {

cmd.Flags().Int64VarP(&limit, "limit", "l", 5, "maximum amount of search results to retrieve")

if keyringBackendType() == keyring.FileBackend {
cmd.PersistentFlags().StringVar(&keychainPassphrase, "keychain-passphrase", "", "passphrase for unlocking keychain")
}

return cmd
}

0 comments on commit e1141bd

Please sign in to comment.