auth.go 9.4 KB

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