forked from GoIncremental/negroni-sessions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_token.go
34 lines (26 loc) · 821 Bytes
/
cookie_token.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
package sessions
import (
"net/http"
"github.com/gorilla/sessions"
)
//TokenGetSetter allows you to save and retrieve a value stored in a cookie
type TokenGetSetter interface {
GetToken(req *http.Request, name string) (string, error)
SetToken(rw http.ResponseWriter, name, value string, options *sessions.Options)
}
//NewCookieToken returns the default TokenGetSetter
func NewCookieToken() TokenGetSetter {
return &cookieToken{}
}
type cookieToken struct{}
func (c *cookieToken) GetToken(req *http.Request, name string) (string, error) {
cook, err := req.Cookie(name)
if err != nil {
return "", err
}
return cook.Value, nil
}
func (c *cookieToken) SetToken(rw http.ResponseWriter, name string, value string,
options *sessions.Options) {
http.SetCookie(rw, sessions.NewCookie(name, value, options))
}