form.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, 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.Token == "" && resp.MFA {
  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. }
  75. if resp.Token == "" {
  76. f.onError(errors.New("missing token"))
  77. return
  78. }
  79. go keyring.SetToken(resp.Token)
  80. if f.done != nil {
  81. f.done(resp.Token)
  82. }
  83. }
  84. func (f *Form) onError(err error) {
  85. slog.Error("failed to login", "err", err)
  86. message := err.Error()
  87. modal := tview.NewModal().
  88. SetText(message).
  89. AddButtons([]string{"Copy", "Close"}).
  90. SetDoneFunc(func(buttonIndex int, _ string) {
  91. if buttonIndex == 0 {
  92. go clipboard.Write(clipboard.FmtText, []byte(message))
  93. } else {
  94. f.
  95. RemovePage(errorPageName).
  96. SwitchToPage(formPageName)
  97. }
  98. })
  99. f.
  100. AddAndSwitchToPage(errorPageName, ui.Centered(modal, 0, 0), true).
  101. ShowPage(formPageName)
  102. }
  103. func (f *Form) loginWithQR() {
  104. qr := newQRLogin(f.app, f.cfg, func(token string, err error) {
  105. if err != nil {
  106. f.onError(err)
  107. return
  108. }
  109. if token == "" {
  110. f.RemovePage(qrPageName).SwitchToPage(formPageName)
  111. return
  112. }
  113. go keyring.SetToken(token)
  114. f.RemovePage(qrPageName)
  115. if f.done != nil {
  116. f.done(token)
  117. }
  118. })
  119. f.AddAndSwitchToPage(qrPageName, qr, true)
  120. qr.start()
  121. }