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

Method Login

internal/config/config.go:413–447  ·  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

411// If the encrypt option is specified it will first try to store the auth token
412// in encrypted storage and will fall back to the plain text config file.
413func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secureStorage bool) (bool, error) {
414 // In this section we set up the users config
415 var setErr error
416 if secureStorage {
417 // Try to set the token for this user in the encrypted storage for later switching
418 setErr = keyring.Set(keyringServiceName(hostname), username, token)
419 if setErr == nil {
420 // Clean up the previous oauth_token from the config file, if there were one
421 _ = c.cfg.Remove([]string{hostsKey, hostname, usersKey, username, oauthTokenKey})
422 }
423 }
424 insecureStorageUsed := false
425 if !secureStorage || setErr != nil {
426 // And set the oauth token under the user for later switching
427 c.cfg.Set([]string{hostsKey, hostname, usersKey, username, oauthTokenKey}, token)
428 insecureStorageUsed = true
429 }
430
431 if gitProtocol != "" {
432 // Set the host level git protocol
433 // Although it might be expected that this is handled by switch, git protocol
434 // is currently a host level config and not a user level config, so any change
435 // will overwrite the protocol for all users on the host.
436 c.cfg.Set([]string{hostsKey, hostname, gitProtocolKey}, gitProtocol)
437 }
438
439 // Create the username key with an empty value so it will be
440 // written even when there are no keys set under it.
441 if _, getErr := c.cfg.Get([]string{hostsKey, hostname, usersKey, username}); getErr != nil {
442 c.cfg.Set([]string{hostsKey, hostname, usersKey, username}, "")
443 }
444
445 // Then we activate the new user
446 return insecureStorageUsed, c.activateUser(hostname, username)
447}
448
449func (c *AuthConfig) SwitchUser(hostname, user string) error {
450 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