fix cookie -- not working
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash"
|
||||
"hash/crc32"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@@ -99,7 +100,7 @@ func (c *nativeCodec) Encode(session *SessionContext) (string, error) {
|
||||
|
||||
plaintext := session.Plaintext
|
||||
if plaintext == "" {
|
||||
plaintext = serializeValues(session.Values, session.OrderedKeys)
|
||||
plaintext = serializeCookieValues(session.Values, session.OrderedKeys, c.cfg.CookieIV)
|
||||
}
|
||||
return c.encryptInternal(plaintext)
|
||||
}
|
||||
@@ -321,6 +322,67 @@ func serializeValues(values map[string]string, orderedKeys []string) string {
|
||||
return strings.Join(pairs, fieldSeparator)
|
||||
}
|
||||
|
||||
func serializeCookieValues(values map[string]string, orderedKeys []string, cookieIV string) string {
|
||||
if len(values) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
keys := orderedValueKeys(values, orderedKeys, "checksum")
|
||||
if len(keys) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
for _, key := range keys {
|
||||
builder.WriteString(key)
|
||||
builder.WriteString(pairSeparator)
|
||||
builder.WriteString(values[key])
|
||||
builder.WriteString(fieldSeparator)
|
||||
}
|
||||
|
||||
checksum := crc32.ChecksumIEEE([]byte(cookieIV + builder.String()))
|
||||
builder.WriteString("checksum")
|
||||
builder.WriteString(pairSeparator)
|
||||
builder.WriteString(fmt.Sprintf("%d", checksum))
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func orderedValueKeys(values map[string]string, orderedKeys []string, excluded ...string) []string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
excludeSet := make(map[string]struct{}, len(excluded))
|
||||
for _, key := range excluded {
|
||||
excludeSet[key] = struct{}{}
|
||||
}
|
||||
|
||||
keys := make([]string, 0, len(values))
|
||||
seen := map[string]struct{}{}
|
||||
for _, key := range orderedKeys {
|
||||
if _, skip := excludeSet[key]; skip {
|
||||
continue
|
||||
}
|
||||
if _, ok := values[key]; ok {
|
||||
keys = append(keys, key)
|
||||
seen[key] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
extra := make([]string, 0)
|
||||
for key := range values {
|
||||
if _, skip := excludeSet[key]; skip {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[key]; !ok {
|
||||
extra = append(extra, key)
|
||||
}
|
||||
}
|
||||
sort.Strings(extra)
|
||||
keys = append(keys, extra...)
|
||||
return keys
|
||||
}
|
||||
|
||||
func int64Ptr(value string) *int64 {
|
||||
if value == "" {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user