forked from osuripple/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrf.go
40 lines (34 loc) · 706 Bytes
/
csrf.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
package cieca
import (
"strconv"
"time"
"github.com/thehowl/cieca"
"zxq.co/ripple/hanayo/services"
"zxq.co/x/rs"
)
// NewCSRF creates a new CSRF service as described in the services.CSRF
// interface.
func NewCSRF() services.CSRF {
return &ciecaCSRF{
DataStore: new(cieca.DataStore),
}
}
type ciecaCSRF struct {
*cieca.DataStore
}
func (c *ciecaCSRF) Generate(u int) (string, error) {
var s string
for {
s = rs.String(10)
_, e := c.GetWithExist(s)
if !e {
break
}
}
c.SetWithExpiration(strconv.Itoa(u)+s, nil, time.Minute*15)
return s, nil
}
func (c *ciecaCSRF) Validate(u int, token string) (bool, error) {
_, e := c.GetWithExist(strconv.Itoa(u) + token)
return e, nil
}