1
0

server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // HandleLogin handle the login request
  62. func (h *SSOHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
  63. //Handle the login request
  64. username, err := utils.PostPara(r, "username")
  65. if err != nil {
  66. utils.SendErrorResponse(w, "invalid username or password")
  67. return
  68. }
  69. password, err := utils.PostPara(r, "password")
  70. if err != nil {
  71. utils.SendErrorResponse(w, "invalid username or password")
  72. return
  73. }
  74. rememberMe, err := utils.PostBool(r, "remember_me")
  75. if err != nil {
  76. rememberMe = false
  77. }
  78. //Check if the user exists
  79. userEntry, err := h.SSO_GetUser(username)
  80. if err != nil {
  81. utils.SendErrorResponse(w, "user not found")
  82. return
  83. }
  84. //Check if the password is correct
  85. if !userEntry.VerifyPassword(password) {
  86. utils.SendErrorResponse(w, "incorrect password")
  87. return
  88. }
  89. //Create a new session for the user
  90. session, _ := h.cookieStore.Get(r, "Zoraxy-SSO")
  91. session.Values["username"] = username
  92. if rememberMe {
  93. session.Options.MaxAge = 86400 * 15 //15 days
  94. } else {
  95. session.Options.MaxAge = 3600 //1 hour
  96. }
  97. session.Save(r, w) //Save the session
  98. utils.SendOK(w)
  99. }