form.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 {
  63. switch {
  64. case resp.TOTP:
  65. code := f.form.GetFormItem(2).(*tview.InputField).GetText()
  66. if code == "" {
  67. f.onError(errors.New("code required"))
  68. return
  69. }
  70. // Attempt to login using the code.
  71. resp, err = client.TOTP(code, resp.Ticket)
  72. if err != nil {
  73. f.onError(err)
  74. return
  75. }
  76. default:
  77. f.onError(errors.New("unsupported mfa type"))
  78. return
  79. }
  80. }
  81. if resp.Token == "" {
  82. f.onError(errors.New("missing token"))
  83. return
  84. }
  85. go keyring.SetToken(resp.Token)
  86. if f.done != nil {
  87. f.done(resp.Token)
  88. }
  89. }
  90. func (f *Form) onError(err error) {
  91. slog.Error("failed to login", "err", err)
  92. message := err.Error()
  93. modal := tview.NewModal().
  94. SetText(message).
  95. AddButtons([]string{"Copy", "Close"}).
  96. SetDoneFunc(func(buttonIndex int, _ string) {
  97. if buttonIndex == 0 {
  98. go clipboard.Write(clipboard.FmtText, []byte(message))
  99. } else {
  100. f.
  101. RemovePage(errorPageName).
  102. SwitchToPage(formPageName)
  103. }
  104. })
  105. f.
  106. AddAndSwitchToPage(errorPageName, ui.Centered(modal, 0, 0), true).
  107. ShowPage(formPageName)
  108. }
  109. func (f *Form) loginWithQR() {
  110. qr := newQRLogin(f.app, f.cfg, func(token string, err error) {
  111. if err != nil {
  112. f.onError(err)
  113. return
  114. }
  115. if token == "" {
  116. f.RemovePage(qrPageName).SwitchToPage(formPageName)
  117. return
  118. }
  119. go keyring.SetToken(token)
  120. f.RemovePage(qrPageName)
  121. if f.done != nil {
  122. f.done(token)
  123. }
  124. })
  125. f.AddAndSwitchToPage(qrPageName, qr, true)
  126. qr.start()
  127. }