|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "runtime" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/urfave/cli/v2" |
| 15 | + "go.mau.fi/util/dbutil" |
| 16 | + "maunium.net/go/mautrix/id" |
| 17 | + |
| 18 | + "github.com/beeper/bridge-manager/api/beeperapi" |
| 19 | + |
| 20 | + _ "go.mau.fi/util/dbutil/litestream" |
| 21 | +) |
| 22 | + |
| 23 | +var loginDesktopCommand = &cli.Command{ |
| 24 | + Name: "login-desktop", |
| 25 | + Usage: "Import Beeper Desktop's Matrix credentials into bbctl config", |
| 26 | + Action: loginDesktop, |
| 27 | + Flags: desktopLoginFlags(), |
| 28 | +} |
| 29 | + |
| 30 | +func desktopLoginFlags() []cli.Flag { |
| 31 | + return []cli.Flag{ |
| 32 | + &cli.StringFlag{ |
| 33 | + Name: "profile", |
| 34 | + EnvVars: []string{"BEEPER_PROFILE"}, |
| 35 | + Usage: "Beeper Desktop profile name, equivalent to BEEPER_PROFILE in Desktop", |
| 36 | + }, |
| 37 | + &cli.StringFlag{ |
| 38 | + Name: "desktop-data-dir", |
| 39 | + EnvVars: []string{"BBCTL_DESKTOP_DATA_DIR"}, |
| 40 | + Usage: "Read Matrix credentials from this Beeper Desktop user data directory", |
| 41 | + }, |
| 42 | + &cli.StringFlag{ |
| 43 | + Name: "desktop-account-db", |
| 44 | + EnvVars: []string{"BBCTL_DESKTOP_ACCOUNT_DB"}, |
| 45 | + Usage: "Read Matrix credentials from this Beeper Desktop account.db", |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +type DesktopAccount struct { |
| 51 | + UserID id.UserID |
| 52 | + DeviceID id.DeviceID |
| 53 | + AccessToken string |
| 54 | + Homeserver string |
| 55 | +} |
| 56 | + |
| 57 | +func getDesktopAccountDBPath(ctx *cli.Context) (string, bool) { |
| 58 | + if dbPath := ctx.String("desktop-account-db"); dbPath != "" { |
| 59 | + return dbPath, true |
| 60 | + } |
| 61 | + if dataDir := ctx.String("desktop-data-dir"); dataDir != "" { |
| 62 | + return filepath.Join(dataDir, "account.db"), true |
| 63 | + } |
| 64 | + return "", false |
| 65 | +} |
| 66 | + |
| 67 | +func resolveDesktopDataDir(profile string) (string, error) { |
| 68 | + appName := "BeeperTexts" |
| 69 | + if profile != "" { |
| 70 | + appName += "-" + profile |
| 71 | + } |
| 72 | + switch runtime.GOOS { |
| 73 | + case "darwin": |
| 74 | + home, err := os.UserHomeDir() |
| 75 | + if err != nil { |
| 76 | + return "", err |
| 77 | + } |
| 78 | + return filepath.Join(home, "Library", "Application Support", appName), nil |
| 79 | + case "windows": |
| 80 | + if appData := os.Getenv("APPDATA"); appData != "" { |
| 81 | + return filepath.Join(appData, appName), nil |
| 82 | + } |
| 83 | + home, err := os.UserHomeDir() |
| 84 | + if err != nil { |
| 85 | + return "", err |
| 86 | + } |
| 87 | + return filepath.Join(home, appName), nil |
| 88 | + default: |
| 89 | + configHome := os.Getenv("XDG_CONFIG_HOME") |
| 90 | + if configHome == "" { |
| 91 | + home, err := os.UserHomeDir() |
| 92 | + if err != nil { |
| 93 | + return "", err |
| 94 | + } |
| 95 | + configHome = filepath.Join(home, ".config") |
| 96 | + } |
| 97 | + return filepath.Join(configHome, appName), nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func getLoginDesktopAccountDBPath(ctx *cli.Context) (string, error) { |
| 102 | + if dbPath, ok := getDesktopAccountDBPath(ctx); ok { |
| 103 | + return dbPath, nil |
| 104 | + } |
| 105 | + dataDir, err := resolveDesktopDataDir(ctx.String("profile")) |
| 106 | + if err != nil { |
| 107 | + return "", fmt.Errorf("failed to resolve desktop data directory: %w", err) |
| 108 | + } |
| 109 | + return filepath.Join(dataDir, "account.db"), nil |
| 110 | +} |
| 111 | + |
| 112 | +func readDesktopAccount(ctx context.Context, dbPath string) (*DesktopAccount, error) { |
| 113 | + dbURI := (&url.URL{ |
| 114 | + Scheme: "file", |
| 115 | + Path: dbPath, |
| 116 | + RawQuery: "mode=ro", |
| 117 | + }).String() |
| 118 | + db, err := dbutil.NewWithDialect(dbURI, "sqlite3-fk-wal") |
| 119 | + if err != nil { |
| 120 | + return nil, fmt.Errorf("failed to open desktop account database: %w", err) |
| 121 | + } |
| 122 | + defer db.Close() |
| 123 | + |
| 124 | + var account DesktopAccount |
| 125 | + err = db.QueryRow(ctx, "SELECT user_id, device_id, access_token, homeserver FROM account LIMIT 1"). |
| 126 | + Scan(&account.UserID, &account.DeviceID, &account.AccessToken, &account.Homeserver) |
| 127 | + if errors.Is(err, sql.ErrNoRows) { |
| 128 | + return nil, fmt.Errorf("desktop account database has no logged-in account") |
| 129 | + } else if err != nil { |
| 130 | + return nil, fmt.Errorf("failed to read desktop account database: %w", err) |
| 131 | + } else if account.UserID == "" || account.AccessToken == "" { |
| 132 | + return nil, fmt.Errorf("desktop account database has incomplete credentials") |
| 133 | + } |
| 134 | + return &account, nil |
| 135 | +} |
| 136 | + |
| 137 | +func desktopAccountHomeserverDomain(account *DesktopAccount) (string, error) { |
| 138 | + if account.Homeserver == "" { |
| 139 | + return "", nil |
| 140 | + } |
| 141 | + parsed, err := url.Parse(account.Homeserver) |
| 142 | + if err != nil { |
| 143 | + return "", fmt.Errorf("desktop account has invalid homeserver URL %q: %w", account.Homeserver, err) |
| 144 | + } |
| 145 | + return strings.TrimPrefix(parsed.Host, "matrix."), nil |
| 146 | +} |
| 147 | + |
| 148 | +func envForHomeserverDomain(domain string) string { |
| 149 | + for env, envDomain := range envs { |
| 150 | + if domain == envDomain { |
| 151 | + return env |
| 152 | + } |
| 153 | + } |
| 154 | + return "" |
| 155 | +} |
| 156 | + |
| 157 | +func saveDesktopLogin(ctx *cli.Context, account *DesktopAccount) (string, string, error) { |
| 158 | + homeserver, err := desktopAccountHomeserverDomain(account) |
| 159 | + if err != nil { |
| 160 | + return "", "", err |
| 161 | + } |
| 162 | + env := ctx.String("env") |
| 163 | + if homeserverEnv := envForHomeserverDomain(homeserver); homeserverEnv != "" { |
| 164 | + env = homeserverEnv |
| 165 | + homeserver = envs[env] |
| 166 | + } else if homeserver == "" { |
| 167 | + homeserver = ctx.String("homeserver") |
| 168 | + } |
| 169 | + |
| 170 | + whoami, err := beeperapi.Whoami(homeserver, account.AccessToken) |
| 171 | + if err != nil { |
| 172 | + return "", "", fmt.Errorf("failed to verify desktop credentials with whoami: %w", err) |
| 173 | + } |
| 174 | + |
| 175 | + cfg := GetConfig(ctx) |
| 176 | + envCfg := cfg.Environments.Get(env) |
| 177 | + envCfg.ClusterID = whoami.UserInfo.BridgeClusterID |
| 178 | + envCfg.Username = whoami.UserInfo.Username |
| 179 | + envCfg.AccessToken = account.AccessToken |
| 180 | + envCfg.BridgeDataDir = filepath.Join(UserDataDir, "bbctl", env) |
| 181 | + err = cfg.Save() |
| 182 | + if err != nil { |
| 183 | + return "", "", fmt.Errorf("failed to save config: %w", err) |
| 184 | + } |
| 185 | + |
| 186 | + return env, homeserver, nil |
| 187 | +} |
| 188 | + |
| 189 | +func loginDesktop(ctx *cli.Context) error { |
| 190 | + dbPath, err := getLoginDesktopAccountDBPath(ctx) |
| 191 | + if err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + |
| 195 | + account, err := readDesktopAccount(ctx.Context, dbPath) |
| 196 | + if err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + |
| 200 | + env, homeserver, err := saveDesktopLogin(ctx, account) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + fmt.Printf("Imported Desktop login for @%s into bbctl env %q (%s)\n", account.UserID, env, homeserver) |
| 206 | + return nil |
| 207 | +} |
0 commit comments