1
0

handlers.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package sso
  2. /*
  3. handlers.go
  4. This file contains the handlers for the SSO module.
  5. If you are looking for handlers for SSO user management,
  6. please refer to userHandlers.go.
  7. */
  8. import (
  9. "encoding/json"
  10. "net/http"
  11. "strings"
  12. "github.com/gofrs/uuid"
  13. "imuslab.com/zoraxy/mod/utils"
  14. )
  15. // HandleSSOStatus handle the request to get the status of the SSO portal server
  16. func (s *SSOHandler) HandleSSOStatus(w http.ResponseWriter, r *http.Request) {
  17. type SSOStatus struct {
  18. Enabled bool
  19. SSOInterceptEnabled bool
  20. ListeningPort int
  21. AuthURL string
  22. }
  23. status := SSOStatus{
  24. Enabled: s.ssoPortalServer != nil,
  25. //SSOInterceptEnabled: s.ssoInterceptEnabled,
  26. ListeningPort: s.Config.PortalServerPort,
  27. AuthURL: s.Config.AuthURL,
  28. }
  29. js, _ := json.Marshal(status)
  30. utils.SendJSONResponse(w, string(js))
  31. }
  32. // HandleStartSSOPortal handle the request to start the SSO portal server
  33. func (s *SSOHandler) HandleStartSSOPortal(w http.ResponseWriter, r *http.Request) {
  34. err := s.StartSSOPortal()
  35. if err != nil {
  36. s.Log("Failed to start SSO portal server", err)
  37. utils.SendErrorResponse(w, "failed to start SSO portal server")
  38. return
  39. }
  40. //Write current state to database
  41. err = s.Config.Database.Write("sso_conf", "enabled", true)
  42. if err != nil {
  43. utils.SendErrorResponse(w, "failed to update SSO state")
  44. return
  45. }
  46. utils.SendOK(w)
  47. }
  48. // HandleStopSSOPortal handle the request to stop the SSO portal server
  49. func (s *SSOHandler) HandleStopSSOPortal(w http.ResponseWriter, r *http.Request) {
  50. err := s.ssoPortalServer.Close()
  51. if err != nil {
  52. s.Log("Failed to stop SSO portal server", err)
  53. utils.SendErrorResponse(w, "failed to stop SSO portal server")
  54. return
  55. }
  56. s.ssoPortalServer = nil
  57. //Write current state to database
  58. err = s.Config.Database.Write("sso_conf", "enabled", false)
  59. if err != nil {
  60. utils.SendErrorResponse(w, "failed to update SSO state")
  61. return
  62. }
  63. //Clear the cookie store and restart the server
  64. err = s.RestartSSOServer()
  65. if err != nil {
  66. utils.SendErrorResponse(w, "failed to restart SSO server")
  67. return
  68. }
  69. utils.SendOK(w)
  70. }
  71. // HandlePortChange handle the request to change the SSO portal server port
  72. func (s *SSOHandler) HandlePortChange(w http.ResponseWriter, r *http.Request) {
  73. if r.Method == http.MethodGet {
  74. //Return the current port
  75. js, _ := json.Marshal(s.Config.PortalServerPort)
  76. utils.SendJSONResponse(w, string(js))
  77. return
  78. }
  79. port, err := utils.PostInt(r, "port")
  80. if err != nil {
  81. utils.SendErrorResponse(w, "invalid port given")
  82. return
  83. }
  84. s.Config.PortalServerPort = port
  85. //Write to the database
  86. err = s.Config.Database.Write("sso_conf", "port", port)
  87. if err != nil {
  88. utils.SendErrorResponse(w, "failed to update port")
  89. return
  90. }
  91. //Clear the cookie store and restart the server
  92. err = s.RestartSSOServer()
  93. if err != nil {
  94. utils.SendErrorResponse(w, "failed to restart SSO server")
  95. return
  96. }
  97. utils.SendOK(w)
  98. }
  99. // HandleSetAuthURL handle the request to change the SSO auth URL
  100. // This is the URL that the SSO portal server will redirect to for authentication
  101. // e.g. auth.yourdomain.com
  102. func (s *SSOHandler) HandleSetAuthURL(w http.ResponseWriter, r *http.Request) {
  103. if r.Method == http.MethodGet {
  104. //Return the current auth URL
  105. js, _ := json.Marshal(s.Config.AuthURL)
  106. utils.SendJSONResponse(w, string(js))
  107. return
  108. }
  109. //Get the auth URL
  110. authURL, err := utils.PostPara(r, "auth_url")
  111. if err != nil {
  112. utils.SendErrorResponse(w, "invalid auth URL given")
  113. return
  114. }
  115. s.Config.AuthURL = authURL
  116. //Write to the database
  117. err = s.Config.Database.Write("sso_conf", "authurl", authURL)
  118. if err != nil {
  119. utils.SendErrorResponse(w, "failed to update auth URL")
  120. return
  121. }
  122. //Clear the cookie store and restart the server
  123. err = s.RestartSSOServer()
  124. if err != nil {
  125. utils.SendErrorResponse(w, "failed to restart SSO server")
  126. return
  127. }
  128. utils.SendOK(w)
  129. }
  130. // HandleRegisterApp handle the request to register a new app to the SSO portal
  131. func (s *SSOHandler) HandleRegisterApp(w http.ResponseWriter, r *http.Request) {
  132. appName, err := utils.PostPara(r, "app_name")
  133. if err != nil {
  134. utils.SendErrorResponse(w, "invalid app name given")
  135. return
  136. }
  137. id, err := utils.PostPara(r, "app_id")
  138. if err != nil {
  139. //If id is not given, use the app name with a random UUID
  140. newID, err := uuid.NewV4()
  141. if err != nil {
  142. utils.SendErrorResponse(w, "failed to generate new app ID")
  143. return
  144. }
  145. id = strings.ReplaceAll(appName, " ", "") + "-" + newID.String()
  146. }
  147. appDomain, err := utils.PostPara(r, "app_domain")
  148. if err != nil {
  149. utils.SendErrorResponse(w, "invalid app URL given")
  150. return
  151. }
  152. appURLs := strings.Split(appDomain, ",")
  153. //Remove padding and trailing spaces in each URL
  154. for i := range appURLs {
  155. appURLs[i] = strings.TrimSpace(appURLs[i])
  156. }
  157. //Create a new app entry
  158. thisAppEntry := RegisteredUpstreamApp{
  159. ID: id,
  160. Secret: "",
  161. Domain: appURLs,
  162. Scopes: []string{},
  163. SessionDuration: 3600,
  164. }
  165. js, _ := json.Marshal(thisAppEntry)
  166. //Create a new app in the database
  167. err = s.Config.Database.Write("sso_apps", appName, string(js))
  168. if err != nil {
  169. utils.SendErrorResponse(w, "failed to create new app")
  170. return
  171. }
  172. utils.SendOK(w)
  173. }