auth.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main
  2. import (
  3. "crypto/rand"
  4. "log"
  5. "net/http"
  6. auth "imuslab.com/arozos/mod/auth"
  7. "imuslab.com/arozos/mod/common"
  8. prout "imuslab.com/arozos/mod/prouter"
  9. )
  10. func AuthInit() {
  11. //Generate session key for authentication module if empty
  12. sysdb.NewTable("auth")
  13. if *session_key == "" {
  14. //Check if the key was generated already. If not, generate a new one
  15. if !sysdb.KeyExists("auth", "sessionkey") {
  16. key := make([]byte, 32)
  17. rand.Read(key)
  18. newSessionKey := string(key)
  19. sysdb.Write("auth", "sessionkey", newSessionKey)
  20. log.Println("New authentication session key generated")
  21. } else {
  22. log.Println("Authentication session key loaded from database")
  23. }
  24. skeyString := ""
  25. sysdb.Read("auth", "sessionkey", &skeyString)
  26. session_key = &skeyString
  27. }
  28. //Create an Authentication Agent
  29. authAgent = auth.NewAuthenticationAgent("ao_auth", []byte(*session_key), sysdb, *allow_public_registry, func(w http.ResponseWriter, r *http.Request) {
  30. //Login Redirection Handler, redirect it login.system
  31. w.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")
  32. http.Redirect(w, r, common.ConstructRelativePathFromRequestURL(r.RequestURI, "login.system")+"?redirect="+r.URL.Path, 307)
  33. })
  34. if *allow_autologin == true {
  35. authAgent.AllowAutoLogin = true
  36. } else {
  37. //Default is false. But just in case
  38. authAgent.AllowAutoLogin = false
  39. }
  40. //Register the API endpoints for the authentication UI
  41. authAgent.RegisterPublicAPIs(auth.AuthEndpoints{
  42. Login: "/system/auth/login",
  43. Logout: "/system/auth/logout",
  44. Register: "/system/auth/register",
  45. CheckLoggedIn: "/system/auth/checkLogin",
  46. Autologin: "/api/auth/login",
  47. })
  48. authAgent.LoadAutologinTokenFromDB()
  49. }
  50. func AuthSettingsInit() {
  51. //Authentication related settings
  52. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  53. ModuleName: "System Setting",
  54. AdminOnly: true,
  55. UserHandler: userHandler,
  56. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  57. sendErrorResponse(w, "Permission Denied")
  58. },
  59. })
  60. //Handle additional batch operations
  61. adminRouter.HandleFunc("/system/auth/csvimport", authAgent.HandleCreateUserAccountsFromCSV)
  62. adminRouter.HandleFunc("/system/auth/groupdel", authAgent.HandleUserDeleteByGroup)
  63. //System for logging and displaying login user information
  64. //Register FTP Server Setting page
  65. registerSetting(settingModule{
  66. Name: "Connection Log",
  67. Desc: "Logs for login attempts",
  68. IconPath: "SystemAO/security/img/small_icon.png",
  69. Group: "Security",
  70. StartDir: "SystemAO/security/connlog.html",
  71. RequireAdmin: true,
  72. })
  73. adminRouter.HandleFunc("/system/auth/logger/index", authAgent.Logger.HandleIndexListing)
  74. adminRouter.HandleFunc("/system/auth/logger/list", authAgent.Logger.HandleTableListing)
  75. }