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