diff options
author | Matt Silverlock <matt@eatsleeprepeat.net> | 2016-04-22 16:45:19 +0300 |
---|---|---|
committer | Matt Silverlock <matt@eatsleeprepeat.net> | 2016-04-22 16:45:19 +0300 |
commit | 667fe4e3466a040b780561fe9b51a83a3753eefc (patch) | |
tree | 36612f0532c3fd1489aaa3f2504f03c97179bc01 | |
parent | 4c7f85bfe584193f9ff70acbaf06f3b81e5d5bf8 (diff) | |
parent | a29e8718b61897fd1e4c14d8cf4d4baf173a26e8 (diff) |
Merge branch 'master' of github.com:gorilla/securecookie
-rw-r--r-- | doc.go | 2 | ||||
-rw-r--r-- | securecookie.go | 25 |
2 files changed, 26 insertions, 1 deletions
@@ -3,7 +3,7 @@ // license that can be found in the LICENSE file. /* -Package gorilla/securecookie encodes and decodes authenticated and optionally +Package securecookie encodes and decodes authenticated and optionally encrypted cookie values. Secure cookies can't be forged, because their values are validated using HMAC. diff --git a/securecookie.go b/securecookie.go index be43e9d..83dd606 100644 --- a/securecookie.go +++ b/securecookie.go @@ -101,6 +101,7 @@ var ( errTimestampTooNew = cookieError{typ: decodeError, msg: "timestamp is too new"} errTimestampExpired = cookieError{typ: decodeError, msg: "expired timestamp"} errDecryptionFailed = cookieError{typ: decodeError, msg: "the value could not be decrypted"} + errValueNotByte = cookieError{typ: decodeError, msg: "value not a []byte."} // ErrMacInvalid indicates that cookie decoding failed because the HMAC // could not be extracted and verified. Direct use of this error @@ -181,6 +182,11 @@ type GobEncoder struct{} // json.Unmarshaller interfaces. type JSONEncoder struct{} +// NopEncoder does not encode cookie values, and instead simply accepts a []byte +// (as an interface{}) and returns a []byte. This is particularly useful when +// you encoding an object upstream and do not wish to re-encode it. +type NopEncoder struct{} + // MaxLength restricts the maximum length, in bytes, for the cookie value. // // Default is 4096, which is the maximum value accepted by Internet Explorer. @@ -457,6 +463,25 @@ func (e JSONEncoder) Deserialize(src []byte, dst interface{}) error { return nil } +// Serialize passes a []byte through as-is. +func (e NopEncoder) Serialize(src interface{}) ([]byte, error) { + if b, ok := src.([]byte); ok { + return b, nil + } + + return nil, errValueNotByte +} + +// Deserialize passes a []byte through as-is. +func (e NopEncoder) Deserialize(src []byte, dst interface{}) error { + if _, ok := dst.([]byte); ok { + dst = src + return nil + } + + return errValueNotByte +} + // Encoding ------------------------------------------------------------------- // encode encodes a value using base64. |