web_admin.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package ldap
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "regexp"
  6. "strconv"
  7. "imuslab.com/arozos/mod/auth/ldap/ldapreader"
  8. "imuslab.com/arozos/mod/common"
  9. )
  10. func (ldap *ldapHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
  11. //basic components
  12. enabled, err := strconv.ParseBool(ldap.readSingleConfig("enabled"))
  13. if err != nil {
  14. common.SendTextResponse(w, "Invalid config value [key=enabled].")
  15. return
  16. }
  17. //get the LDAP config from db
  18. BindUsername := ldap.readSingleConfig("BindUsername")
  19. BindPassword := ldap.readSingleConfig("BindPassword")
  20. FQDN := ldap.readSingleConfig("FQDN")
  21. BaseDN := ldap.readSingleConfig("BaseDN")
  22. //marshall it and return
  23. config, err := json.Marshal(Config{
  24. Enabled: enabled,
  25. BindUsername: BindUsername,
  26. BindPassword: BindPassword,
  27. FQDN: FQDN,
  28. BaseDN: BaseDN,
  29. })
  30. if err != nil {
  31. empty, err := json.Marshal(Config{})
  32. if err != nil {
  33. common.SendErrorResponse(w, "Error while marshalling config")
  34. }
  35. common.SendJSONResponse(w, string(empty))
  36. }
  37. common.SendJSONResponse(w, string(config))
  38. }
  39. func (ldap *ldapHandler) WriteConfig(w http.ResponseWriter, r *http.Request) {
  40. //receive the parameter
  41. enabled, err := common.Mv(r, "enabled", true)
  42. if err != nil {
  43. common.SendErrorResponse(w, "enabled field can't be empty")
  44. return
  45. }
  46. //allow empty fields if enabled is false
  47. showError := true
  48. if enabled != "true" {
  49. showError = false
  50. }
  51. //four fields to store the LDAP authentication information
  52. BindUsername, err := common.Mv(r, "bind_username", true)
  53. if err != nil {
  54. if showError {
  55. common.SendErrorResponse(w, "bind_username field can't be empty")
  56. return
  57. }
  58. }
  59. BindPassword, err := common.Mv(r, "bind_password", true)
  60. if err != nil {
  61. if showError {
  62. common.SendErrorResponse(w, "bind_password field can't be empty")
  63. return
  64. }
  65. }
  66. FQDN, err := common.Mv(r, "fqdn", true)
  67. if err != nil {
  68. if showError {
  69. common.SendErrorResponse(w, "fqdn field can't be empty")
  70. return
  71. }
  72. }
  73. BaseDN, err := common.Mv(r, "base_dn", true)
  74. if err != nil {
  75. if showError {
  76. common.SendErrorResponse(w, "base_dn field can't be empty")
  77. return
  78. }
  79. }
  80. //write the data back to db
  81. ldap.coredb.Write("ldap", "enabled", enabled)
  82. ldap.coredb.Write("ldap", "BindUsername", BindUsername)
  83. ldap.coredb.Write("ldap", "BindPassword", BindPassword)
  84. ldap.coredb.Write("ldap", "FQDN", FQDN)
  85. ldap.coredb.Write("ldap", "BaseDN", BaseDN)
  86. //update the new authencation infromation
  87. ldap.ldapreader = ldapreader.NewLDAPReader(BindUsername, BindPassword, FQDN, BaseDN)
  88. //return ok
  89. common.SendOK(w)
  90. }
  91. func (ldap *ldapHandler) TestConnection(w http.ResponseWriter, r *http.Request) {
  92. //marshall it and return the connection status
  93. userList, totalLength, err := ldap.getAllUser(10)
  94. if err != nil {
  95. errMessage, err := json.Marshal(syncorizeUserReturnInterface{Error: err.Error()})
  96. if err != nil {
  97. common.SendErrorResponse(w, "{\"error\":\"Error while marshalling information\"}")
  98. return
  99. }
  100. common.SendJSONResponse(w, string(errMessage))
  101. return
  102. }
  103. returnJSON := syncorizeUserReturnInterface{Userinfo: userList, Length: len(userList), TotalLength: totalLength, Error: ""}
  104. accountJSON, err := json.Marshal(returnJSON)
  105. if err != nil {
  106. errMessage, err := json.Marshal(syncorizeUserReturnInterface{Error: err.Error()})
  107. if err != nil {
  108. common.SendErrorResponse(w, "{\"error\":\"Error while marshalling information\"}")
  109. return
  110. }
  111. common.SendJSONResponse(w, string(errMessage))
  112. return
  113. }
  114. common.SendJSONResponse(w, string(accountJSON))
  115. }
  116. func (ldap *ldapHandler) checkCurrUserAdmin(w http.ResponseWriter, r *http.Request) bool {
  117. //check current user is admin and new update will remove it or not
  118. currentLoggedInUser, err := ldap.userHandler.GetUserInfoFromRequest(w, r)
  119. if err != nil {
  120. common.SendErrorResponse(w, "Error while getting user info")
  121. return false
  122. }
  123. ldapCurrUserInfo, err := ldap.ldapreader.GetUser(currentLoggedInUser.Username)
  124. if err != nil {
  125. common.SendErrorResponse(w, "Error while getting user info from LDAP")
  126. return false
  127. }
  128. isAdmin := false
  129. //get the croups out from LDAP group list
  130. regexSyntax := regexp.MustCompile("cn=([^,]+),")
  131. for _, v := range ldapCurrUserInfo.GetAttributeValues("memberOf") {
  132. //loop through all memberOf's array
  133. groups := regexSyntax.FindStringSubmatch(v)
  134. //if after regex there is still groups exists
  135. if len(groups) > 0 {
  136. //check if the LDAP group is already exists in ArOZOS system
  137. if ldap.permissionHandler.GroupExists(groups[1]) {
  138. if ldap.permissionHandler.GetPermissionGroupByName(groups[1]).IsAdmin {
  139. isAdmin = true
  140. }
  141. }
  142. }
  143. }
  144. return isAdmin
  145. }
  146. func (ldap *ldapHandler) SynchronizeUser(w http.ResponseWriter, r *http.Request) {
  147. //check if suer is admin before executing the command
  148. //if user is admin then check if user will lost him/her's admin access
  149. consistencyCheck := ldap.checkCurrUserAdmin(w, r)
  150. if !consistencyCheck {
  151. common.SendErrorResponse(w, "You will no longer become the admin after synchronizing, synchronize terminated")
  152. return
  153. }
  154. err := ldap.SynchronizeUserFromLDAP()
  155. if err != nil {
  156. common.SendErrorResponse(w, err.Error())
  157. return
  158. }
  159. common.SendOK(w)
  160. }