form.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package login
  2. import (
  3. "errors"
  4. "log/slog"
  5. "github.com/ayn2op/discordo/internal/config"
  6. "github.com/ayn2op/discordo/internal/http"
  7. "github.com/ayn2op/discordo/internal/keyring"
  8. "github.com/ayn2op/discordo/internal/ui"
  9. "github.com/ayn2op/tview"
  10. "github.com/diamondburned/arikawa/v3/api"
  11. "golang.design/x/clipboard"
  12. )
  13. const (
  14. formPageName = "form"
  15. errorPageName = "error"
  16. qrPageName = "qr"
  17. )
  18. type DoneFn = func(token string)
  19. type Form struct {
  20. *tview.Pages
  21. app *tview.Application
  22. cfg *config.Config
  23. form *tview.Form
  24. done DoneFn
  25. }
  26. func NewForm(app *tview.Application, cfg *config.Config, done DoneFn) *Form {
  27. f := &Form{
  28. Pages: tview.NewPages(),
  29. app: app,
  30. cfg: cfg,
  31. form: tview.NewForm(),
  32. done: done,
  33. }
  34. f.form.
  35. AddInputField("Email", "", 0, nil).
  36. AddPasswordField("Password", "", 0, 0, nil).
  37. AddPasswordField("Code (optional)", "", 0, 0, nil).
  38. AddButton("Login", f.login).
  39. AddButton("Login with QR", f.loginWithQR)
  40. f.AddAndSwitchToPage(formPageName, f.form, true)
  41. return f
  42. }
  43. func (f *Form) login() {
  44. email := f.form.GetFormItem(0).(*tview.InputField).GetText()
  45. password := f.form.GetFormItem(1).(*tview.InputField).GetText()
  46. if email == "" || password == "" {
  47. return
  48. }
  49. // Create an API client without an authentication token.
  50. client := api.NewClient("")
  51. props := http.IdentifyProperties()
  52. if browserUserAgent, ok := props["browser_user_agent"]; ok {
  53. if val, ok := browserUserAgent.(string); ok {
  54. api.UserAgent = val
  55. }
  56. }
  57. resp, err := client.Login(email, password)
  58. if err != nil {
  59. f.onError(err)
  60. return
  61. }
  62. if resp.MFA && resp.TOTP {
  63. code := f.form.GetFormItem(2).(*tview.InputField).GetText()
  64. if code == "" {
  65. f.onError(errors.New("code required"))
  66. return
  67. }
  68. // Attempt to login using the code.
  69. resp, err = client.TOTP(code, resp.Ticket)
  70. if err != nil {
  71. f.onError(err)
  72. return
  73. }
  74. } else {
  75. f.onError(errors.New("unsupported mfa type"))
  76. return
  77. }
  78. if resp.Token == "" {
  79. f.onError(errors.New("missing token"))
  80. return
  81. }
  82. go keyring.SetToken(resp.Token)
  83. if f.done != nil {
  84. f.done(resp.Token)
  85. }
  86. }
  87. func (f *Form) onError(err error) {
  88. slog.Error("failed to login", "err", err)
  89. message := err.Error()
  90. modal := tview.NewModal().
  91. SetText(message).
  92. AddButtons([]string{"Copy", "Close"}).
  93. SetDoneFunc(func(buttonIndex int, _ string) {
  94. if buttonIndex == 0 {
  95. go clipboard.Write(clipboard.FmtText, []byte(message))
  96. } else {
  97. f.
  98. RemovePage(errorPageName).
  99. SwitchToPage(formPageName)
  100. }
  101. })
  102. f.
  103. AddAndSwitchToPage(errorPageName, ui.Centered(modal, 0, 0), true).
  104. ShowPage(formPageName)
  105. }
  106. func (f *Form) loginWithQR() {
  107. qr := newQRLogin(f.app, f.cfg, func(token string, err error) {
  108. if err != nil {
  109. f.onError(err)
  110. return
  111. }
  112. if token == "" {
  113. f.RemovePage(qrPageName).SwitchToPage(formPageName)
  114. return
  115. }
  116. go keyring.SetToken(token)
  117. f.RemovePage(qrPageName)
  118. if f.done != nil {
  119. f.done(token)
  120. }
  121. })
  122. f.AddAndSwitchToPage(qrPageName, qr, true)
  123. qr.start()
  124. }