auth.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package main
  2. import (
  3. "crypto/rand"
  4. "net/http"
  5. auth "imuslab.com/arozos/mod/auth"
  6. prout "imuslab.com/arozos/mod/prouter"
  7. "imuslab.com/arozos/mod/utils"
  8. )
  9. func AuthInit() {
  10. //Generate session key for authentication module if empty
  11. sysdb.NewTable("auth")
  12. if *session_key == "" {
  13. //Check if the key was generated already. If not, generate a new one
  14. if !sysdb.KeyExists("auth", "sessionkey") {
  15. key := make([]byte, 32)
  16. rand.Read(key)
  17. newSessionKey := string(key)
  18. sysdb.Write("auth", "sessionkey", newSessionKey)
  19. systemWideLogger.PrintAndLog("Auth", "New authentication session key generated", nil)
  20. } else {
  21. systemWideLogger.PrintAndLog("Auth", "Authentication session key loaded from database", nil)
  22. }
  23. skeyString := ""
  24. sysdb.Read("auth", "sessionkey", &skeyString)
  25. session_key = &skeyString
  26. }
  27. //Create an Authentication Agent
  28. authAgent = auth.NewAuthenticationAgent("ao_auth", []byte(*session_key), sysdb, *allow_public_registry, func(w http.ResponseWriter, r *http.Request) {
  29. //Login Redirection Handler, redirect it login.system
  30. w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")
  31. http.Redirect(w, r, utils.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect="+r.URL.Path, 307)
  32. })
  33. if *allow_autologin {
  34. authAgent.AllowAutoLogin = true
  35. } else {
  36. //Default is false. But just in case
  37. authAgent.AllowAutoLogin = false
  38. }
  39. //Register the API endpoints for the authentication UI
  40. http.HandleFunc("/system/auth/login", authAgent.HandleLogin)
  41. http.HandleFunc("/system/auth/logout", authAgent.HandleLogout)
  42. http.HandleFunc("/system/auth/register", authAgent.HandleRegister)
  43. http.HandleFunc("/system/auth/checkLogin", authAgent.CheckLogin)
  44. http.HandleFunc("/api/auth/login", authAgent.HandleAutologinTokenLogin)
  45. authAgent.LoadAutologinTokenFromDB()
  46. }
  47. func AuthSettingsInit() {
  48. //Authentication related settings
  49. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  50. ModuleName: "System Setting",
  51. AdminOnly: true,
  52. UserHandler: userHandler,
  53. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  54. utils.SendErrorResponse(w, "Permission Denied")
  55. },
  56. })
  57. //Handle additional batch operations
  58. adminRouter.HandleFunc("/system/auth/csvimport", authAgent.HandleCreateUserAccountsFromCSV)
  59. adminRouter.HandleFunc("/system/auth/groupdel", authAgent.HandleUserDeleteByGroup)
  60. //System for logging and displaying login user information
  61. registerSetting(settingModule{
  62. Name: "Connection Log",
  63. Desc: "Logs for login attempts",
  64. IconPath: "SystemAO/security/img/small_icon.png",
  65. Group: "Security",
  66. StartDir: "SystemAO/security/connlog.html",
  67. RequireAdmin: true,
  68. })
  69. adminRouter.HandleFunc("/system/auth/logger/index", authAgent.Logger.HandleIndexListing)
  70. adminRouter.HandleFunc("/system/auth/logger/list", authAgent.Logger.HandleTableListing)
  71. //Blacklist Management
  72. registerSetting(settingModule{
  73. Name: "Access Control",
  74. Desc: "Prevent / Allow certain IP ranges from logging in",
  75. IconPath: "SystemAO/security/img/small_icon.png",
  76. Group: "Security",
  77. StartDir: "SystemAO/security/accesscontrol.html",
  78. RequireAdmin: true,
  79. })
  80. //Whitelist API
  81. adminRouter.HandleFunc("/system/auth/whitelist/enable", authAgent.WhitelistManager.HandleSetWhitelistEnable)
  82. adminRouter.HandleFunc("/system/auth/whitelist/list", authAgent.WhitelistManager.HandleListWhitelistedIPs)
  83. adminRouter.HandleFunc("/system/auth/whitelist/set", authAgent.WhitelistManager.HandleAddWhitelistedIP)
  84. adminRouter.HandleFunc("/system/auth/whitelist/unset", authAgent.WhitelistManager.HandleRemoveWhitelistedIP)
  85. //Blacklist API
  86. adminRouter.HandleFunc("/system/auth/blacklist/enable", authAgent.BlacklistManager.HandleSetBlacklistEnable)
  87. adminRouter.HandleFunc("/system/auth/blacklist/list", authAgent.BlacklistManager.HandleListBannedIPs)
  88. adminRouter.HandleFunc("/system/auth/blacklist/ban", authAgent.BlacklistManager.HandleAddBannedIP)
  89. adminRouter.HandleFunc("/system/auth/blacklist/unban", authAgent.BlacklistManager.HandleRemoveBannedIP)
  90. //Register nightly task for clearup all user retry counter
  91. nightlyManager.RegisterNightlyTask(authAgent.ExpDelayHandler.ResetAllUserRetryCounter)
  92. }