detached 925cad7

feat(auth): idempotent auth login — verify existing token when no --token provided

commit
925cad780972ed6f7b0ea84d579b80c037994647
author
brice · · 2026-03-23 05:32 UTC
parent
7d67af7
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
1 file · +33 -7
M internal/adapters/in/cli/auth.go +33 -7
@@ -211,21 +211,22 @@ func newAuthLoginCmd() *cobra.Command {
211211 cmd := &cobra.Command{
212212 Use: "login",
213213 Short: "Authenticate with a Gordon server",
214- Long: `Store a pre-generated token for a Gordon server remote.
214+ Long: `Store or verify a token for a Gordon server remote.
215215
216-The token is stored in your remote configuration and verified
217-against /admin/status on a best-effort basis.
216+With --token: stores the token and verifies it.
217+Without --token: verifies the existing stored token still works.
218218
219219 Generate a token on the server with: gordon auth token generate
220220
221221 Examples:
222222 gordon auth login --token <token> Store token for active remote
223- gordon auth login --remote prod --token <tok> Store token for specific remote`,
223+ gordon auth login --remote prod --token <tok> Store token for specific remote
224+ gordon auth login Verify existing token`,
224225 RunE: func(cmd *cobra.Command, args []string) error {
225- if token == "" {
226- return fmt.Errorf("--token is required; generate a token on the server with: gordon auth token generate")
226+ if token != "" {
227+ return runAuthLoginWithToken(cmd.Context(), remoteName, token, cmd.OutOrStdout())
227228 }
228- return runAuthLoginWithToken(cmd.Context(), remoteName, token, cmd.OutOrStdout())
229+ return runAuthLoginVerify(cmd.Context(), remoteName, cmd.OutOrStdout())
229230 },
230231 }
231232
@@ -268,6 +269,31 @@ func runAuthLoginWithToken(ctx context.Context, remoteName, token string, out io
268269 return nil
269270 }
270271
272+func runAuthLoginVerify(ctx context.Context, remoteName string, out io.Writer) error {
273+ resolvedName, entry, err := resolveRemoteEntry(remoteName)
274+ if err != nil {
275+ return err
276+ }
277+
278+ token := remote.ResolveTokenForRemote(resolvedName, entry)
279+ if token == "" {
280+ return fmt.Errorf("no token stored for remote '%s'; use: gordon auth login --token <token>", resolvedName)
281+ }
282+
283+ insecureTLS := remote.ResolveInsecureTLSForRemote(insecureTLSFlag, resolvedName)
284+ client := remote.NewClient(entry.URL, remoteClientOptions(token, insecureTLS)...)
285+ verifyCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
286+ defer cancel()
287+
288+ if _, err := client.GetStatus(verifyCtx); err != nil {
289+ _ = cliWriteLine(out, styles.RenderWarning(fmt.Sprintf("Token verification failed for '%s': %v", resolvedName, err)))
290+ return fmt.Errorf("authentication failed")
291+ }
292+
293+ _ = cliWriteLine(out, styles.RenderSuccess(fmt.Sprintf("Authenticated to '%s'", resolvedName)))
294+ return nil
295+}
296+
271297 // newAuthShowTokenCmd creates the show-token command.
272298 func newAuthShowTokenCmd() *cobra.Command {
273299 var remoteName string
@@ -211,21 +211,22 @@ func newAuthLoginCmd() *cobra.Command {
211 cmd := &cobra.Command{211 cmd := &cobra.Command{
212 Use: "login",212 Use: "login",
213 Short: "Authenticate with a Gordon server",213 Short: "Authenticate with a Gordon server",
214- Long: `Store a pre-generated token for a Gordon server remote.214+ Long: `Store or verify a token for a Gordon server remote.
215 215
216-The token is stored in your remote configuration and verified216+With --token: stores the token and verifies it.
217-against /admin/status on a best-effort basis.217+Without --token: verifies the existing stored token still works.
218 218
219 Generate a token on the server with: gordon auth token generate219 Generate a token on the server with: gordon auth token generate
220 220
221 Examples:221 Examples:
222 gordon auth login --token <token> Store token for active remote222 gordon auth login --token <token> Store token for active remote
223- gordon auth login --remote prod --token <tok> Store token for specific remote`,223+ gordon auth login --remote prod --token <tok> Store token for specific remote
224+ gordon auth login Verify existing token`,
224 RunE: func(cmd *cobra.Command, args []string) error {225 RunE: func(cmd *cobra.Command, args []string) error {
225- if token == "" {226+ if token != "" {
226- return fmt.Errorf("--token is required; generate a token on the server with: gordon auth token generate")227+ return runAuthLoginWithToken(cmd.Context(), remoteName, token, cmd.OutOrStdout())
227 }228 }
228- return runAuthLoginWithToken(cmd.Context(), remoteName, token, cmd.OutOrStdout())229+ return runAuthLoginVerify(cmd.Context(), remoteName, cmd.OutOrStdout())
229 },230 },
230 }231 }
231 232
@@ -268,6 +269,31 @@ func runAuthLoginWithToken(ctx context.Context, remoteName, token string, out io
268 return nil269 return nil
269 }270 }
270 271
272+func runAuthLoginVerify(ctx context.Context, remoteName string, out io.Writer) error {
273+ resolvedName, entry, err := resolveRemoteEntry(remoteName)
274+ if err != nil {
275+ return err
276+ }
277+
278+ token := remote.ResolveTokenForRemote(resolvedName, entry)
279+ if token == "" {
280+ return fmt.Errorf("no token stored for remote '%s'; use: gordon auth login --token <token>", resolvedName)
281+ }
282+
283+ insecureTLS := remote.ResolveInsecureTLSForRemote(insecureTLSFlag, resolvedName)
284+ client := remote.NewClient(entry.URL, remoteClientOptions(token, insecureTLS)...)
285+ verifyCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
286+ defer cancel()
287+
288+ if _, err := client.GetStatus(verifyCtx); err != nil {
289+ _ = cliWriteLine(out, styles.RenderWarning(fmt.Sprintf("Token verification failed for '%s': %v", resolvedName, err)))
290+ return fmt.Errorf("authentication failed")
291+ }
292+
293+ _ = cliWriteLine(out, styles.RenderSuccess(fmt.Sprintf("Authenticated to '%s'", resolvedName)))
294+ return nil
295+}
296+
271 // newAuthShowTokenCmd creates the show-token command.297 // newAuthShowTokenCmd creates the show-token command.
272 func newAuthShowTokenCmd() *cobra.Command {298 func newAuthShowTokenCmd() *cobra.Command {
273 var remoteName string299 var remoteName string

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close