Skip to content

Commit 2334be0

Browse files
committed
fix: csrf
1 parent 81ba6f2 commit 2334be0

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

csrf/csrf.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ func New() func(http.Handler) http.Handler {
7979
}
8080
}
8181

82+
func newToken(ctx context.Context, signer *securecookie.SecureCookie) (string, error) {
83+
encoded, err := signer.Encode(CookiesName, cookieValue{UserID: session.GetSession(ctx).UserID})
84+
if err != nil {
85+
return "", err
86+
}
87+
88+
return encoded, nil
89+
}
90+
8291
func Verify(r *http.Request, formValue string) bool {
8392
signer := r.Context().Value(signerKey).(*securecookie.SecureCookie)
8493
cookieToken := r.Context().Value(tokenKey).(string)
@@ -96,10 +105,15 @@ func Verify(r *http.Request, formValue string) bool {
96105
return v.UserID == session.GetSession(r.Context()).UserID
97106
}
98107

99-
func Clear(w http.ResponseWriter) {
108+
func Clear(w http.ResponseWriter, r *http.Request) {
109+
token, err := newToken(r.Context(), r.Context().Value(signerKey).(*securecookie.SecureCookie))
110+
if err != nil {
111+
panic("failed to encode new token")
112+
}
113+
100114
http.SetCookie(w, &http.Cookie{
101115
Name: CookiesName,
102-
Value: "",
116+
Value: token,
103117
Path: "/",
104118
Secure: true,
105119
HttpOnly: true,

0 commit comments

Comments
 (0)