Changed SessionID format.
Pusher libs are validating the sessionID format. So I need to conform with its format.
This commit is contained in:
+1
-1
@@ -226,7 +226,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionID := utils.RandomHash()
|
sessionID := utils.GenerateSessionID()
|
||||||
|
|
||||||
if err := onOpen(conn, w, r, sessionID, app); err != nil {
|
if err := onOpen(conn, w, r, sessionID, app); err != nil {
|
||||||
emitWSError(err, conn)
|
emitWSError(err, conn)
|
||||||
|
|||||||
+7
-9
@@ -6,10 +6,11 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base32"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result
|
// 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
|
// Generate a new random Hash
|
||||||
func RandomHash() string {
|
func GenerateSessionID() string {
|
||||||
b := make([]byte, 25)
|
MAX := 999999999
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
|
||||||
if _, err := rand.Read(b); err != nil {
|
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
|
||||||
panic("websockets: Could not generate a random session ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
return base32.StdEncoding.EncodeToString(b)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user