server.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package sso
  2. import (
  3. "context"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. "imuslab.com/zoraxy/mod/utils"
  8. )
  9. /*
  10. server.go
  11. This is the web server for the SSO portal. It contains the
  12. HTTP server and the handlers for the SSO portal.
  13. If you are looking for handlers that changes the settings
  14. of the SSO portale or user management, please refer to
  15. handlers.go.
  16. */
  17. func (h *SSOHandler) InitSSOPortal(portalServerPort int) {
  18. //Create a new web server for the SSO portal
  19. pmux := http.NewServeMux()
  20. fs := http.FileServer(http.FS(staticFiles))
  21. pmux.Handle("/", fs)
  22. //Register API endpoint for the SSO portal
  23. pmux.HandleFunc("/login", h.HandleLogin)
  24. //Add more API endpoints here
  25. h.ssoPortalMux = pmux
  26. }
  27. // StartSSOPortal start the SSO portal server
  28. func (h *SSOHandler) StartSSOPortal() error {
  29. h.ssoPortalServer = &http.Server{
  30. Addr: ":" + strconv.Itoa(h.Config.PortalServerPort),
  31. Handler: h.ssoPortalMux,
  32. }
  33. err := h.ssoPortalServer.ListenAndServe()
  34. if err != nil {
  35. h.Log("Failed to start SSO portal server", err)
  36. }
  37. return err
  38. }
  39. // StopSSOPortal stop the SSO portal server
  40. func (h *SSOHandler) StopSSOPortal() error {
  41. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  42. defer cancel()
  43. err := h.ssoPortalServer.Shutdown(ctx)
  44. if err != nil {
  45. h.Log("Failed to stop SSO portal server", err)
  46. return err
  47. }
  48. return nil
  49. }
  50. // HandleLogin handle the login request
  51. func (h *SSOHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
  52. //Handle the login request
  53. username, err := utils.PostPara(r, "username")
  54. if err != nil {
  55. utils.SendErrorResponse(w, "invalid username or password")
  56. return
  57. }
  58. password, err := utils.PostPara(r, "password")
  59. if err != nil {
  60. utils.SendErrorResponse(w, "invalid username or password")
  61. return
  62. }
  63. rememberMe, err := utils.PostBool(r, "remember_me")
  64. if err != nil {
  65. rememberMe = false
  66. }
  67. //Check if the user exists
  68. userEntry, err := h.SSO_GetUser(username)
  69. if err != nil {
  70. utils.SendErrorResponse(w, "user not found")
  71. return
  72. }
  73. //Check if the password is correct
  74. if !userEntry.VerifyPassword(password) {
  75. utils.SendErrorResponse(w, "incorrect password")
  76. return
  77. }
  78. //Create a new session for the user
  79. session, _ := h.cookieStore.Get(r, "Zoraxy-SSO")
  80. session.Values["username"] = username
  81. if rememberMe {
  82. session.Options.MaxAge = 86400 * 15 //15 days
  83. } else {
  84. session.Options.MaxAge = 3600 //1 hour
  85. }
  86. session.Save(r, w) //Save the session
  87. utils.SendOK(w)
  88. }