auth.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "net/url"
  7. "github.com/go-macaron/captcha"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/mailer"
  13. "github.com/gogits/gogs/modules/middleware"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. const (
  17. SIGNIN base.TplName = "user/auth/signin"
  18. SIGNUP base.TplName = "user/auth/signup"
  19. ACTIVATE base.TplName = "user/auth/activate"
  20. FORGOT_PASSWORD base.TplName = "user/auth/forgot_passwd"
  21. RESET_PASSWORD base.TplName = "user/auth/reset_passwd"
  22. )
  23. func SignIn(ctx *middleware.Context) {
  24. ctx.Data["Title"] = ctx.Tr("sign_in")
  25. // Check auto-login.
  26. isSucceed, err := middleware.AutoSignIn(ctx)
  27. if err != nil {
  28. ctx.Handle(500, "AutoSignIn", err)
  29. return
  30. }
  31. if isSucceed {
  32. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  33. ctx.SetCookie("redirect_to", "", -1, setting.AppSubUrl)
  34. ctx.Redirect(redirectTo)
  35. } else {
  36. ctx.Redirect(setting.AppSubUrl + "/")
  37. }
  38. return
  39. }
  40. ctx.HTML(200, SIGNIN)
  41. }
  42. func SignInPost(ctx *middleware.Context, form auth.SignInForm) {
  43. ctx.Data["Title"] = ctx.Tr("sign_in")
  44. if ctx.HasError() {
  45. ctx.HTML(200, SIGNIN)
  46. return
  47. }
  48. u, err := models.UserSignIn(form.UserName, form.Password)
  49. if err != nil {
  50. if models.IsErrUserNotExist(err) {
  51. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), SIGNIN, &form)
  52. } else {
  53. ctx.Handle(500, "UserSignIn", err)
  54. }
  55. return
  56. }
  57. if form.Remember {
  58. days := 86400 * setting.LogInRememberDays
  59. ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubUrl)
  60. ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
  61. setting.CookieRememberName, u.Name, days, setting.AppSubUrl)
  62. }
  63. ctx.Session.Set("uid", u.Id)
  64. ctx.Session.Set("uname", u.Name)
  65. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  66. ctx.SetCookie("redirect_to", "", -1, setting.AppSubUrl)
  67. ctx.Redirect(redirectTo)
  68. return
  69. }
  70. ctx.Redirect(setting.AppSubUrl + "/")
  71. }
  72. func SignOut(ctx *middleware.Context) {
  73. ctx.Session.Delete("uid")
  74. ctx.Session.Delete("uname")
  75. ctx.Session.Delete("socialId")
  76. ctx.Session.Delete("socialName")
  77. ctx.Session.Delete("socialEmail")
  78. ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubUrl)
  79. ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubUrl)
  80. ctx.Redirect(setting.AppSubUrl + "/")
  81. }
  82. func SignUp(ctx *middleware.Context) {
  83. ctx.Data["Title"] = ctx.Tr("sign_up")
  84. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  85. if setting.Service.DisableRegistration {
  86. ctx.Data["DisableRegistration"] = true
  87. ctx.HTML(200, SIGNUP)
  88. return
  89. }
  90. ctx.HTML(200, SIGNUP)
  91. }
  92. func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  93. ctx.Data["Title"] = ctx.Tr("sign_up")
  94. ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
  95. if setting.Service.DisableRegistration {
  96. ctx.Error(403)
  97. return
  98. }
  99. if ctx.HasError() {
  100. ctx.HTML(200, SIGNUP)
  101. return
  102. }
  103. if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
  104. ctx.Data["Err_Captcha"] = true
  105. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), SIGNUP, &form)
  106. return
  107. }
  108. if form.Password != form.Retype {
  109. ctx.Data["Err_Password"] = true
  110. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), SIGNUP, &form)
  111. return
  112. }
  113. u := &models.User{
  114. Name: form.UserName,
  115. Email: form.Email,
  116. Passwd: form.Password,
  117. IsActive: !setting.Service.RegisterEmailConfirm,
  118. }
  119. if err := models.CreateUser(u); err != nil {
  120. switch {
  121. case models.IsErrUserAlreadyExist(err):
  122. ctx.Data["Err_UserName"] = true
  123. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form)
  124. case models.IsErrEmailAlreadyUsed(err):
  125. ctx.Data["Err_Email"] = true
  126. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form)
  127. case models.IsErrNameReserved(err):
  128. ctx.Data["Err_UserName"] = true
  129. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), SIGNUP, &form)
  130. case models.IsErrNamePatternNotAllowed(err):
  131. ctx.Data["Err_UserName"] = true
  132. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SIGNUP, &form)
  133. default:
  134. ctx.Handle(500, "CreateUser", err)
  135. }
  136. return
  137. }
  138. log.Trace("Account created: %s", u.Name)
  139. // Auto-set admin for the only user.
  140. if models.CountUsers() == 1 {
  141. u.IsAdmin = true
  142. u.IsActive = true
  143. if err := models.UpdateUser(u); err != nil {
  144. ctx.Handle(500, "UpdateUser", err)
  145. return
  146. }
  147. }
  148. // Send confirmation e-mail, no need for social account.
  149. if setting.Service.RegisterEmailConfirm && u.Id > 1 {
  150. mailer.SendActivateAccountMail(ctx.Context, u)
  151. ctx.Data["IsSendRegisterMail"] = true
  152. ctx.Data["Email"] = u.Email
  153. ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
  154. ctx.HTML(200, ACTIVATE)
  155. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  156. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  157. }
  158. return
  159. }
  160. ctx.Redirect(setting.AppSubUrl + "/user/login")
  161. }
  162. func Activate(ctx *middleware.Context) {
  163. code := ctx.Query("code")
  164. if len(code) == 0 {
  165. ctx.Data["IsActivatePage"] = true
  166. if ctx.User.IsActive {
  167. ctx.Error(404)
  168. return
  169. }
  170. // Resend confirmation e-mail.
  171. if setting.Service.RegisterEmailConfirm {
  172. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  173. ctx.Data["ResendLimited"] = true
  174. } else {
  175. ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
  176. mailer.SendActivateAccountMail(ctx.Context, ctx.User)
  177. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  178. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  179. }
  180. }
  181. } else {
  182. ctx.Data["ServiceNotEnabled"] = true
  183. }
  184. ctx.HTML(200, ACTIVATE)
  185. return
  186. }
  187. // Verify code.
  188. if user := models.VerifyUserActiveCode(code); user != nil {
  189. user.IsActive = true
  190. user.Rands = models.GetUserSalt()
  191. if err := models.UpdateUser(user); err != nil {
  192. if models.IsErrUserNotExist(err) {
  193. ctx.Error(404)
  194. } else {
  195. ctx.Handle(500, "UpdateUser", err)
  196. }
  197. return
  198. }
  199. log.Trace("User activated: %s", user.Name)
  200. ctx.Session.Set("uid", user.Id)
  201. ctx.Session.Set("uname", user.Name)
  202. ctx.Redirect(setting.AppSubUrl + "/")
  203. return
  204. }
  205. ctx.Data["IsActivateFailed"] = true
  206. ctx.HTML(200, ACTIVATE)
  207. }
  208. func ActivateEmail(ctx *middleware.Context) {
  209. code := ctx.Query("code")
  210. email_string := ctx.Query("email")
  211. // Verify code.
  212. if email := models.VerifyActiveEmailCode(code, email_string); email != nil {
  213. if err := email.Activate(); err != nil {
  214. ctx.Handle(500, "ActivateEmail", err)
  215. }
  216. log.Trace("Email activated: %s", email.Email)
  217. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  218. }
  219. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  220. return
  221. }
  222. func ForgotPasswd(ctx *middleware.Context) {
  223. ctx.Data["Title"] = ctx.Tr("auth.forgot_password")
  224. if setting.MailService == nil {
  225. ctx.Data["IsResetDisable"] = true
  226. ctx.HTML(200, FORGOT_PASSWORD)
  227. return
  228. }
  229. ctx.Data["IsResetRequest"] = true
  230. ctx.HTML(200, FORGOT_PASSWORD)
  231. }
  232. func ForgotPasswdPost(ctx *middleware.Context) {
  233. ctx.Data["Title"] = ctx.Tr("auth.forgot_password")
  234. if setting.MailService == nil {
  235. ctx.Handle(403, "ForgotPasswdPost", nil)
  236. return
  237. }
  238. ctx.Data["IsResetRequest"] = true
  239. email := ctx.Query("email")
  240. ctx.Data["Email"] = email
  241. u, err := models.GetUserByEmail(email)
  242. if err != nil {
  243. if models.IsErrUserNotExist(err) {
  244. ctx.Data["Err_Email"] = true
  245. ctx.RenderWithErr(ctx.Tr("auth.email_not_associate"), FORGOT_PASSWORD, nil)
  246. } else {
  247. ctx.Handle(500, "user.ResetPasswd(check existence)", err)
  248. }
  249. return
  250. }
  251. if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  252. ctx.Data["ResendLimited"] = true
  253. ctx.HTML(200, FORGOT_PASSWORD)
  254. return
  255. }
  256. mailer.SendResetPasswordMail(ctx.Context, u)
  257. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  258. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  259. }
  260. ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
  261. ctx.Data["IsResetSent"] = true
  262. ctx.HTML(200, FORGOT_PASSWORD)
  263. }
  264. func ResetPasswd(ctx *middleware.Context) {
  265. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  266. code := ctx.Query("code")
  267. if len(code) == 0 {
  268. ctx.Error(404)
  269. return
  270. }
  271. ctx.Data["Code"] = code
  272. ctx.Data["IsResetForm"] = true
  273. ctx.HTML(200, RESET_PASSWORD)
  274. }
  275. func ResetPasswdPost(ctx *middleware.Context) {
  276. ctx.Data["Title"] = ctx.Tr("auth.reset_password")
  277. code := ctx.Query("code")
  278. if len(code) == 0 {
  279. ctx.Error(404)
  280. return
  281. }
  282. ctx.Data["Code"] = code
  283. if u := models.VerifyUserActiveCode(code); u != nil {
  284. // Validate password length.
  285. passwd := ctx.Query("password")
  286. if len(passwd) < 6 {
  287. ctx.Data["IsResetForm"] = true
  288. ctx.Data["Err_Password"] = true
  289. ctx.RenderWithErr(ctx.Tr("auth.password_too_short"), RESET_PASSWORD, nil)
  290. return
  291. }
  292. u.Passwd = passwd
  293. u.Rands = models.GetUserSalt()
  294. u.Salt = models.GetUserSalt()
  295. u.EncodePasswd()
  296. if err := models.UpdateUser(u); err != nil {
  297. ctx.Handle(500, "UpdateUser", err)
  298. return
  299. }
  300. log.Trace("User password reset: %s", u.Name)
  301. ctx.Redirect(setting.AppSubUrl + "/user/login")
  302. return
  303. }
  304. ctx.Data["IsResetFailed"] = true
  305. ctx.HTML(200, RESET_PASSWORD)
  306. }