security.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. prout "imuslab.com/arozos/mod/prouter"
  6. "imuslab.com/arozos/mod/security/csrf"
  7. "imuslab.com/arozos/mod/utils"
  8. )
  9. /*
  10. Security.go
  11. Author: tobychui
  12. This module handles the system security related functions.
  13. If you are looking for authentication or login related features, see auth.go
  14. */
  15. var (
  16. CSRFTokenManager *csrf.TokenManager
  17. tokenExpireTime int64 = 10 //Token expire in 10 seconds
  18. tokenCleaningTime int = int(tokenExpireTime) * 12 //Tokens are cleared every 12 x tokenExpireTime
  19. )
  20. //Initiation function
  21. func security_init() {
  22. //Create a default permission router accessable by everyone
  23. router := prout.NewModuleRouter(prout.RouterOption{
  24. ModuleName: "",
  25. AdminOnly: false,
  26. UserHandler: userHandler,
  27. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  28. utils.SendErrorResponse(w, "Permission Denied")
  29. },
  30. })
  31. //Creat a new CSRF Token Manager and token expire in 30 seconds
  32. CSRFTokenManager = csrf.NewTokenManager(userHandler, tokenExpireTime)
  33. //Register functions related to CSRF Tokens
  34. router.HandleFunc("/system/csrf/new", CSRFTokenManager.HandleNewToken)
  35. //Create a timer to clear expired tokens
  36. ticker := time.NewTicker(time.Duration(tokenCleaningTime) * time.Second)
  37. go func() {
  38. for {
  39. select {
  40. case <-ticker.C:
  41. CSRFTokenManager.ClearExpiredTokens()
  42. }
  43. }
  44. }()
  45. }