summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-12-29 03:04:07 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-12-29 03:04:07 (GMT)
commitb84ca16ba73b56229b3c99dc2a9dd61508519c5e (patch)
treea3ae771ecca35a4708e203656a3cf90c845e2644 /src
parentcd8dd43123550f7e01bd09e6f5d8bb6dbd2bfee0 (diff)
improved session id generation with less deps
Diffstat (limited to 'src')
-rw-r--r--src/helsinki.at/rhimport/session_store.go31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/helsinki.at/rhimport/session_store.go b/src/helsinki.at/rhimport/session_store.go
index b7ea3d9..5025d8c 100644
--- a/src/helsinki.at/rhimport/session_store.go
+++ b/src/helsinki.at/rhimport/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
}