Skip to content

Commit

Permalink
detect cookie errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jtolio committed Dec 28, 2016
1 parent 540cb22 commit ef4c1ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 11 additions & 3 deletions whsess/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,34 @@ func (cs *CookieStore) Save(w http.ResponseWriter, namespace string,
}
value := base64.URLEncoding.EncodeToString(
secretbox.Seal(nonce[:], out.Bytes(), &nonce, &cs.Secret))
http.SetCookie(w, &http.Cookie{

return setCookie(w, &http.Cookie{
Name: namespace,
Value: value,
Path: cs.Options.Path,
Domain: cs.Options.Domain,
MaxAge: cs.Options.MaxAge,
Secure: cs.Options.Secure,
HttpOnly: cs.Options.HttpOnly})
}

func setCookie(w http.ResponseWriter, cookie *http.Cookie) error {
v := cookie.String()
if v == "" {
return SessionError.New("invalid cookie %#v", cookie.Name)
}
w.Header().Add("Set-Cookie", v)
return nil
}

// Clear implements the Store interface. Not expected to be used directly.
func (cs *CookieStore) Clear(w http.ResponseWriter, namespace string) error {
http.SetCookie(w, &http.Cookie{
return setCookie(w, &http.Cookie{
Name: namespace,
Value: "",
Path: cs.Options.Path,
Domain: cs.Options.Domain,
MaxAge: -1,
Secure: cs.Options.Secure,
HttpOnly: cs.Options.HttpOnly})
return nil
}
4 changes: 3 additions & 1 deletion whsess/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type ctxKey int

var (
reqCtxKey ctxKey = 1

SessionError = errors.NewClass("session")
)

type SessionData struct {
Expand Down Expand Up @@ -62,7 +64,7 @@ func HandlerWithStore(s Store, h http.Handler) http.Handler {
func Load(ctx context.Context, namespace string) (*Session, error) {
rc, ok := ctx.Value(reqCtxKey).(*reqCtx)
if !ok {
return nil, errors.ProgrammerError.New(
return nil, SessionError.New(
"no session store handler wrapper installed")
}
if rc.cache != nil {
Expand Down

0 comments on commit ef4c1ca

Please sign in to comment.