diff --git a/README.md b/README.md index 02ccfa8..427705b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ drops you into its persistent `work` tmux session over your own tailnet. ```sh curl -fsSL https://raw.githubusercontent.com/JumpstartLab/work/main/install.sh | sh -work login +work login you@yourcompany.com work ``` @@ -21,7 +21,7 @@ network changes. Without `et`, `work` uses `ssh`. | | | |---|---| -| `work login` | Sign in (device code → your browser → your Google account) | +| `work login ` | Sign in (device code → your browser → your Google account). The email picks your organization; leave it off only for a personal address that belongs to none | | `work` / `work ` | Wake the box if needed, attach its `work` tmux session | | `work list` | Your boxes and their state | | `work code [dir]` | VS Code on this machine, Remote-SSH'd into the box | diff --git a/auth.go b/auth.go index 9cfff99..129bcfc 100644 --- a/auth.go +++ b/auth.go @@ -92,8 +92,29 @@ func (tr *tokenResponse) toToken(prevRefresh string) *token { return t } -func login() error { - _, raw, err := postForm("/oauth/v2/device_authorization", url.Values{"client_id": {clientID}, "scope": {scope}}) +// loginScope adds Zitadel's org scope for the email's domain. The device flow +// never passes the edge's "work email" step, so without it Zitadel signs the +// person in through the default org, which refuses customer accounts. An empty +// email (a personal address that belongs to no org) keeps the default org. +func loginScope(email string) (string, error) { + email = strings.ToLower(strings.TrimSpace(email)) + if email == "" { + return scope, nil + } + at := strings.LastIndex(email, "@") + domain := email[at+1:] + if at < 1 || !strings.Contains(domain, ".") || strings.ContainsAny(domain, " /") { + return "", fmt.Errorf("%q is not an email address", email) + } + return scope + " urn:zitadel:iam:org:domain:primary:" + domain, nil +} + +func login(email string) error { + sc, err := loginScope(email) + if err != nil { + return err + } + _, raw, err := postForm("/oauth/v2/device_authorization", url.Values{"client_id": {clientID}, "scope": {sc}}) if err != nil { return err } diff --git a/main.go b/main.go index c56f5c3..aea9d75 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,6 @@ // work — the front door to your dev box. // -// work login sign in (your own Google account, via Jumpstart Lab SSO) +// work login sign in (your own Google account, via Jumpstart Lab SSO) // work open your box (if you have exactly one) // work wake it if it is asleep, then attach its `work` tmux session // work list your boxes and whether each is running @@ -41,7 +41,11 @@ func run(args []string) error { } switch cmd { case "login": - return login() + email := "" + if len(args) > 1 { + email = args[1] + } + return login(email) case "logout": return logout() case "list", "ls": @@ -82,7 +86,7 @@ func run(args []string) error { } const usage = `usage: work [] open your box (wakes it if asleep) - work login sign in with your own account + work login sign in with your own work email work list your boxes and their state work code [dir] VS Code here, Remote-SSH'd into the box work doctor check the whole path @@ -181,7 +185,7 @@ func doctor() error { fmt.Printf(" FAIL %s: %v\n → %s\n", name, err, fix) } _, err := accessToken() - check("signed in", err, "work login") + check("signed in", err, "work login ") if err == nil { who, _ := whoami() fmt.Printf(" as %s\n", who) diff --git a/work_test.go b/work_test.go index e0e4b3d..2baee69 100644 --- a/work_test.go +++ b/work_test.go @@ -127,3 +127,18 @@ func TestNotSignedIn(t *testing.T) { t.Fatalf("err = %v", err) } } + +func TestLoginScopeAddsTheEmailsOrg(t *testing.T) { + sc, err := loginScope(" Me@Example.COM ") + if err != nil || !strings.HasSuffix(sc, " urn:zitadel:iam:org:domain:primary:example.com") { + t.Fatalf("scope %q, err %v", sc, err) + } + if sc, _ := loginScope(""); sc != scope { + t.Fatalf("no email must keep the default scope, got %q", sc) + } + for _, bad := range []string{"nobody", "@example.com", "x@localhost", "x@exa mple.com"} { + if _, err := loginScope(bad); err == nil { + t.Errorf("%q accepted", bad) + } + } +}