MCPcopy Create free account
hub / github.com/cli/cli / Login

Method Login

internal/config/config.go:369–403  ·  view source on GitHub ↗

Login will set user, git protocol, and auth token for the given hostname. If the encrypt option is specified it will first try to store the auth token in encrypted storage and will fall back to the plain text config file.

(hostname, username, token, gitProtocol string, secureStorage bool)

Source from the content-addressed store, hash-verified

367// If the encrypt option is specified it will first try to store the auth token
368// in encrypted storage and will fall back to the plain text config file.
369func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secureStorage bool) (bool, error) {
370 // In this section we set up the users config
371 var setErr error
372 if secureStorage {
373 // Try to set the token for this user in the encrypted storage for later switching
374 setErr = keyring.Set(keyringServiceName(hostname), username, token)
375 if setErr == nil {
376 // Clean up the previous oauth_token from the config file, if there were one
377 _ = c.cfg.Remove([]string{hostsKey, hostname, usersKey, username, oauthTokenKey})
378 }
379 }
380 insecureStorageUsed := false
381 if !secureStorage || setErr != nil {
382 // And set the oauth token under the user for later switching
383 c.cfg.Set([]string{hostsKey, hostname, usersKey, username, oauthTokenKey}, token)
384 insecureStorageUsed = true
385 }
386
387 if gitProtocol != "" {
388 // Set the host level git protocol
389 // Although it might be expected that this is handled by switch, git protocol
390 // is currently a host level config and not a user level config, so any change
391 // will overwrite the protocol for all users on the host.
392 c.cfg.Set([]string{hostsKey, hostname, gitProtocolKey}, gitProtocol)
393 }
394
395 // Create the username key with an empty value so it will be
396 // written even when there are no keys set under it.
397 if _, getErr := c.cfg.Get([]string{hostsKey, hostname, usersKey, username}); getErr != nil {
398 c.cfg.Set([]string{hostsKey, hostname, usersKey, username}, "")
399 }
400
401 // Then we activate the new user
402 return insecureStorageUsed, c.activateUser(hostname, username)
403}
404
405func (c *AuthConfig) SwitchUser(hostname, user string) error {
406 previouslyActiveUser, err := c.ActiveUser(hostname)

Callers

nothing calls this directly

Calls 6

activateUserMethod · 0.95
SetFunction · 0.92
keyringServiceNameFunction · 0.70
RemoveMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected