Files
Gogs/internal/route/user/auth.go

574 lines
14 KiB
Go
Raw Normal View History

2014-07-26 00:24:27 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package user
import (
2016-03-11 11:56:52 -05:00
"fmt"
2014-07-26 00:24:27 -04:00
"net/url"
2015-10-15 21:28:12 -04:00
"github.com/go-macaron/captcha"
log "unknwon.dev/clog/v2"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/form"
"gogs.io/gogs/internal/mailer"
"gogs.io/gogs/internal/tool"
2014-07-26 00:24:27 -04:00
)
const (
2017-04-06 00:14:30 -04:00
LOGIN = "user/auth/login"
TWO_FACTOR = "user/auth/two_factor"
TWO_FACTOR_RECOVERY_CODE = "user/auth/two_factor_recovery_code"
SIGNUP = "user/auth/signup"
ACTIVATE = "user/auth/activate"
FORGOT_PASSWORD = "user/auth/forgot_passwd"
RESET_PASSWORD = "user/auth/reset_passwd"
2014-07-26 00:24:27 -04:00
)
2017-04-06 00:14:30 -04:00
// AutoLogin reads cookie and try to auto-login.
func AutoLogin(c *context.Context) (bool, error) {
if !db.HasEngine {
2016-03-11 11:56:52 -05:00
return false, nil
}
2020-02-22 20:46:16 +08:00
uname := c.GetCookie(conf.Security.CookieUsername)
2016-03-11 11:56:52 -05:00
if len(uname) == 0 {
return false, nil
}
isSucceed := false
defer func() {
if !isSucceed {
log.Trace("auto-login cookie cleared: %s", uname)
2020-02-22 20:46:16 +08:00
c.SetCookie(conf.Security.CookieUsername, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.CookieRememberName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.LoginStatusCookieName, "", -1, conf.Server.Subpath)
2016-03-11 11:56:52 -05:00
}
}()
u, err := db.GetUserByName(uname)
2016-03-11 11:56:52 -05:00
if err != nil {
if !errors.IsUserNotExist(err) {
2016-03-11 11:56:52 -05:00
return false, fmt.Errorf("GetUserByName: %v", err)
}
return false, nil
}
2020-02-22 20:46:16 +08:00
if val, ok := c.GetSuperSecureCookie(u.Rands+u.Passwd, conf.Security.CookieRememberName); !ok || val != u.Name {
2016-03-11 11:56:52 -05:00
return false, nil
}
isSucceed = true
2017-04-06 00:14:30 -04:00
c.Session.Set("uid", u.ID)
c.Session.Set("uname", u.Name)
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
2020-02-22 20:46:16 +08:00
if conf.Security.EnableLoginStatusCookie {
c.SetCookie(conf.Security.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
}
2016-03-11 11:56:52 -05:00
return true, nil
}
2017-04-06 00:14:30 -04:00
func Login(c *context.Context) {
c.Title("sign_in")
2014-07-26 00:24:27 -04:00
// Check auto-login
2017-04-06 00:14:30 -04:00
isSucceed, err := AutoLogin(c)
2014-07-26 00:24:27 -04:00
if err != nil {
c.ServerError("AutoLogin", err)
2014-07-26 00:24:27 -04:00
return
}
2017-04-06 00:14:30 -04:00
redirectTo := c.Query("redirect_to")
2016-08-27 15:07:02 -07:00
if len(redirectTo) > 0 {
c.SetCookie("redirect_to", redirectTo, 0, conf.Server.Subpath)
2016-08-27 15:07:02 -07:00
} else {
2017-04-06 00:14:30 -04:00
redirectTo, _ = url.QueryUnescape(c.GetCookie("redirect_to"))
2016-08-27 15:07:02 -07:00
}
if isSucceed {
if tool.IsSameSiteURLPath(redirectTo) {
2017-04-06 00:14:30 -04:00
c.Redirect(redirectTo)
2015-11-18 23:52:09 -05:00
} else {
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/")
}
c.SetCookie("redirect_to", "", -1, conf.Server.Subpath)
2014-07-26 00:24:27 -04:00
return
}
// Display normal login page
loginSources, err := db.ActivatedLoginSources()
if err != nil {
c.ServerError("ActivatedLoginSources", err)
return
}
c.Data["LoginSources"] = loginSources
for i := range loginSources {
if loginSources[i].IsDefault {
c.Data["DefaultLoginSource"] = loginSources[i]
c.Data["login_source"] = loginSources[i].ID
break
}
}
c.Success(LOGIN)
2017-04-06 00:14:30 -04:00
}
func afterLogin(c *context.Context, u *db.User, remember bool) {
2017-04-06 00:14:30 -04:00
if remember {
2020-02-22 20:46:16 +08:00
days := 86400 * conf.Security.LoginRememberDays
c.SetCookie(conf.Security.CookieUsername, u.Name, days, conf.Server.Subpath, "", conf.Security.CookieSecure, true)
c.SetSuperSecureCookie(u.Rands+u.Passwd, conf.Security.CookieRememberName, u.Name, days, conf.Server.Subpath, "", conf.Security.CookieSecure, true)
2017-04-06 00:14:30 -04:00
}
c.Session.Set("uid", u.ID)
c.Session.Set("uname", u.Name)
c.Session.Delete("twoFactorRemember")
c.Session.Delete("twoFactorUserID")
// Clear whatever CSRF has right now, force to generate a new one
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
2020-02-22 20:46:16 +08:00
if conf.Security.EnableLoginStatusCookie {
c.SetCookie(conf.Security.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
2017-04-06 00:14:30 -04:00
}
redirectTo, _ := url.QueryUnescape(c.GetCookie("redirect_to"))
c.SetCookie("redirect_to", "", -1, conf.Server.Subpath)
if tool.IsSameSiteURLPath(redirectTo) {
2017-04-06 00:14:30 -04:00
c.Redirect(redirectTo)
return
}
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/")
2014-07-26 00:24:27 -04:00
}
2017-04-06 00:14:30 -04:00
func LoginPost(c *context.Context, f form.SignIn) {
c.Title("sign_in")
loginSources, err := db.ActivatedLoginSources()
if err != nil {
c.ServerError("ActivatedLoginSources", err)
return
}
c.Data["LoginSources"] = loginSources
2014-07-26 00:24:27 -04:00
2017-04-06 00:14:30 -04:00
if c.HasError() {
c.Success(LOGIN)
2014-07-26 00:24:27 -04:00
return
}
u, err := db.UserLogin(f.UserName, f.Password, f.LoginSource)
2014-07-26 00:24:27 -04:00
if err != nil {
switch err.(type) {
case errors.UserNotExist:
2018-05-21 14:45:47 +08:00
c.FormErr("UserName", "Password")
2017-04-06 00:14:30 -04:00
c.RenderWithErr(c.Tr("form.username_password_incorrect"), LOGIN, &f)
case errors.LoginSourceMismatch:
c.FormErr("LoginSource")
c.RenderWithErr(c.Tr("form.auth_source_mismatch"), LOGIN, &f)
default:
c.ServerError("UserLogin", err)
2014-07-26 00:24:27 -04:00
}
for i := range loginSources {
if loginSources[i].IsDefault {
c.Data["DefaultLoginSource"] = loginSources[i]
break
}
}
2014-07-26 00:24:27 -04:00
return
}
2017-04-06 00:14:30 -04:00
if !u.IsEnabledTwoFactor() {
afterLogin(c, u, f.Remember)
return
2014-07-26 00:24:27 -04:00
}
2017-04-06 00:14:30 -04:00
c.Session.Set("twoFactorRemember", f.Remember)
c.Session.Set("twoFactorUserID", u.ID)
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/user/login/two_factor")
2017-04-06 00:14:30 -04:00
}
2017-04-06 00:14:30 -04:00
func LoginTwoFactor(c *context.Context) {
_, ok := c.Session.Get("twoFactorUserID").(int64)
if !ok {
c.NotFound()
return
}
2017-04-06 00:14:30 -04:00
c.Success(TWO_FACTOR)
}
func LoginTwoFactorPost(c *context.Context) {
userID, ok := c.Session.Get("twoFactorUserID").(int64)
if !ok {
c.NotFound()
2014-07-26 00:24:27 -04:00
return
}
t, err := db.GetTwoFactorByUserID(userID)
2017-04-06 00:14:30 -04:00
if err != nil {
c.ServerError("GetTwoFactorByUserID", err)
return
}
passcode := c.Query("passcode")
valid, err := t.ValidateTOTP(passcode)
2017-04-06 00:14:30 -04:00
if err != nil {
c.ServerError("ValidateTOTP", err)
return
} else if !valid {
c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode"))
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/user/login/two_factor")
2017-04-06 00:14:30 -04:00
return
}
u, err := db.GetUserByID(userID)
2017-04-06 00:14:30 -04:00
if err != nil {
c.ServerError("GetUserByID", err)
return
}
// Prevent same passcode from being reused
if c.Cache.IsExist(u.TwoFactorCacheKey(passcode)) {
c.Flash.Error(c.Tr("settings.two_factor_reused_passcode"))
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/user/login/two_factor")
return
}
if err = c.Cache.Put(u.TwoFactorCacheKey(passcode), 1, 60); err != nil {
log.Error("Failed to put cache 'two factor passcode': %v", err)
}
2017-04-06 00:14:30 -04:00
afterLogin(c, u, c.Session.Get("twoFactorRemember").(bool))
}
func LoginTwoFactorRecoveryCode(c *context.Context) {
_, ok := c.Session.Get("twoFactorUserID").(int64)
if !ok {
c.NotFound()
return
}
c.Success(TWO_FACTOR_RECOVERY_CODE)
}
func LoginTwoFactorRecoveryCodePost(c *context.Context) {
userID, ok := c.Session.Get("twoFactorUserID").(int64)
if !ok {
c.NotFound()
return
}
if err := db.UseRecoveryCode(userID, c.Query("recovery_code")); err != nil {
2017-04-06 00:14:30 -04:00
if errors.IsTwoFactorRecoveryCodeNotFound(err) {
c.Flash.Error(c.Tr("auth.login_two_factor_invalid_recovery_code"))
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/user/login/two_factor_recovery_code")
2017-04-06 00:14:30 -04:00
} else {
c.ServerError("UseRecoveryCode", err)
}
return
}
u, err := db.GetUserByID(userID)
2017-04-06 00:14:30 -04:00
if err != nil {
c.ServerError("GetUserByID", err)
return
}
afterLogin(c, u, c.Session.Get("twoFactorRemember").(bool))
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func SignOut(c *context.Context) {
c.Session.Flush()
c.Session.Destory(c.Context)
2020-02-22 20:46:16 +08:00
c.SetCookie(conf.Security.CookieUsername, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.CookieRememberName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/")
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func SignUp(c *context.Context) {
2018-05-21 14:45:47 +08:00
c.Title("sign_up")
2014-07-26 00:24:27 -04:00
c.Data["EnableCaptcha"] = conf.Service.EnableCaptcha
if conf.Service.DisableRegistration {
2017-06-03 07:26:09 -04:00
c.Data["DisableRegistration"] = true
2018-05-21 14:45:47 +08:00
c.Success(SIGNUP)
2014-07-26 00:24:27 -04:00
return
}
2018-05-21 14:45:47 +08:00
c.Success(SIGNUP)
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) {
2018-05-21 14:45:47 +08:00
c.Title("sign_up")
2014-07-26 00:24:27 -04:00
c.Data["EnableCaptcha"] = conf.Service.EnableCaptcha
if conf.Service.DisableRegistration {
2018-05-21 14:45:47 +08:00
c.Status(403)
2014-07-26 00:24:27 -04:00
return
}
2017-06-03 07:26:09 -04:00
if c.HasError() {
2018-05-21 14:45:47 +08:00
c.Success(SIGNUP)
2014-07-26 00:24:27 -04:00
return
}
if conf.Service.EnableCaptcha && !cpt.VerifyReq(c.Req) {
2018-05-21 14:45:47 +08:00
c.FormErr("Captcha")
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("form.captcha_incorrect"), SIGNUP, &f)
2014-07-26 00:24:27 -04:00
return
}
if f.Password != f.Retype {
2018-05-21 14:45:47 +08:00
c.FormErr("Password")
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("form.password_not_match"), SIGNUP, &f)
2014-07-26 00:24:27 -04:00
return
}
u := &db.User{
Name: f.UserName,
Email: f.Email,
Passwd: f.Password,
IsActive: !conf.Service.RegisterEmailConfirm,
2014-07-26 00:24:27 -04:00
}
if err := db.CreateUser(u); err != nil {
switch {
case db.IsErrUserAlreadyExist(err):
2018-05-21 14:45:47 +08:00
c.FormErr("UserName")
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("form.username_been_taken"), SIGNUP, &f)
case db.IsErrEmailAlreadyUsed(err):
2018-05-21 14:45:47 +08:00
c.FormErr("Email")
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("form.email_been_used"), SIGNUP, &f)
case db.IsErrNameReserved(err):
2018-05-21 14:45:47 +08:00
c.FormErr("UserName")
c.RenderWithErr(c.Tr("user.form.name_reserved", err.(db.ErrNameReserved).Name), SIGNUP, &f)
case db.IsErrNamePatternNotAllowed(err):
2018-05-21 14:45:47 +08:00
c.FormErr("UserName")
c.RenderWithErr(c.Tr("user.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), SIGNUP, &f)
2014-07-26 00:24:27 -04:00
default:
2018-05-21 14:45:47 +08:00
c.ServerError("CreateUser", err)
2014-07-26 00:24:27 -04:00
}
return
}
log.Trace("Account created: %s", u.Name)
// Auto-set admin for the only user.
if db.CountUsers() == 1 {
u.IsAdmin = true
u.IsActive = true
if err := db.UpdateUser(u); err != nil {
2018-05-21 14:45:47 +08:00
c.ServerError("UpdateUser", err)
return
}
}
// Send confirmation email, no need for social account.
if conf.Service.RegisterEmailConfirm && u.ID > 1 {
mailer.SendActivateAccountMail(c.Context, db.NewMailerUser(u))
2017-06-03 07:26:09 -04:00
c.Data["IsSendRegisterMail"] = true
c.Data["Email"] = u.Email
c.Data["Hours"] = conf.Service.ActiveCodeLives / 60
2018-05-21 14:45:47 +08:00
c.Success(ACTIVATE)
2014-08-09 17:25:02 -07:00
2018-05-21 14:45:47 +08:00
if err := c.Cache.Put(u.MailResendCacheKey(), 1, 180); err != nil {
log.Error("Failed to put cache key 'mail resend': %v", err)
2014-08-09 17:25:02 -07:00
}
return
}
2014-07-26 00:24:27 -04:00
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/user/login")
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func Activate(c *context.Context) {
code := c.Query("code")
2014-08-09 21:02:00 -07:00
if len(code) == 0 {
2017-06-03 07:26:09 -04:00
c.Data["IsActivatePage"] = true
if c.User.IsActive {
2018-05-21 14:45:47 +08:00
c.NotFound()
2014-08-09 21:02:00 -07:00
return
}
// Resend confirmation email.
if conf.Service.RegisterEmailConfirm {
2018-05-21 14:45:47 +08:00
if c.Cache.IsExist(c.User.MailResendCacheKey()) {
2017-06-03 07:26:09 -04:00
c.Data["ResendLimited"] = true
2014-08-09 21:02:00 -07:00
} else {
c.Data["Hours"] = conf.Service.ActiveCodeLives / 60
mailer.SendActivateAccountMail(c.Context, db.NewMailerUser(c.User))
2014-08-09 21:02:00 -07:00
2018-05-21 14:45:47 +08:00
if err := c.Cache.Put(c.User.MailResendCacheKey(), 1, 180); err != nil {
log.Error("Failed to put cache key 'mail resend': %v", err)
2014-08-09 21:02:00 -07:00
}
}
} else {
2017-06-03 07:26:09 -04:00
c.Data["ServiceNotEnabled"] = true
2014-08-09 21:02:00 -07:00
}
2018-05-21 14:45:47 +08:00
c.Success(ACTIVATE)
2014-08-09 21:02:00 -07:00
return
}
// Verify code.
if user := db.VerifyUserActiveCode(code); user != nil {
2014-08-09 21:02:00 -07:00
user.IsActive = true
var err error
if user.Rands, err = db.GetUserSalt(); err != nil {
2018-05-21 14:45:47 +08:00
c.ServerError("GetUserSalt", err)
return
}
if err := db.UpdateUser(user); err != nil {
2018-05-21 14:45:47 +08:00
c.ServerError("UpdateUser", err)
2014-08-09 21:02:00 -07:00
return
}
log.Trace("User activated: %s", user.Name)
2017-06-03 07:26:09 -04:00
c.Session.Set("uid", user.ID)
c.Session.Set("uname", user.Name)
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/")
2014-08-09 21:02:00 -07:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["IsActivateFailed"] = true
2018-05-21 14:45:47 +08:00
c.Success(ACTIVATE)
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func ActivateEmail(c *context.Context) {
code := c.Query("code")
email_string := c.Query("email")
// Verify code.
if email := db.VerifyActiveEmailCode(code, email_string); email != nil {
if err := email.Activate(); err != nil {
2018-05-21 14:45:47 +08:00
c.ServerError("ActivateEmail", err)
}
log.Trace("Email activated: %s", email.Email)
2017-06-03 07:26:09 -04:00
c.Flash.Success(c.Tr("settings.add_email_success"))
}
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/user/settings/email")
return
}
2017-06-03 07:26:09 -04:00
func ForgotPasswd(c *context.Context) {
2018-05-21 14:45:47 +08:00
c.Title("auth.forgot_password")
2014-07-26 00:24:27 -04:00
if conf.MailService == nil {
2017-06-03 07:26:09 -04:00
c.Data["IsResetDisable"] = true
2018-05-21 14:45:47 +08:00
c.Success(FORGOT_PASSWORD)
2014-07-26 00:24:27 -04:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["IsResetRequest"] = true
2018-05-21 14:45:47 +08:00
c.Success(FORGOT_PASSWORD)
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func ForgotPasswdPost(c *context.Context) {
2018-05-21 14:45:47 +08:00
c.Title("auth.forgot_password")
2014-08-09 21:02:00 -07:00
if conf.MailService == nil {
2018-05-21 14:45:47 +08:00
c.Status(403)
2014-08-09 21:02:00 -07:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["IsResetRequest"] = true
2014-08-09 21:02:00 -07:00
2017-06-03 07:26:09 -04:00
email := c.Query("email")
c.Data["Email"] = email
2015-09-17 14:57:24 -04:00
u, err := db.GetUserByEmail(email)
2014-08-09 21:02:00 -07:00
if err != nil {
if errors.IsUserNotExist(err) {
c.Data["Hours"] = conf.Service.ActiveCodeLives / 60
2017-06-03 07:26:09 -04:00
c.Data["IsResetSent"] = true
2018-05-21 14:45:47 +08:00
c.Success(FORGOT_PASSWORD)
return
2014-08-09 21:02:00 -07:00
} else {
2018-05-21 14:45:47 +08:00
c.ServerError("GetUserByEmail", err)
2014-08-09 21:02:00 -07:00
}
return
}
if !u.IsLocal() {
2018-05-21 14:45:47 +08:00
c.FormErr("Email")
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("auth.non_local_account"), FORGOT_PASSWORD, nil)
return
}
2018-05-21 14:45:47 +08:00
if c.Cache.IsExist(u.MailResendCacheKey()) {
2017-06-03 07:26:09 -04:00
c.Data["ResendLimited"] = true
2018-05-21 14:45:47 +08:00
c.Success(FORGOT_PASSWORD)
2014-08-09 21:02:00 -07:00
return
}
mailer.SendResetPasswordMail(c.Context, db.NewMailerUser(u))
2018-05-21 14:45:47 +08:00
if err = c.Cache.Put(u.MailResendCacheKey(), 1, 180); err != nil {
log.Error("Failed to put cache key 'mail resend': %v", err)
2014-08-09 21:02:00 -07:00
}
c.Data["Hours"] = conf.Service.ActiveCodeLives / 60
2017-06-03 07:26:09 -04:00
c.Data["IsResetSent"] = true
2018-05-21 14:45:47 +08:00
c.Success(FORGOT_PASSWORD)
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func ResetPasswd(c *context.Context) {
2018-05-21 14:45:47 +08:00
c.Title("auth.reset_password")
2014-07-26 00:24:27 -04:00
2017-06-03 07:26:09 -04:00
code := c.Query("code")
2014-07-26 00:24:27 -04:00
if len(code) == 0 {
2018-05-21 14:45:47 +08:00
c.NotFound()
2014-07-26 00:24:27 -04:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["Code"] = code
c.Data["IsResetForm"] = true
2018-05-21 14:45:47 +08:00
c.Success(RESET_PASSWORD)
2014-07-26 00:24:27 -04:00
}
2017-06-03 07:26:09 -04:00
func ResetPasswdPost(c *context.Context) {
2018-05-21 14:45:47 +08:00
c.Title("auth.reset_password")
2014-08-09 21:02:00 -07:00
2017-06-03 07:26:09 -04:00
code := c.Query("code")
2014-08-09 21:02:00 -07:00
if len(code) == 0 {
2018-05-21 14:45:47 +08:00
c.NotFound()
2014-08-09 21:02:00 -07:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["Code"] = code
2014-08-09 21:02:00 -07:00
if u := db.VerifyUserActiveCode(code); u != nil {
2014-08-09 21:02:00 -07:00
// Validate password length.
2017-06-03 07:26:09 -04:00
passwd := c.Query("password")
2014-08-09 21:02:00 -07:00
if len(passwd) < 6 {
2017-06-03 07:26:09 -04:00
c.Data["IsResetForm"] = true
c.Data["Err_Password"] = true
c.RenderWithErr(c.Tr("auth.password_too_short"), RESET_PASSWORD, nil)
2014-08-09 21:02:00 -07:00
return
}
u.Passwd = passwd
var err error
if u.Rands, err = db.GetUserSalt(); err != nil {
2018-05-21 14:45:47 +08:00
c.ServerError("GetUserSalt", err)
return
}
if u.Salt, err = db.GetUserSalt(); err != nil {
2018-05-21 14:45:47 +08:00
c.ServerError("GetUserSalt", err)
return
}
2014-08-09 21:02:00 -07:00
u.EncodePasswd()
if err := db.UpdateUser(u); err != nil {
2018-05-21 14:45:47 +08:00
c.ServerError("UpdateUser", err)
2014-08-09 21:02:00 -07:00
return
}
log.Trace("User password reset: %s", u.Name)
2018-05-21 14:45:47 +08:00
c.SubURLRedirect("/user/login")
2014-08-09 21:02:00 -07:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["IsResetFailed"] = true
2018-05-21 14:45:47 +08:00
c.Success(RESET_PASSWORD)
2014-07-26 00:24:27 -04:00
}