Changed SessionID format.

Pusher libs are validating the sessionID format. So I need to conform
with its format.
This commit is contained in:
claudemiro
2015-05-17 09:25:57 -03:00
parent 7b578c4fb4
commit 485bc01abe
3 changed files with 28 additions and 10 deletions
+1 -1
View File
@@ -226,7 +226,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
return
}
sessionID := utils.RandomHash()
sessionID := utils.GenerateSessionID()
if err := onOpen(conn, w, r, sessionID, app); err != nil {
emitWSError(err, conn)
+7 -9
View File
@@ -6,10 +6,11 @@ package utils
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base32"
"encoding/hex"
"fmt"
"math/rand"
"time"
)
// HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result
@@ -22,12 +23,9 @@ func HashMAC(message, key []byte) string {
}
// Generate a new random Hash
func RandomHash() string {
b := make([]byte, 25)
func GenerateSessionID() string {
MAX := 999999999
rand.Seed(time.Now().Unix())
if _, err := rand.Read(b); err != nil {
panic("websockets: Could not generate a random session ID")
}
return base32.StdEncoding.EncodeToString(b)
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
}
+20
View File
@@ -0,0 +1,20 @@
// Copyright 2015 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package utils
import (
"fmt"
"regexp"
"testing"
)
func TestGenerateSession(t *testing.T) {
sessionID := GenerateSessionID()
fmt.Println(sessionID)
if matched, _ := regexp.MatchString("^\\d+\\.\\d+$", sessionID); !matched {
t.Errorf("Must match ^\\d+\\.\\d+$, value: '%s'", sessionID)
}
}