ci / test (push) Canceled after 0s
ci / cross-build (amd64, darwin) (push) Canceled after 0s
ci / cross-build (amd64, linux) (push) Canceled after 0s
ci / cross-build (amd64, windows) (push) Canceled after 0s
ci / cross-build (arm64, darwin) (push) Canceled after 0s
ci / cross-build (arm64, linux) (push) Canceled after 0s
ci / cross-build (arm64, windows) (push) Canceled after 0s
- apiclient: AuthSignIn (/signin/email-password) + AuthRefresh (/token) - login: interactive email+password prompt, --email/--password for CI, --token paste - resolveClient: auto-refresh expired session tokens using the stored refresh token - credentials store: refresh_token + auth_url
148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/term"
|
|
|
|
"gitlab.com/ghaymah/gy/internal/apiclient"
|
|
"gitlab.com/ghaymah/gy/internal/gyconfig"
|
|
)
|
|
|
|
func newLoginCmd() *cobra.Command {
|
|
var token, apiURL, authURL, email, password string
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "login",
|
|
Short: "Sign in with email + password (or paste a token)",
|
|
Long: "Prompts for email and password and stores a session. Use --token to paste an existing access token, or --email/--password for non-interactive sign-in.",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
auth := firstNonEmpty(authURL, os.Getenv("GY_AUTH_URL"), apiclient.DefaultAuthURL)
|
|
base := firstNonEmpty(apiURL, os.Getenv("GY_API_URL"))
|
|
if email == "" {
|
|
email = os.Getenv("GY_EMAIL")
|
|
}
|
|
if password == "" {
|
|
password = os.Getenv("GY_PASSWORD")
|
|
}
|
|
|
|
var accessToken, refreshToken string
|
|
|
|
switch {
|
|
case token != "":
|
|
accessToken = token
|
|
|
|
case email != "" && password != "":
|
|
at, rt, _, err := apiclient.AuthSignIn(auth, email, password)
|
|
if err != nil {
|
|
return fmt.Errorf("sign-in failed: %w", err)
|
|
}
|
|
accessToken, refreshToken = at, rt
|
|
|
|
default:
|
|
if !isTTY() {
|
|
return fmt.Errorf("interactive sign-in needs a TTY — use --email/--password, --token, or set GY_EMAIL/GY_PASSWORD")
|
|
}
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Print("Email: ")
|
|
email, _ = reader.ReadString('\n')
|
|
email = strings.TrimSpace(email)
|
|
fmt.Print("Password: ")
|
|
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
|
|
fmt.Println()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
at, rt, _, err := apiclient.AuthSignIn(auth, email, strings.TrimSpace(string(pw)))
|
|
if err != nil {
|
|
return fmt.Errorf("sign-in failed: %w", err)
|
|
}
|
|
accessToken, refreshToken = at, rt
|
|
}
|
|
|
|
if accessToken == "" {
|
|
return fmt.Errorf("no credentials provided")
|
|
}
|
|
|
|
creds := gyconfig.Credentials{
|
|
Token: accessToken,
|
|
RefreshToken: refreshToken,
|
|
BaseURL: base,
|
|
AuthURL: auth,
|
|
}
|
|
if err := gyconfig.SaveCredentials(creds); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Verify the token works before declaring success.
|
|
c := resolveClient()
|
|
if err := c.Verify(context.Background()); err != nil {
|
|
_ = gyconfig.ClearCredentials()
|
|
return fmt.Errorf("token rejected by the gateway: %w", err)
|
|
}
|
|
|
|
fmt.Println(uitCheck() + " Logged in")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVar(&token, "token", "", "paste an access token (or use GY_TOKEN)")
|
|
cmd.Flags().StringVar(&apiURL, "api-url", "", "gateway URL to store")
|
|
cmd.Flags().StringVar(&authURL, "auth-url", "", "auth service URL (default https://auth.ghaymah.systems)")
|
|
cmd.Flags().StringVar(&email, "email", "", "email (non-interactive; also GY_EMAIL)")
|
|
cmd.Flags().StringVar(&password, "password", "", "password (non-interactive; also GY_PASSWORD)")
|
|
return cmd
|
|
}
|
|
|
|
func newLogoutCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "logout",
|
|
Short: "Remove stored credentials",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := gyconfig.ClearCredentials(); err != nil {
|
|
if os.IsNotExist(err) {
|
|
fmt.Println("Already logged out")
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
fmt.Println("Logged out")
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func newWhoamiCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "whoami",
|
|
Short: "Show the authenticated identity and gateway",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
c, err := requireClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := c.Verify(context.Background()); err != nil {
|
|
return fmt.Errorf("not authenticated: %w", err)
|
|
}
|
|
fmt.Printf("%s authenticated · gateway %s\n", uitCheck(), c.BaseURL)
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func newVersionCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "version",
|
|
Short: "Print the CLI version",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Println(version)
|
|
return nil
|
|
},
|
|
}
|
|
}
|