Skip to content

Commit 969a464

Browse files
committed
Document when gh auth login writes oauth token to plain text
Adds documentation to gh auth login about plain text fallback. Prints a warning when it is stored in plain text. Fixes cli#7757
1 parent f777bec commit 969a464

5 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎internal/config/config.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (c *AuthConfig) SetDefaultHost(host, source string) {
220220
// Login will set user, git protocol, and auth token for the given hostname.
221221
// If the encrypt option is specified it will first try to store the auth token
222222
// in encrypted storage and will fall back to the plain text config file.
223-
func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secureStorage bool) error {
223+
func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secureStorage bool) (configWriteErr error, insecureStorageUsed bool) {
224224
var setErr error
225225
if secureStorage {
226226
if setErr = keyring.Set(keyringServiceName(hostname), "", token); setErr == nil {
@@ -230,14 +230,17 @@ func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secure
230230
}
231231
if !secureStorage || setErr != nil {
232232
c.cfg.Set([]string{hosts, hostname, oauthToken}, token)
233+
insecureStorageUsed = true
234+
} else {
235+
insecureStorageUsed = false
233236
}
234237
if username != "" {
235238
c.cfg.Set([]string{hosts, hostname, "user"}, username)
236239
}
237240
if gitProtocol != "" {
238241
c.cfg.Set([]string{hosts, hostname, "git_protocol"}, gitProtocol)
239242
}
240-
return ghConfig.Write(c.cfg)
243+
return ghConfig.Write(c.cfg), insecureStorageUsed
241244
}
242245

243246
// Logout will remove user, git protocol, and auth token for the given hostname.

‎pkg/cmd/auth/login/login.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
5959
6060
The default authentication mode is a web-based browser flow. After completion, an
6161
authentication token will be stored internally.
62+
If a credential store is not found or there is an issue using it we will fallback to writing the token in plain text.
63+
See %[1]sgh auth status%[1]s for its stored location.
6264
6365
Alternatively, use %[1]s--with-token%[1]s to pass in a token on standard input.
6466
The minimum required scopes for the token are: "repo", "read:org".
@@ -127,7 +129,7 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
127129

128130
// secure storage became the default on 2023/4/04; this flag is left as a no-op for backwards compatibility
129131
var secureStorage bool
130-
cmd.Flags().BoolVar(&secureStorage, "secure-storage", false, "Save authentication credentials in secure credential store")
132+
cmd.Flags().BoolVar(&secureStorage, "secure-storage", false, "Save authentication credentials in secure credential store.")
131133
_ = cmd.Flags().MarkHidden("secure-storage")
132134

133135
cmd.Flags().BoolVar(&opts.InsecureStorage, "insecure-storage", false, "Save authentication credentials in plain text instead of credential store")
@@ -172,7 +174,8 @@ func loginRun(opts *LoginOptions) error {
172174
return fmt.Errorf("error validating token: %w", err)
173175
}
174176
// Adding a user key ensures that a nonempty host section gets written to the config file.
175-
return authCfg.Login(hostname, "x-access-token", opts.Token, opts.GitProtocol, !opts.InsecureStorage)
177+
configWriteError, _ := authCfg.Login(hostname, "x-access-token", opts.Token, opts.GitProtocol, !opts.InsecureStorage)
178+
return configWriteError
176179
}
177180

178181
existingToken, _ := authCfg.Token(hostname)

‎pkg/cmd/auth/refresh/refresh.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.
4848
if err != nil {
4949
return err
5050
}
51-
return authCfg.Login(hostname, username, token, "", secureStorage)
51+
configWriteError, _ := authCfg.Login(hostname, username, token, "", secureStorage)
52+
return configWriteError
5253
},
5354
HttpClient: &http.Client{},
5455
GitClient: f.GitClient,

‎pkg/cmd/auth/shared/login_flow.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
const defaultSSHKeyTitle = "GitHub CLI"
2323

2424
type iconfig interface {
25-
Login(string, string, string, string, bool) error
25+
Login(string, string, string, string, bool) (error, bool)
2626
}
2727

2828
type LoginOptions struct {
@@ -188,8 +188,12 @@ func Login(opts *LoginOptions) error {
188188
fmt.Fprintf(opts.IO.ErrOut, "%s Configured git protocol\n", cs.SuccessIcon())
189189
}
190190

191-
if err := cfg.Login(hostname, username, authToken, gitProtocol, opts.SecureStorage); err != nil {
192-
return err
191+
configWriteError, insecureStorageUsed := cfg.Login(hostname, username, authToken, gitProtocol, opts.SecureStorage)
192+
if configWriteError != nil {
193+
return configWriteError
194+
}
195+
if insecureStorageUsed {
196+
fmt.Fprintf(opts.IO.ErrOut, "The authentication credentials were written in plain text.\n")
193197
}
194198

195199
if credentialFlow.ShouldSetup() {

‎pkg/cmd/auth/shared/login_flow_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818

1919
type tinyConfig map[string]string
2020

21-
func (c tinyConfig) Login(host, username, token, gitProtocol string, encrypt bool) error {
21+
func (c tinyConfig) Login(host, username, token, gitProtocol string, encrypt bool) (configWriteErr error, insecureStorageUsed bool) {
2222
c[fmt.Sprintf("%s:%s", host, "user")] = username
2323
c[fmt.Sprintf("%s:%s", host, "oauth_token")] = token
2424
c[fmt.Sprintf("%s:%s", host, "git_protocol")] = gitProtocol
25-
return nil
25+
return nil, false
2626
}
2727

2828
func TestLogin_ssh(t *testing.T) {

0 commit comments

Comments
 (0)