summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/helpers/credentials_helper.go
blob: 20fb5905202fa169f336c083ebee4d05ea57cafe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package helpers

import (
	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

type CredentialsHelper struct {
	c *HelperCommon
}

func NewCredentialsHelper(
	c *HelperCommon,
) *CredentialsHelper {
	return &CredentialsHelper{
		c: c,
	}
}

// promptUserForCredential wait for a username, password or passphrase input from the credentials popup
// We return a channel rather than returning the string directly so that the calling function knows
// when the prompt has been created (before the user has entered anything) so that it can
// note that we're now waiting on user input and lazygit isn't processing anything.
func (self *CredentialsHelper) PromptUserForCredential(passOrUname oscommands.CredentialType) <-chan string {
	ch := make(chan string)

	self.c.OnUIThread(func() error {
		title, mask := self.getTitleAndMask(passOrUname)

		return self.c.Prompt(types.PromptOpts{
			Title: title,
			Mask:  mask,
			HandleConfirm: func(input string) error {
				ch <- input + "\n"

				return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
			},
			HandleClose: func() error {
				ch <- "\n"

				return nil
			},
		})
	})

	return ch
}

func (self *CredentialsHelper) getTitleAndMask(passOrUname oscommands.CredentialType) (string, bool) {
	switch passOrUname {
	case oscommands.Username:
		return self.c.Tr.CredentialsUsername, false
	case oscommands.Password:
		return self.c.Tr.CredentialsPassword, true
	case oscommands.Passphrase:
		return self.c.Tr.CredentialsPassphrase, true
	case oscommands.PIN:
		return self.c.Tr.CredentialsPIN, true
	}

	// should never land here
	panic("unexpected credential request")
}