server.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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("/sso/login", h.HandleLogin)
  24. //Register OAuth2 endpoints
  25. h.Oauth2Server.RegisterOauthEndpoints(pmux)
  26. h.ssoPortalMux = pmux
  27. }
  28. // StartSSOPortal start the SSO portal server
  29. // This function will block the main thread, call it in a goroutine
  30. func (h *SSOHandler) StartSSOPortal() error {
  31. h.ssoPortalServer = &http.Server{
  32. Addr: ":" + strconv.Itoa(h.Config.PortalServerPort),
  33. Handler: h.ssoPortalMux,
  34. }
  35. err := h.ssoPortalServer.ListenAndServe()
  36. if err != nil {
  37. h.Log("Failed to start SSO portal server", err)
  38. }
  39. return err
  40. }
  41. // StopSSOPortal stop the SSO portal server
  42. func (h *SSOHandler) StopSSOPortal() error {
  43. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  44. defer cancel()
  45. err := h.ssoPortalServer.Shutdown(ctx)
  46. if err != nil {
  47. h.Log("Failed to stop SSO portal server", err)
  48. return err
  49. }
  50. return nil
  51. }
  52. // StartSSOPortal start the SSO portal server
  53. func (h *SSOHandler) RestartSSOServer() error {
  54. err := h.StopSSOPortal()
  55. if err != nil {
  56. return err
  57. }
  58. go h.StartSSOPortal()
  59. return nil
  60. }
  61. func (h *SSOHandler) IsRunning() bool {
  62. return h.ssoPortalServer != nil
  63. }
  64. // HandleLogin handle the login request
  65. func (h *SSOHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
  66. //Handle the login request
  67. username, err := utils.PostPara(r, "username")
  68. if err != nil {
  69. utils.SendErrorResponse(w, "invalid username or password")
  70. return
  71. }
  72. password, err := utils.PostPara(r, "password")
  73. if err != nil {
  74. utils.SendErrorResponse(w, "invalid username or password")
  75. return
  76. }
  77. rememberMe, err := utils.PostBool(r, "remember_me")
  78. if err != nil {
  79. rememberMe = false
  80. }
  81. //Check if the user exists
  82. userEntry, err := h.GetSSOUser(username)
  83. if err != nil {
  84. utils.SendErrorResponse(w, "user not found")
  85. return
  86. }
  87. //Check if the password is correct
  88. if !userEntry.VerifyPassword(password) {
  89. utils.SendErrorResponse(w, "incorrect password")
  90. return
  91. }
  92. //Create a new session for the user
  93. session, _ := h.cookieStore.Get(r, "Zoraxy-SSO")
  94. session.Values["username"] = username
  95. if rememberMe {
  96. session.Options.MaxAge = 86400 * 15 //15 days
  97. } else {
  98. session.Options.MaxAge = 3600 //1 hour
  99. }
  100. session.Save(r, w) //Save the session
  101. utils.SendOK(w)
  102. }