1
0

server.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. func (h *SSOHandler) StartSSOPortal() error {
  30. h.ssoPortalServer = &http.Server{
  31. Addr: ":" + strconv.Itoa(h.Config.PortalServerPort),
  32. Handler: h.ssoPortalMux,
  33. }
  34. err := h.ssoPortalServer.ListenAndServe()
  35. if err != nil {
  36. h.Log("Failed to start SSO portal server", err)
  37. }
  38. return err
  39. }
  40. // StopSSOPortal stop the SSO portal server
  41. func (h *SSOHandler) StopSSOPortal() error {
  42. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  43. defer cancel()
  44. err := h.ssoPortalServer.Shutdown(ctx)
  45. if err != nil {
  46. h.Log("Failed to stop SSO portal server", err)
  47. return err
  48. }
  49. return nil
  50. }
  51. // HandleLogin handle the login request
  52. func (h *SSOHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
  53. //Handle the login request
  54. username, err := utils.PostPara(r, "username")
  55. if err != nil {
  56. utils.SendErrorResponse(w, "invalid username or password")
  57. return
  58. }
  59. password, err := utils.PostPara(r, "password")
  60. if err != nil {
  61. utils.SendErrorResponse(w, "invalid username or password")
  62. return
  63. }
  64. rememberMe, err := utils.PostBool(r, "remember_me")
  65. if err != nil {
  66. rememberMe = false
  67. }
  68. //Check if the user exists
  69. userEntry, err := h.SSO_GetUser(username)
  70. if err != nil {
  71. utils.SendErrorResponse(w, "user not found")
  72. return
  73. }
  74. //Check if the password is correct
  75. if !userEntry.VerifyPassword(password) {
  76. utils.SendErrorResponse(w, "incorrect password")
  77. return
  78. }
  79. //Create a new session for the user
  80. session, _ := h.cookieStore.Get(r, "Zoraxy-SSO")
  81. session.Values["username"] = username
  82. if rememberMe {
  83. session.Options.MaxAge = 86400 * 15 //15 days
  84. } else {
  85. session.Options.MaxAge = 3600 //1 hour
  86. }
  87. session.Save(r, w) //Save the session
  88. utils.SendOK(w)
  89. }