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

Method Login

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

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