| 61 | } |
| 62 | |
| 63 | func tokenRun(opts *TokenOptions) error { |
| 64 | cfg, err := opts.Config() |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | authCfg := cfg.Authentication() |
| 69 | cs := opts.IO.ColorScheme() |
| 70 | |
| 71 | hostname := opts.Hostname |
| 72 | if hostname == "" { |
| 73 | hostname, _ = authCfg.DefaultHost() |
| 74 | } |
| 75 | |
| 76 | var val string |
| 77 | var cred gh.Credential |
| 78 | var refreshStatus gh.RefreshStatus |
| 79 | switch { |
| 80 | // No flags: return the active token, refreshing it when necessary, from wherever it is stored. |
| 81 | case !opts.SecureStorage && !opts.NoRefresh: |
| 82 | cred, refreshStatus = getTokenWithRefresh(authCfg, hostname, opts.Username) |
| 83 | val = cred.Token |
| 84 | |
| 85 | // --no-refresh: return the stored active token from wherever it is stored, without refreshing. |
| 86 | case !opts.SecureStorage && opts.NoRefresh: |
| 87 | cred = getTokenReadOnly(authCfg, hostname, opts.Username) |
| 88 | val = cred.Token |
| 89 | |
| 90 | // --secure-storage: compatibility mode for go-gh, which cannot read gh's storage directly. A refreshable token |
| 91 | // is returned regardless of where it is stored, because gh owns its lifecycle and a go-gh based caller cannot |
| 92 | // obtain it any other way. Otherwise this reproduces the original behaviour of the auth token command under |
| 93 | // --secure-storage: read the token straight from the keyring and consult no environment variable or config |
| 94 | // entry at all. |
| 95 | case opts.SecureStorage && !opts.NoRefresh: |
| 96 | // We resolve a username for the refresh lookup because a refreshable token is always stored under a user, |
| 97 | // and ActiveTokenWithRefresh cannot be used upfront: with no username it resolves the active token, which an |
| 98 | // environment variable such as GH_TOKEN can shadow, defeating --secure-storage. |
| 99 | user := resolveUsername(authCfg, hostname, opts.Username) |
| 100 | var c gh.Credential |
| 101 | if user != "" { |
| 102 | c, refreshStatus = getTokenWithRefresh(authCfg, hostname, user) |
| 103 | } |
| 104 | if c.IsRefreshable() { |
| 105 | cred = c |
| 106 | val = c.Token |
| 107 | } else { |
| 108 | // Non-refreshable fallback: read the keyring keyed on the actual flag value, so an omitted --user reads |
| 109 | // the legacy unkeyed slot exactly as the original --secure-storage behaviour did, not a per-user slot. |
| 110 | val = getNonRefreshableTokenFromKeyring(authCfg, hostname, opts.Username) |
| 111 | } |
| 112 | |
| 113 | // --secure-storage --no-refresh: rare combination with no known caller. Respect --secure-storage strictly and |
| 114 | // return the token only when it actually came from the keyring, otherwise nothing. The nature of the credential |
| 115 | // (refreshable or not) is irrelevant here because refreshing was also declined, so there is no lifecycle to |
| 116 | // honor; whoever passed --secure-storage wants the securely stored token specifically, so that is respected. |
| 117 | case opts.SecureStorage && opts.NoRefresh: |
| 118 | // We read under the resolved user so a refreshable token stored in the keyring is still surfaced, but only |
| 119 | // return it when it genuinely came from the keyring. ActiveToken cannot be used upfront because an |
| 120 | // environment variable such as GH_TOKEN can shadow the active token. |