auth.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package main
  2. import (
  3. "net/http"
  4. "strings"
  5. "log"
  6. "encoding/json"
  7. "imuslab.com/arozos/mod/auth"
  8. )
  9. var (
  10. // key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)
  11. key = []byte("super-secret-key") //To be migrated to input flags
  12. )
  13. /*
  14. Initiation of web services endpoints from main()
  15. This function should be the preparation of auth services and register the url for auth services only
  16. Do not put in any computational algorithms
  17. */
  18. func authRegisterHandlerEndpoints(authAgent *auth.AuthAgent){
  19. //Initiate auth services with system database
  20. authAgent = auth.NewAuthenticationAgent("ao_auth", key, sysdb)
  21. //Handle auth API
  22. http.HandleFunc("/system/auth/login", authAgent.HandleLogin)
  23. http.HandleFunc("/system/auth/logout", authAgent.HandleLogout)
  24. http.HandleFunc("/system/auth/checkLogin", authAgent.CheckLogin)
  25. http.HandleFunc("/system/auth/register", authAgent.HandleRegister) //Require implemtantion of group check
  26. http.HandleFunc("/system/auth/unregister", authAgent.HandleUnregister) //Require implementation of admin check
  27. //Handle other related APUs
  28. http.HandleFunc("/system/auth/reflectIP", system_auth_getIPAddress)
  29. http.HandleFunc("/system/auth/checkPublicRegister", system_auth_checkPublicRegister)
  30. log.Println("ArOZ Online Authentication Service Loaded");
  31. if (*allow_public_registry){
  32. //Allow public registry. Create setting interface for this page
  33. registerSetting(settingModule{
  34. Name: "Public Register",
  35. Desc: "Settings for public registration",
  36. IconPath: "SystemAO/auth/img/small_icon.png",
  37. Group: "Users",
  38. StartDir: "SystemAO/auth/regsetting.html",
  39. RequireAdmin: true,
  40. })
  41. //Register the direct link for template serving
  42. http.HandleFunc("/public/register", system_auth_serveRegisterInterface);
  43. http.HandleFunc("/public/register/settings", system_auth_handleRegisterInterfaceUpdate);
  44. }
  45. }
  46. func system_auth_checkPublicRegister(w http.ResponseWriter, r *http.Request){
  47. if (!*allow_public_registry){
  48. sendJSONResponse(w, "false");
  49. return
  50. }else{
  51. AllowPublicRegisterValue := false
  52. sysdb.Read("auth", "public/register/settings/allowRegistry", &AllowPublicRegisterValue)
  53. jsonString, _ := json.Marshal(AllowPublicRegisterValue)
  54. sendJSONResponse(w, string(jsonString))
  55. return
  56. }
  57. sendJSONResponse(w, "false");
  58. }
  59. func system_auth_serveRegisterInterface(w http.ResponseWriter, r *http.Request){
  60. username, err := mv(r, "username", true)
  61. if (err != nil){
  62. //Serve WebUI
  63. //Prepare contents for templating
  64. base64Image, _ := LoadImageAsBase64("./web/" + iconVendor)
  65. requireInvitationCode := false
  66. sysdb.Read("auth", "public/register/settings/enableInvitationCode", &requireInvitationCode)
  67. eic := "false"
  68. if (requireInvitationCode){
  69. eic = "true"
  70. }
  71. //registerUI, _ := ioutil.ReadFile("./web/" + "SystemAO/auth/register.system");
  72. registerUI, _ := template_load("./web/" + "SystemAO/auth/register.system",map[string]interface{}{
  73. "vendor_logo": base64Image,
  74. "host_name": *host_name,
  75. "require_invitationCode": eic,
  76. })
  77. w.Write([]byte(registerUI))
  78. }else{
  79. //Data incoming. Register this user if data is valid
  80. requireInvitationCode := false
  81. sysdb.Read("auth", "public/register/settings/enableInvitationCode", &requireInvitationCode)
  82. //Validate Invitation Code if enabled
  83. if (requireInvitationCode){
  84. //Validate the Invitation Code
  85. userInputCode, _ := mv(r, "invitationcode", true)
  86. correctCode := ""
  87. sysdb.Read("auth", "public/register/settings/invitationCode", &correctCode)
  88. if (correctCode == ""){
  89. panic("Invalid Invitation Code")
  90. }
  91. if (userInputCode != correctCode){
  92. sendErrorResponse(w, "Invalid Invitation Code")
  93. return
  94. }
  95. }
  96. //validate if this username already occupied
  97. if authAgent.UserExists(username){
  98. sendErrorResponse(w, "This username already occupied.")
  99. return
  100. }
  101. //Validate password
  102. password, err := mv(r, "password", true)
  103. if (err != nil){
  104. sendErrorResponse(w, "Invalid password")
  105. return
  106. }
  107. if len(password) < 8{
  108. sendErrorResponse(w, "Password too short. Password must be equal or longer than 8 characters")
  109. return
  110. }
  111. //Validate default usergroup
  112. DefaultUserGroupValue := ""
  113. err = sysdb.Read("auth", "public/register/settings/defaultUserGroup", &DefaultUserGroupValue)
  114. if (err != nil){
  115. log.Println(err.Error())
  116. sendErrorResponse(w, "Internal Server Error")
  117. return
  118. }
  119. /*
  120. if (DefaultUserGroupValue == "" || !system_permission_groupExists(DefaultUserGroupValue)){
  121. log.Println("Invalid group given or group not exists: " + DefaultUserGroupValue)
  122. sendErrorResponse(w, "Internal Server Error")
  123. return
  124. }
  125. */
  126. //Ok to create user
  127. err = authAgent.CreateUserAccount(username, password, DefaultUserGroupValue)
  128. if (err != nil){
  129. log.Println(err.Error())
  130. sendErrorResponse(w, "Internal Server Error")
  131. return
  132. }
  133. sendOK(w);
  134. }
  135. }
  136. func system_auth_handleRegisterInterfaceUpdate(w http.ResponseWriter, r *http.Request){
  137. /*
  138. isAdmin := system_permission_checkUserIsAdmin(w,r)
  139. if !isAdmin{
  140. sendErrorResponse(w, "Permission denied")
  141. return
  142. }
  143. */
  144. //keys for access the properties
  145. var (
  146. rootKey string = "public/register/settings/"
  147. allowPublicRegister string = rootKey + "allowRegistry"
  148. enableInvitationCode string = rootKey + "enableInvitationCode"
  149. invitationCode string = rootKey + "invitationCode"
  150. defaultUserGroup string = rootKey + "defaultUserGroup"
  151. )
  152. opr, _ := mv(r,"opr",true);
  153. if (opr == "write"){
  154. //Write settings to db
  155. config, err := mv(r,"config",true);
  156. if err != nil{
  157. sendErrorResponse(w, "config not defined");
  158. return
  159. }
  160. type configStruct struct {
  161. Apr bool `json:"apr"`
  162. Eivc bool `json:"eivc"`
  163. Icode string `json:"icode"`
  164. Group string `json:"group"`
  165. }
  166. newConfig := new(configStruct)
  167. err = json.Unmarshal([]byte(config), &newConfig)
  168. if (err != nil){
  169. sendErrorResponse(w, err.Error())
  170. return
  171. }
  172. /*
  173. if (newConfig.Group == "" || !system_permission_groupExists(newConfig.Group)){
  174. //Group is not set. Reject update
  175. sendErrorResponse(w, "Invalid group selected");
  176. return
  177. }
  178. */
  179. //Write the configuration to file
  180. sysdb.Write("auth", allowPublicRegister, newConfig.Apr)
  181. sysdb.Write("auth", enableInvitationCode, newConfig.Eivc)
  182. sysdb.Write("auth", invitationCode, newConfig.Icode)
  183. sysdb.Write("auth", defaultUserGroup, newConfig.Group)
  184. sendOK(w)
  185. }else{
  186. //Read the current settings
  187. type replyStruct struct{
  188. AllowPublicRegister bool
  189. EnableInvitationCode bool
  190. InvitationCode string
  191. DefaultUserGroup string
  192. }
  193. var AllowPublicRegisterValue bool = false
  194. var EnableInvitationCodeValue bool = false
  195. var InvitationCodeValue string = ""
  196. var DefaultUserGroupValue string = ""
  197. sysdb.Read("auth", allowPublicRegister, &AllowPublicRegisterValue)
  198. sysdb.Read("auth", enableInvitationCode, &EnableInvitationCodeValue)
  199. sysdb.Read("auth", invitationCode, &InvitationCodeValue)
  200. sysdb.Read("auth", defaultUserGroup, &DefaultUserGroupValue)
  201. jsonString, _ := json.Marshal(replyStruct{
  202. AllowPublicRegister:AllowPublicRegisterValue,
  203. EnableInvitationCode:EnableInvitationCodeValue,
  204. InvitationCode:InvitationCodeValue,
  205. DefaultUserGroup:DefaultUserGroupValue,
  206. })
  207. sendJSONResponse(w, string(jsonString))
  208. }
  209. }