replace zxq.co/ripple/hanayo

This commit is contained in:
Alicia
2019-02-23 13:29:15 +00:00
commit c3d206c173
5871 changed files with 1353715 additions and 0 deletions

40
services/cieca/csrf.go Normal file
View File

@@ -0,0 +1,40 @@
package cieca
import (
"strconv"
"time"
"github.com/thehowl/cieca"
"github.com/osuYozora/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
}

View File

@@ -0,0 +1,20 @@
package cieca
import (
"testing"
)
func TestCSRF(t *testing.T) {
c := NewCSRF()
tok, err := c.Generate(1009)
if err != nil {
t.Fatal(err)
}
ok, err := c.Validate(1009, tok)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("ok is false")
}
}