Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -21,7 +21,7 @@ network changes. Without `et`, `work` uses `ssh`.

| | |
|---|---|
| `work login` | Sign in (device code → your browser → your Google account) |
| `work login <email>` | 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 <box>` | Wake the box if needed, attach its `work` tmux session |
| `work list` | Your boxes and their state |
| `work code <box> [dir]` | VS Code on this machine, Remote-SSH'd into the box |
Expand Down
25 changes: 23 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -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 <email> sign in (your own Google account, via Jumpstart Lab SSO)
// work open your box (if you have exactly one)
// work <box> wake it if it is asleep, then attach its `work` tmux session
// work list your boxes and whether each is running
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -82,7 +86,7 @@ func run(args []string) error {
}

const usage = `usage: work [<box>] open your box (wakes it if asleep)
work login sign in with your own account
work login <email> sign in with your own work email
work list your boxes and their state
work code <box> [dir] VS Code here, Remote-SSH'd into the box
work doctor check the whole path
Expand Down Expand Up @@ -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 <your email>")
if err == nil {
who, _ := whoami()
fmt.Printf(" as %s\n", who)
Expand Down
15 changes: 15 additions & 0 deletions work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Loading