86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/99designs/keyring"
|
|
"github.com/frenocorp/pop/internal/auth"
|
|
"github.com/frenocorp/pop/internal/config"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
func loginCmd() *cobra.Command {
|
|
var email, password, totpCode string
|
|
var interactive bool
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "login",
|
|
Short: "Log in to ProtonMail",
|
|
Long: `Authenticate with ProtonMail API and store session credentials.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfgMgr := config.NewConfigManager()
|
|
config, err := cfgMgr.Load()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
|
|
manager, err := auth.NewSessionManager()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create session manager: %w", err)
|
|
}
|
|
|
|
if interactive {
|
|
return manager.LoginInteractive(config.APIBaseURL)
|
|
}
|
|
|
|
if email == "" || password == "" {
|
|
return fmt.Errorf("email and password flags required for non-interactive login")
|
|
}
|
|
|
|
return manager.LoginWithCredentials(config.APIBaseURL, email, password)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&email, "email", "e", "", "ProtonMail email address")
|
|
cmd.Flags().StringVarP(&password, "password", "p", "", "ProtonMail password")
|
|
cmd.Flags().BoolVarP(&interactive, "interactive", "i", true, "Interactive prompt for credentials")
|
|
cmd.Flags().StringVar(&totpCode, "totp", "", "TOTP code for 2FA authentication")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func logoutCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "logout",
|
|
Short: "Log out from ProtonMail",
|
|
Long: `Clear stored session credentials.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
manager := auth.NewSessionManager()
|
|
return manager.Logout()
|
|
},
|
|
}
|
|
|
|
return cmd
|
|
}
|
|
|
|
func sessionCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "session",
|
|
Short: "Show current session info",
|
|
Long: `Display current authentication session details.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
manager := auth.NewSessionManager()
|
|
session, err := manager.GetSession()
|
|
if err != nil {
|
|
return fmt.Errorf("no active session: %w", err)
|
|
}
|
|
fmt.Fprintf(os.Stdout, "Session: %s\n", session.UID)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
return cmd
|
|
}
|