auth.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 !config.ConfigInstance().RegistrationEnabled {
  37. s.error(http.StatusNotImplemented, "Registration is disabled on this server", w)
  38. return
  39. }
  40. //Check if user already logged in and entered register page accidently
  41. if s.authenticator.Verify(s.extractAuth(w, r)) {
  42. http.Redirect(w, r, "/m/0", http.StatusTemporaryRedirect)
  43. return
  44. }
  45. switch r.Method {
  46. case "GET":
  47. fmt.Fprint(w, s.templater.ExecuteRegister(&struct {
  48. Version string
  49. Domain string
  50. }{common.Version, config.ConfigInstance().MyDomain}))
  51. return
  52. case "POST":
  53. user := r.FormValue("user")
  54. password := r.FormValue("password")
  55. fullName := r.FormValue("fullName")
  56. if user != "" && password != "" && fullName != "" {
  57. ok, email := s.checkEmail(user)
  58. if ok && len(password) < 128 && len(fullName) < 128 && utils.RegExpUtilsInstance().FullNameChecker.MatchString(fullName) {
  59. err := s.storage.AddUser(email, password, fullName)
  60. if err != nil {
  61. log.Println(err.Error())
  62. s.error(http.StatusInternalServerError, "Unable to create user", w)
  63. return
  64. }
  65. s.scanner.Reconfigure()
  66. token, _ := s.authenticator.Login(email, password)
  67. s.login(email, token, w, r)
  68. return
  69. }
  70. }
  71. }
  72. s.error(http.StatusNotImplemented, "Invalid registration handling", w)
  73. }
  74. func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
  75. switch r.Method {
  76. case "GET":
  77. //Check if user already logged in and entered login page accidently
  78. if s.authenticator.Verify(s.extractAuth(w, r)) {
  79. http.Redirect(w, r, "/m/0", http.StatusTemporaryRedirect)
  80. return
  81. }
  82. case "POST":
  83. //Check passed in form login/password pair first
  84. user := r.FormValue("user")
  85. password := r.FormValue("password")
  86. token, ok := s.authenticator.Login(user, password)
  87. if ok {
  88. s.login(user, token, w, r)
  89. return
  90. }
  91. }
  92. var signupTemplate template.HTML
  93. if config.ConfigInstance().RegistrationEnabled {
  94. signupTemplate = template.HTML(s.templater.ExecuteSignup(""))
  95. } else {
  96. signupTemplate = ""
  97. }
  98. //Otherwise make sure user logged out and show login page
  99. s.logout(w, r)
  100. fmt.Fprint(w, s.templater.ExecuteLogin(&struct {
  101. Version string
  102. Signup template.HTML
  103. }{common.Version, signupTemplate}))
  104. }
  105. func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
  106. s.logout(w, r)
  107. http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
  108. }
  109. func (s *Server) handleCheckEmail(w http.ResponseWriter, r *http.Request) {
  110. if err := r.ParseForm(); err == nil {
  111. if ok, _ := s.checkEmail(r.FormValue("user")); ok {
  112. w.Write([]byte{0})
  113. return
  114. }
  115. s.error(http.StatusNotAcceptable, "Email exists", w)
  116. return
  117. }
  118. s.error(http.StatusBadRequest, "Invalid arguments", w)
  119. return
  120. }
  121. func (s *Server) checkEmail(user string) (bool, string) {
  122. email := user + "@" + config.ConfigInstance().MyDomain
  123. return utils.RegExpUtilsInstance().EmailChecker.MatchString(email) && !s.storage.CheckEmailExists(email), email
  124. }
  125. func (s *Server) logout(w http.ResponseWriter, r *http.Request) {
  126. session, err := s.sessionStore.Get(r, CookieSessionToken)
  127. if err == nil {
  128. if session.Values["user"] != nil && session.Values["token"] != nil {
  129. s.authenticator.Logout(session.Values["user"].(string), session.Values["token"].(string))
  130. }
  131. session.Values["user"] = ""
  132. session.Values["token"] = ""
  133. session.Save(r, w)
  134. }
  135. }
  136. func (s *Server) login(user, token string, w http.ResponseWriter, r *http.Request) {
  137. session, _ := s.sessionStore.Get(r, CookieSessionToken)
  138. session.Values["user"] = user
  139. session.Values["token"] = token
  140. session.Save(r, w)
  141. http.Redirect(w, r, "/m/0", http.StatusTemporaryRedirect)
  142. }