diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-29 03:04:07 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-29 03:04:07 (GMT) |
commit | 43d7fc0b4110ebf27c2847fc2660916da60629e9 (patch) | |
tree | d8758c3e9e4d93255b6b53ae9b2e0aafc0378f42 | |
parent | 46ae2f5c6db5897c00d75ce8d29cc0ad55b7b613 (diff) |
improved session id generation with less deps
-rw-r--r-- | session_store.go | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/session_store.go b/session_store.go index b7ea3d9..5025d8c 100644 --- a/session_store.go +++ b/session_store.go @@ -25,11 +25,10 @@ package rhimport import ( - "encoding/base32" + "crypto/rand" + "encoding/base64" "fmt" - "github.com/satori/go.uuid" "net/http" - "strings" ) type newSessionResponse struct { @@ -76,6 +75,14 @@ type SessionStore struct { removeChan chan removeSessionRequest } +func generateSessionId() (string, error) { + var b [32]byte + if _, err := rand.Read(b[:]); err != nil { + return "", err + } + return base64.RawStdEncoding.EncodeToString(b[:]), nil +} + func (self *SessionStore) new(ctx *ImportContext) (resp newSessionResponse) { resp.responsecode = http.StatusOK resp.errorstring = "OK" @@ -90,14 +97,18 @@ func (self *SessionStore) new(ctx *ImportContext) (resp newSessionResponse) { return } } - b := uuid.NewV4().Bytes() - resp.id = strings.ToLower(strings.TrimRight(base32.StdEncoding.EncodeToString(b), "=")) - if _, exists := self.store[ctx.UserName]; !exists { - self.store[ctx.UserName] = make(map[string]*Session) + if id, err := generateSessionId(); err != nil { + resp.responsecode = http.StatusInternalServerError + resp.errorstring = err.Error() + } else { + resp.id = id + if _, exists := self.store[ctx.UserName]; !exists { + self.store[ctx.UserName] = make(map[string]*Session) + } + self.store[ctx.UserName][resp.id] = NewSession(ctx, func() { self.GetInterface().Remove(ctx.UserName, resp.id) }) + resp.session = self.store[ctx.UserName][resp.id].getInterface() + rhdl.Printf("SessionStore: created session for '%s' -> %s", ctx.UserName, resp.id) } - self.store[ctx.UserName][resp.id] = NewSession(ctx, func() { self.GetInterface().Remove(ctx.UserName, resp.id) }) - resp.session = self.store[ctx.UserName][resp.id].getInterface() - rhdl.Printf("SessionStore: created session for '%s' -> %s", ctx.UserName, resp.id) return } |