From 485bc01abef37dfb0e2fc4044103f6e2ae19c738 Mon Sep 17 00:00:00 2001 From: claudemiro Date: Sun, 17 May 2015 09:25:57 -0300 Subject: [PATCH] Changed SessionID format. Pusher libs are validating the sessionID format. So I need to conform with its format. --- ipe/websockets.go | 2 +- utils/utils.go | 16 +++++++--------- utils/utils_test.go | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 utils/utils_test.go diff --git a/ipe/websockets.go b/ipe/websockets.go index 71f906e..5994e2b 100644 --- a/ipe/websockets.go +++ b/ipe/websockets.go @@ -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) diff --git a/utils/utils.go b/utils/utils.go index 3824a06..157f40c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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)) } diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 0000000..b62321b --- /dev/null +++ b/utils/utils_test.go @@ -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) + } +}