@@ -16,7 +16,7 @@ type key int
1616const tokenKey = key (1 )
1717const signerKey = key (2 )
1818
19- const CookiesName = "x-csrf-token-2 "
19+ const CookiesName = "x-csrf-token-3 "
2020const FormName = "x-csrf-token"
2121
2222func GetToken (r * http.Request ) string {
@@ -45,11 +45,14 @@ func New() func(http.Handler) http.Handler {
4545 return
4646 }
4747
48+ ctx := r .Context ()
49+ ctx = context .WithValue (ctx , signerKey , signer )
50+
4851 c , err := r .Cookie (CookiesName )
4952 if err == nil && c .Value != "" {
50- next . ServeHTTP ( w , r . WithContext (
51- context . WithValue ( context . WithValue ( r . Context (), signerKey , signer ), tokenKey , c . Value ),
52- ))
53+ ctx = context . WithValue ( ctx , tokenKey , c . Value )
54+
55+ next . ServeHTTP ( w , r . WithContext ( ctx ))
5356 return
5457 }
5558
@@ -69,18 +72,23 @@ func New() func(http.Handler) http.Handler {
6972 MaxAge : int (time .Hour / time .Second ) * 24 * 7 ,
7073 })
7174
72- next . ServeHTTP ( w , r . WithContext (
73- context . WithValue ( context . WithValue ( r . Context (), signerKey , signer ), tokenKey , encoded ),
74- ))
75+ ctx = context . WithValue ( ctx , tokenKey , encoded )
76+
77+ next . ServeHTTP ( w , r . WithContext ( ctx ))
7578 })
7679 }
7780}
7881
79- func Verify (r * http.Request , token string ) bool {
82+ func Verify (r * http.Request , formValue string ) bool {
8083 signer := r .Context ().Value (signerKey ).(* securecookie.SecureCookie )
84+ cookieToken := r .Context ().Value (tokenKey ).(string )
85+
86+ if cookieToken != formValue {
87+ return false
88+ }
8189
8290 var v cookieValue
83- err := signer .Decode (CookiesName , token , & v )
91+ err := signer .Decode (CookiesName , formValue , & v )
8492 if err != nil {
8593 return false
8694 }
0 commit comments