Skip to content

Commit

Permalink
feat(mega): add 2FA support (#6473)
Browse files Browse the repository at this point in the history
* feat(mega): add support for two-factor authentication in Mega driver  #6226

* feat(mega): remove debug print statement in Mega driver Init function

* feat(mega): add help message for new field
  • Loading branch information
Kuingsmile authored May 22, 2024
1 parent 037850b commit 9eec872
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 10 additions & 1 deletion drivers/mega/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/alist-org/alist/v3/pkg/http_range"
"github.com/pquerna/otp/totp"
"github.com/rclone/rclone/lib/readers"

"github.com/alist-org/alist/v3/internal/driver"
Expand All @@ -33,8 +34,16 @@ func (d *Mega) GetAddition() driver.Additional {
}

func (d *Mega) Init(ctx context.Context) error {
var twoFACode = d.TwoFACode
d.c = mega.New()
return d.c.Login(d.Email, d.Password)
if d.TwoFASecret != "" {
code, err := totp.GenerateCode(d.TwoFASecret, time.Now())
if err != nil {
return fmt.Errorf("generate totp code failed: %w", err)
}
twoFACode = code
}
return d.c.MultiFactorLogin(d.Email, d.Password, twoFACode)
}

func (d *Mega) Drop(ctx context.Context) error {
Expand Down
6 changes: 4 additions & 2 deletions drivers/mega/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ type Addition struct {
// Usually one of two
//driver.RootPath
//driver.RootID
Email string `json:"email" required:"true"`
Password string `json:"password" required:"true"`
Email string `json:"email" required:"true"`
Password string `json:"password" required:"true"`
TwoFACode string `json:"two_fa_code" required:"false" help:"2FA 6-digit code, filling in the 2FA code alone will not support reloading driver"`
TwoFASecret string `json:"two_fa_secret" required:"false" help:"2FA secret"`
}

var config = driver.Config{
Expand Down

0 comments on commit 9eec872

Please sign in to comment.