auth.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. "strings"
  8. "github.com/macaron-contrib/captcha"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. // "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. SIGNIN base.TplName = "user/signin"
  19. SIGNUP base.TplName = "user/signup"
  20. DELETE base.TplName = "user/delete"
  21. ACTIVATE base.TplName = "user/activate"
  22. FORGOT_PASSWORD base.TplName = "user/forgot_passwd"
  23. RESET_PASSWORD base.TplName = "user/reset_passwd"
  24. )
  25. func SignIn(ctx *middleware.Context) {
  26. ctx.Data["Title"] = ctx.Tr("sign_in")
  27. if _, ok := ctx.Session.Get("socialId").(int64); ok {
  28. ctx.Data["IsSocialLogin"] = true
  29. ctx.HTML(200, SIGNIN)
  30. return
  31. }
  32. if setting.OauthService != nil {
  33. ctx.Data["OauthEnabled"] = true
  34. ctx.Data["OauthService"] = setting.OauthService
  35. }
  36. // Check auto-login.
  37. uname := ctx.GetCookie(setting.CookieUserName)
  38. if len(uname) == 0 {
  39. ctx.HTML(200, SIGNIN)
  40. return
  41. }
  42. isSucceed := false
  43. defer func() {
  44. if !isSucceed {
  45. log.Trace("auto-login cookie cleared: %s", uname)
  46. ctx.SetCookie(setting.CookieUserName, "", -1)
  47. ctx.SetCookie(setting.CookieRememberName, "", -1)
  48. return
  49. }
  50. }()
  51. u, err := models.GetUserByName(uname)
  52. if err != nil {
  53. if err != models.ErrUserNotExist {
  54. ctx.Handle(500, "GetUserByName", err)
  55. }
  56. return
  57. }
  58. if val, _ := ctx.GetSuperSecureCookie(
  59. base.EncodeMd5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name {
  60. ctx.HTML(200, SIGNIN)
  61. return
  62. }
  63. isSucceed = true
  64. ctx.Session.Set("uid", u.Id)
  65. ctx.Session.Set("uname", u.Name)
  66. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  67. ctx.SetCookie("redirect_to", "", -1)
  68. ctx.Redirect(redirectTo)
  69. return
  70. }
  71. ctx.Redirect("/")
  72. }
  73. func SignInPost(ctx *middleware.Context, form auth.SignInForm) {
  74. ctx.Data["Title"] = ctx.Tr("sign_in")
  75. sid, isOauth := ctx.Session.Get("socialId").(int64)
  76. if isOauth {
  77. ctx.Data["IsSocialLogin"] = true
  78. } else if setting.OauthService != nil {
  79. ctx.Data["OauthEnabled"] = true
  80. ctx.Data["OauthService"] = setting.OauthService
  81. }
  82. if ctx.HasError() {
  83. ctx.HTML(200, SIGNIN)
  84. return
  85. }
  86. u, err := models.UserSignIn(form.UserName, form.Password)
  87. if err != nil {
  88. if err == models.ErrUserNotExist {
  89. ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), SIGNIN, &form)
  90. return
  91. }
  92. ctx.Handle(500, "UserSignIn", err)
  93. return
  94. }
  95. if form.Remember {
  96. days := 86400 * setting.LogInRememberDays
  97. ctx.SetCookie(setting.CookieUserName, u.Name, days)
  98. ctx.SetSuperSecureCookie(base.EncodeMd5(u.Rands+u.Passwd),
  99. setting.CookieRememberName, u.Name, days)
  100. }
  101. // Bind with social account.
  102. if isOauth {
  103. if err = models.BindUserOauth2(u.Id, sid); err != nil {
  104. if err == models.ErrOauth2RecordNotExist {
  105. ctx.Handle(404, "GetOauth2ById", err)
  106. } else {
  107. ctx.Handle(500, "GetOauth2ById", err)
  108. }
  109. return
  110. }
  111. ctx.Session.Delete("socialId")
  112. log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
  113. }
  114. ctx.Session.Set("uid", u.Id)
  115. ctx.Session.Set("uname", u.Name)
  116. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  117. ctx.SetCookie("redirect_to", "", -1)
  118. ctx.Redirect(redirectTo)
  119. return
  120. }
  121. ctx.Redirect("/")
  122. }
  123. func SignOut(ctx *middleware.Context) {
  124. ctx.Session.Delete("uid")
  125. ctx.Session.Delete("uname")
  126. ctx.Session.Delete("socialId")
  127. ctx.Session.Delete("socialName")
  128. ctx.Session.Delete("socialEmail")
  129. ctx.SetCookie(setting.CookieUserName, "", -1)
  130. ctx.SetCookie(setting.CookieRememberName, "", -1)
  131. ctx.Redirect("/")
  132. }
  133. func oauthSignUp(ctx *middleware.Context, sid int64) {
  134. // ctx.Data["Title"] = "OAuth Sign Up"
  135. // ctx.Data["PageIsSignUp"] = true
  136. // if _, err := models.GetOauth2ById(sid); err != nil {
  137. // if err == models.ErrOauth2RecordNotExist {
  138. // ctx.Handle(404, "user.oauthSignUp(GetOauth2ById)", err)
  139. // } else {
  140. // ctx.Handle(500, "user.oauthSignUp(GetOauth2ById)", err)
  141. // }
  142. // return
  143. // }
  144. // ctx.Data["IsSocialLogin"] = true
  145. // ctx.Data["username"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1)
  146. // ctx.Data["email"] = ctx.Session.Get("socialEmail")
  147. // log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId"))
  148. // ctx.HTML(200, SIGNUP)
  149. }
  150. func SignUp(ctx *middleware.Context) {
  151. ctx.Data["Title"] = ctx.Tr("sign_up")
  152. if setting.Service.DisableRegistration {
  153. ctx.Data["DisableRegistration"] = true
  154. ctx.HTML(200, SIGNUP)
  155. return
  156. }
  157. if sid, ok := ctx.Session.Get("socialId").(int64); ok {
  158. oauthSignUp(ctx, sid)
  159. return
  160. }
  161. ctx.HTML(200, SIGNUP)
  162. }
  163. func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
  164. ctx.Data["Title"] = ctx.Tr("sign_up")
  165. if setting.Service.DisableRegistration {
  166. ctx.Error(403)
  167. return
  168. }
  169. isOauth := false
  170. // sid, isOauth := ctx.Session.Get("socialId").(int64)
  171. // if isOauth {
  172. // ctx.Data["IsSocialLogin"] = true
  173. // }
  174. // May redirect from home page.
  175. if ctx.Query("from") == "home" {
  176. // Clear input error box.
  177. ctx.Data["Err_UserName"] = false
  178. ctx.Data["Err_Email"] = false
  179. // Make the best guess.
  180. uname := ctx.Query("uname")
  181. i := strings.Index(uname, "@")
  182. if i > -1 {
  183. ctx.Data["email"] = uname
  184. ctx.Data["uname"] = uname[:i]
  185. } else {
  186. ctx.Data["uname"] = uname
  187. }
  188. ctx.Data["password"] = ctx.Query("password")
  189. ctx.HTML(200, SIGNUP)
  190. return
  191. }
  192. if ctx.HasError() {
  193. ctx.HTML(200, SIGNUP)
  194. return
  195. }
  196. if !cpt.VerifyReq(ctx.Req) {
  197. ctx.Data["Err_Captcha"] = true
  198. ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), SIGNUP, &form)
  199. return
  200. } else if form.Password != form.Retype {
  201. ctx.Data["Err_Password"] = true
  202. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), SIGNUP, &form)
  203. return
  204. }
  205. u := &models.User{
  206. Name: form.UserName,
  207. Email: form.Email,
  208. Passwd: form.Password,
  209. IsActive: !setting.Service.RegisterEmailConfirm || isOauth,
  210. }
  211. if err := models.CreateUser(u); err != nil {
  212. switch err {
  213. case models.ErrUserAlreadyExist:
  214. ctx.Data["Err_UserName"] = true
  215. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form)
  216. case models.ErrEmailAlreadyUsed:
  217. ctx.Data["Err_Email"] = true
  218. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form)
  219. case models.ErrUserNameIllegal:
  220. ctx.Data["Err_UserName"] = true
  221. ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SIGNUP, &form)
  222. default:
  223. ctx.Handle(500, "CreateUser", err)
  224. }
  225. return
  226. }
  227. log.Trace("Account created: %s", u.Name)
  228. // Bind social account.
  229. // if isOauth {
  230. // if err = models.BindUserOauth2(u.Id, sid); err != nil {
  231. // ctx.Handle(500, "user.SignUp(BindUserOauth2)", err)
  232. // return
  233. // }
  234. // ctx.Session.Delete("socialId")
  235. // log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
  236. // }
  237. // Send confirmation e-mail, no need for social account.
  238. // if !isOauth && setting.Service.RegisterEmailConfirm && u.Id > 1 {
  239. // mailer.SendRegisterMail(ctx.Render, u)
  240. // ctx.Data["IsSendRegisterMail"] = true
  241. // ctx.Data["Email"] = u.Email
  242. // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
  243. // ctx.HTML(200, "user/activate")
  244. // if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  245. // log.Error("Set cache(MailResendLimit) fail: %v", err)
  246. // }
  247. // return
  248. // }
  249. ctx.Redirect("/user/login")
  250. }
  251. func Activate(ctx *middleware.Context) {
  252. // code := ctx.Query("code")
  253. // if len(code) == 0 {
  254. // ctx.Data["IsActivatePage"] = true
  255. // if ctx.User.IsActive {
  256. // ctx.Handle(404, "user.Activate", nil)
  257. // return
  258. // }
  259. // // Resend confirmation e-mail.
  260. // if setting.Service.RegisterEmailConfirm {
  261. // if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  262. // ctx.Data["ResendLimited"] = true
  263. // } else {
  264. // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
  265. // mailer.SendActiveMail(ctx.Render, ctx.User)
  266. // if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  267. // log.Error("Set cache(MailResendLimit) fail: %v", err)
  268. // }
  269. // }
  270. // } else {
  271. // ctx.Data["ServiceNotEnabled"] = true
  272. // }
  273. // ctx.HTML(200, ACTIVATE)
  274. // return
  275. // }
  276. // // Verify code.
  277. // if user := models.VerifyUserActiveCode(code); user != nil {
  278. // user.IsActive = true
  279. // user.Rands = models.GetUserSalt()
  280. // if err := models.UpdateUser(user); err != nil {
  281. // ctx.Handle(404, "user.Activate", err)
  282. // return
  283. // }
  284. // log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.Name)
  285. // ctx.Session.Set("userId", user.Id)
  286. // ctx.Session.Set("userName", user.Name)
  287. // ctx.Redirect("/")
  288. // return
  289. // }
  290. // ctx.Data["IsActivateFailed"] = true
  291. // ctx.HTML(200, ACTIVATE)
  292. }
  293. func ForgotPasswd(ctx *middleware.Context) {
  294. ctx.Data["Title"] = "Forgot Password"
  295. if setting.MailService == nil {
  296. ctx.Data["IsResetDisable"] = true
  297. ctx.HTML(200, FORGOT_PASSWORD)
  298. return
  299. }
  300. ctx.Data["IsResetRequest"] = true
  301. ctx.HTML(200, FORGOT_PASSWORD)
  302. }
  303. func ForgotPasswdPost(ctx *middleware.Context) {
  304. // ctx.Data["Title"] = "Forgot Password"
  305. // if setting.MailService == nil {
  306. // ctx.Handle(403, "user.ForgotPasswdPost", nil)
  307. // return
  308. // }
  309. // ctx.Data["IsResetRequest"] = true
  310. // email := ctx.Query("email")
  311. // u, err := models.GetUserByEmail(email)
  312. // if err != nil {
  313. // if err == models.ErrUserNotExist {
  314. // ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil)
  315. // } else {
  316. // ctx.Handle(500, "user.ResetPasswd(check existence)", err)
  317. // }
  318. // return
  319. // }
  320. // if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  321. // ctx.Data["ResendLimited"] = true
  322. // ctx.HTML(200, FORGOT_PASSWORD)
  323. // return
  324. // }
  325. // mailer.SendResetPasswdMail(ctx.Render, u)
  326. // if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  327. // log.Error("Set cache(MailResendLimit) fail: %v", err)
  328. // }
  329. // ctx.Data["Email"] = email
  330. // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
  331. // ctx.Data["IsResetSent"] = true
  332. // ctx.HTML(200, FORGOT_PASSWORD)
  333. }
  334. func ResetPasswd(ctx *middleware.Context) {
  335. ctx.Data["Title"] = "Reset Password"
  336. code := ctx.Query("code")
  337. if len(code) == 0 {
  338. ctx.Error(404)
  339. return
  340. }
  341. ctx.Data["Code"] = code
  342. ctx.Data["IsResetForm"] = true
  343. ctx.HTML(200, RESET_PASSWORD)
  344. }
  345. func ResetPasswdPost(ctx *middleware.Context) {
  346. // ctx.Data["Title"] = "Reset Password"
  347. // code := ctx.Query("code")
  348. // if len(code) == 0 {
  349. // ctx.Error(404)
  350. // return
  351. // }
  352. // ctx.Data["Code"] = code
  353. // if u := models.VerifyUserActiveCode(code); u != nil {
  354. // // Validate password length.
  355. // passwd := ctx.Query("passwd")
  356. // if len(passwd) < 6 || len(passwd) > 30 {
  357. // ctx.Data["IsResetForm"] = true
  358. // ctx.RenderWithErr("Password length should be in 6 and 30.", "user/reset_passwd", nil)
  359. // return
  360. // }
  361. // u.Passwd = passwd
  362. // u.Rands = models.GetUserSalt()
  363. // u.Salt = models.GetUserSalt()
  364. // u.EncodePasswd()
  365. // if err := models.UpdateUser(u); err != nil {
  366. // ctx.Handle(500, "user.ResetPasswd(UpdateUser)", err)
  367. // return
  368. // }
  369. // log.Trace("%s User password reset: %s", ctx.Req.RequestURI, u.Name)
  370. // ctx.Redirect("/user/login")
  371. // return
  372. // }
  373. // ctx.Data["IsResetFailed"] = true
  374. // ctx.HTML(200, RESET_PASSWORD)
  375. }