auth.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package web
  26. import (
  27. "fmt"
  28. template "html/template"
  29. "log"
  30. "net/http"
  31. "git.semlanik.org/semlanik/gostfix/common"
  32. "git.semlanik.org/semlanik/gostfix/config"
  33. "git.semlanik.org/semlanik/gostfix/utils"
  34. )
  35. func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) {
  36. // if session, err := s.sessionStore.Get(r, CookieSessionToken); err == nil && session.Values["user"] != nil && session.Values["token"] != nil {
  37. // http.Redirect(w, r, "/m0", http.StatusTemporaryRedirect)
  38. // return
  39. // }
  40. if !config.ConfigInstance().RegistrationEnabled {
  41. s.error(http.StatusNotImplemented, "Registration is disabled on this server", w)
  42. return
  43. }
  44. //Check if user already logged in and entered register page accidently
  45. if s.authenticator.Verify(s.extractAuth(w, r)) {
  46. http.Redirect(w, r, "/m/0", http.StatusTemporaryRedirect)
  47. return
  48. }
  49. switch r.Method {
  50. case "GET":
  51. fmt.Fprint(w, s.templater.ExecuteRegister(&struct {
  52. Version string
  53. Domain string
  54. }{common.Version, config.ConfigInstance().MyDomain}))
  55. return
  56. case "POST":
  57. user := r.FormValue("user")
  58. password := r.FormValue("password")
  59. fullName := r.FormValue("fullName")
  60. if user != "" && password != "" && fullName != "" {
  61. ok, email := s.checkEmail(user)
  62. if ok && len(password) < 128 && len(fullName) < 128 && utils.RegExpUtilsInstance().FullNameChecker.MatchString(fullName) {
  63. err := s.storage.AddUser(email, password, fullName)
  64. if err != nil {
  65. log.Println(err.Error())
  66. s.error(http.StatusInternalServerError, "Unable to create user", w)
  67. return
  68. }
  69. s.scanner.Reconfigure()
  70. token, _ := s.authenticator.Login(email, password)
  71. s.login(email, token, w, r)
  72. return
  73. }
  74. }
  75. }
  76. s.error(http.StatusNotImplemented, "Invalid registration handling", w)
  77. }
  78. func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
  79. switch r.Method {
  80. case "GET":
  81. //Check if user already logged in and entered login page accidently
  82. if s.authenticator.Verify(s.extractAuth(w, r)) {
  83. http.Redirect(w, r, "/m/0", http.StatusTemporaryRedirect)
  84. return
  85. }
  86. var signupTemplate template.HTML
  87. if config.ConfigInstance().RegistrationEnabled {
  88. signupTemplate = template.HTML(s.templater.ExecuteSignup(""))
  89. } else {
  90. signupTemplate = ""
  91. }
  92. //Otherwise make sure user logged out and show login page
  93. s.logout(w, r)
  94. fmt.Fprint(w, s.templater.ExecuteLogin(&struct {
  95. Version string
  96. Signup template.HTML
  97. }{common.Version, signupTemplate}))
  98. case "POST":
  99. //Check passed in form login/password pair first
  100. user := r.FormValue("user")
  101. password := r.FormValue("password")
  102. token, ok := s.authenticator.Login(user, password)
  103. if ok {
  104. s.login(user, token, w, r)
  105. return
  106. }
  107. }
  108. }
  109. func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
  110. s.logout(w, r)
  111. http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
  112. }
  113. func (s *Server) handleCheckEmail(w http.ResponseWriter, r *http.Request) {
  114. if err := r.ParseForm(); err == nil {
  115. if ok, _ := s.checkEmail(r.FormValue("user")); ok {
  116. w.Write([]byte{0})
  117. return
  118. }
  119. s.error(http.StatusNotAcceptable, "Email exists", w)
  120. return
  121. }
  122. s.error(http.StatusBadRequest, "Invalid arguments", w)
  123. return
  124. }
  125. func (s *Server) checkEmail(user string) (bool, string) {
  126. email := user + "@" + config.ConfigInstance().MyDomain
  127. return utils.RegExpUtilsInstance().EmailChecker.MatchString(email) && !s.storage.CheckEmailExists(email), email
  128. }
  129. func (s *Server) logout(w http.ResponseWriter, r *http.Request) {
  130. session, err := s.sessionStore.Get(r, CookieSessionToken)
  131. if err == nil {
  132. if session.Values["user"] != nil && session.Values["token"] != nil {
  133. s.authenticator.Logout(session.Values["user"].(string), session.Values["token"].(string))
  134. }
  135. session.Values["user"] = ""
  136. session.Values["token"] = ""
  137. session.Save(r, w)
  138. }
  139. }
  140. func (s *Server) login(user, token string, w http.ResponseWriter, r *http.Request) {
  141. session, _ := s.sessionStore.Get(r, CookieSessionToken)
  142. session.Values["user"] = user
  143. session.Values["token"] = token
  144. session.Save(r, w)
  145. http.Redirect(w, r, "/m/0", http.StatusTemporaryRedirect)
  146. }