securezone.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "log"
  29. "net/http"
  30. "git.semlanik.org/semlanik/gostfix/common"
  31. )
  32. func (s *Server) handleSecureZone(w http.ResponseWriter, r *http.Request) {
  33. user, token := s.extractAuth(w, r)
  34. if !s.authenticator.Verify(user, token) {
  35. s.error(http.StatusUnauthorized, "You are not allowed to access this function", w)
  36. return
  37. }
  38. s.error(http.StatusNotImplemented, "Admin panel is not implemented", w)
  39. }
  40. func (s *Server) handleSettings(w http.ResponseWriter, r *http.Request) {
  41. user, token := s.extractAuth(w, r)
  42. if !s.authenticator.Verify(user, token) {
  43. s.error(http.StatusUnauthorized, "You are not allowed to access this function", w)
  44. return
  45. }
  46. switch r.Method {
  47. case "GET":
  48. info, err := s.storage.GetUserInfo(user)
  49. if err != nil {
  50. s.error(http.StatusInternalServerError, "Unable to obtain user information", w)
  51. return
  52. }
  53. fmt.Fprintf(w, s.templater.ExecuteSettings(&struct {
  54. Version string
  55. FullName string
  56. }{common.Version, info.FullName}))
  57. case "PATCH":
  58. s.handleSettingsUpdate(w, r, user)
  59. }
  60. }
  61. func (s *Server) handleSettingsUpdate(w http.ResponseWriter, r *http.Request, user string) {
  62. if err := r.ParseForm(); err != nil {
  63. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  64. return
  65. }
  66. oldPassword := r.FormValue("oldPassword")
  67. if err := s.authenticator.CheckUser(user, oldPassword); err != nil {
  68. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  69. return
  70. }
  71. password := r.FormValue("password")
  72. fullName := r.FormValue("fullName")
  73. err := s.storage.UpdateUser(user, password, fullName)
  74. if err != nil {
  75. log.Println(err.Error())
  76. s.error(http.StatusInternalServerError, "Unable to update user data", w)
  77. return
  78. }
  79. w.Write([]byte{0})
  80. }