aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Silverlock <matt@eatsleeprepeat.net>2016-03-29 08:20:53 +0300
committerMatt Silverlock <matt@eatsleeprepeat.net>2016-03-31 07:13:44 +0300
commit6ac16e3b5cbfaeb260678ec53e1e052a50b01232 (patch)
treea2d5a55f79ac3774deb64cce6b2917357968929f
parent8dacca26977607e637262eb66b15b7d39f2d3009 (diff)
[feature] NopEncoder: accept/return []byte.
- [docs] Update doc.go for golint.
-rw-r--r--doc.go2
-rw-r--r--securecookie.go25
2 files changed, 26 insertions, 1 deletions
diff --git a/doc.go b/doc.go
index e80e3ae..ae89408 100644
--- a/doc.go
+++ b/doc.go
@@ -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.