Permalink
Browse files

Add Session.Id() to return a UUID for the session.

Using the simpleuuid package.
  • Loading branch information...
1 parent b3b68a2 commit 4c3767c492d5d2c2492e2dda3b12338ae0ae8f15 @robfig committed Feb 8, 2013
Showing with 23 additions and 0 deletions.
  1. +23 −0 session.go
View
@@ -1,15 +1,35 @@
package revel
import (
+ "github.com/streadway/simpleuuid"
"net/http"
"net/url"
"strings"
+ "time"
)
// A signed cookie (and thus limited to 4kb in size).
// Restriction: Keys may not have a colon in them.
type Session map[string]string
+const (
+ SESSION_ID_KEY = "_ID"
+)
+
+// Return a UUID identifying this session.
+func (s Session) Id() string {
+ if uuidStr, ok := s[SESSION_ID_KEY]; ok {
+ return uuidStr
+ }
+
+ uuid, err := simpleuuid.NewTime(time.Now())
+ if err != nil {
+ panic(err) // I don't think this can actually happen.
+ }
+ s[SESSION_ID_KEY] = uuid.String()
+ return s[SESSION_ID_KEY]
+}
+
type SessionPlugin struct{ EmptyPlugin }
func (p SessionPlugin) BeforeRequest(c *Controller) {
@@ -20,6 +40,9 @@ func (p SessionPlugin) AfterRequest(c *Controller) {
// Store the session (and sign it).
var sessionValue string
for key, value := range c.Session {
+ if strings.Contains(key, ":") {
+ panic("Session keys may not have colons")
+ }
sessionValue += "\x00" + key + ":" + value + "\x00"
}
sessionData := url.QueryEscape(sessionValue)

0 comments on commit 4c3767c

Please sign in to comment.