| 447 | } |
| 448 | |
| 449 | func SettingsTwoFactorEnablePost(c *context.Context) { |
| 450 | secret, ok := c.Session.Get("twoFactorSecret").(string) |
| 451 | if !ok { |
| 452 | c.NotFound() |
| 453 | return |
| 454 | } |
| 455 | |
| 456 | if !totp.Validate(c.Query("passcode"), secret) { |
| 457 | c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode")) |
| 458 | c.RedirectSubpath("/user/settings/security/two_factor_enable") |
| 459 | return |
| 460 | } |
| 461 | |
| 462 | if err := database.Handle.TwoFactors().Create(c.Req.Context(), c.UserID(), conf.Security.SecretKey, secret); err != nil { |
| 463 | c.Flash.Error(c.Tr("settings.two_factor_enable_error", err)) |
| 464 | c.RedirectSubpath("/user/settings/security/two_factor_enable") |
| 465 | return |
| 466 | } |
| 467 | |
| 468 | _ = c.Session.Delete("twoFactorSecret") |
| 469 | _ = c.Session.Delete("twoFactorURL") |
| 470 | c.Flash.Success(c.Tr("settings.two_factor_enable_success")) |
| 471 | c.RedirectSubpath("/user/settings/security/two_factor_recovery_codes") |
| 472 | } |
| 473 | |
| 474 | func SettingsTwoFactorRecoveryCodes(c *context.Context) { |
| 475 | if !database.Handle.TwoFactors().IsEnabled(c.Req.Context(), c.User.ID) { |