Skip to content

Commit a7bc5cd

Browse files
committed
Fix registry auth: use source org for token, suggest login on 403
RegistrySource now uses its own Org field for token lookup instead of detecting from git remote. This fixes auth when pulling from multiple orgs' registries. Catalog search now detects auth-related failures (403, token errors) and suggests running apx auth login.
1 parent 99be69e commit a7bc5cd

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

cmd/apx/commands/search.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ func searchAction(cmd *cobra.Command, args []string) error {
7575
Tag: tag,
7676
})
7777
if err != nil {
78+
// If the error is auth-related, suggest running apx auth login.
79+
errStr := err.Error()
80+
if strings.Contains(errStr, "auth") || strings.Contains(errStr, "403") ||
81+
strings.Contains(errStr, "token") || strings.Contains(errStr, "user app") {
82+
ui.Error("Catalog search failed due to authentication.")
83+
ui.Info("Run `apx auth login` to authenticate and discover catalogs.")
84+
return err
85+
}
7886
ui.Error("Failed to search catalog: %v", err)
7987
return err
8088
}

internal/catalog/registry.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,34 @@ func (r *RegistrySource) httpClient() *http.Client {
140140
}
141141

142142
// ghToken gets a GitHub token for GHCR authentication.
143+
// Uses the RegistrySource's Org to look up the correct token, rather
144+
// than guessing from the current git remote.
143145
func (r *RegistrySource) ghToken() (string, error) {
144146
if r.GHTokenFn != nil {
145147
return r.GHTokenFn()
146148
}
147-
return ghAuthToken()
149+
150+
// Use the registry's own org for token lookup.
151+
org := r.Org
152+
if org == "" {
153+
// Fallback: detect from git remote.
154+
var err error
155+
org, err = githubauth.DetectOrg()
156+
if err != nil {
157+
return "", fmt.Errorf("cannot detect GitHub org: %w", err)
158+
}
159+
}
160+
161+
token, err := githubauth.EnsureToken(org)
162+
if err != nil {
163+
return "", fmt.Errorf("GitHub auth for org %q failed: %w", org, err)
164+
}
165+
return token, nil
148166
}
149167

150-
// ghAuthToken returns a GitHub token for GHCR auth.
151-
// Uses the githubauth package (device flow + token cache) instead of `gh auth token`.
168+
// ghAuthToken returns a GitHub token for GHCR auth using the current
169+
// directory's org. Used by DiscoverRegistries which doesn't have an
170+
// org-specific RegistrySource yet.
152171
var ghAuthToken = ghAuthTokenReal
153172

154173
func ghAuthTokenReal() (string, error) {

0 commit comments

Comments
 (0)