securezone.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. switch r.URL.Path {
  39. case "/settings":
  40. s.handleSettings(w, user)
  41. case "/update":
  42. s.handleUpdate(w, r, user)
  43. // case "/admin":
  44. // case "/addUser":
  45. // //TODO:
  46. // case "/removeUser":
  47. // //TODO:
  48. // case "/changeUser":
  49. // //TODO:
  50. }
  51. }
  52. func (s *Server) handleSettings(w http.ResponseWriter, user string) {
  53. info, err := s.storage.GetUserInfo(user)
  54. if err != nil {
  55. s.error(http.StatusInternalServerError, "Unable to obtain user information", w)
  56. return
  57. }
  58. fmt.Fprintf(w, s.templater.ExecuteSettings(&struct {
  59. Version string
  60. FullName string
  61. }{common.Version, info.FullName}))
  62. }
  63. func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request, user string) {
  64. if err := r.ParseForm(); err != nil {
  65. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  66. return
  67. }
  68. oldPassword := r.FormValue("oldPassword")
  69. if err := s.authenticator.CheckUser(user, oldPassword); err != nil {
  70. s.error(http.StatusUnauthorized, "Password entered is invalid", w)
  71. return
  72. }
  73. password := r.FormValue("password")
  74. fullName := r.FormValue("fullName")
  75. err := s.storage.UpdateUser(user, password, fullName)
  76. if err != nil {
  77. log.Println(err.Error())
  78. s.error(http.StatusInternalServerError, "Unable to update user data", w)
  79. return
  80. }
  81. w.Write([]byte{0})
  82. }