forked from pusher/pusher-http-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
105 lines (93 loc) · 2.36 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package pusher
import (
"errors"
"encoding/json"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
var channelValidationRegex = regexp.MustCompile("^[-a-zA-Z0-9_=@,.;]+$")
var socketIDValidationRegex = regexp.MustCompile(`\A\d+\.\d+\z`)
var maxChannelNameSize = 200
func jsonMarshalToString(data interface{}) (result string, err error) {
var _result []byte
_result, err = json.Marshal(data)
if err != nil {
return
}
return string(_result), err
}
func authTimestamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
func parseUserAuthenticationRequestParams(_params []byte) (socketID string, err error) {
params, err := url.ParseQuery(string(_params))
if err != nil {
return
}
if _, ok := params["socket_id"]; !ok {
return "", errors.New("socket_id not found")
}
return params["socket_id"][0], nil
}
func parseChannelAuthorizationRequestParams(_params []byte) (channelName string, socketID string, err error) {
params, err := url.ParseQuery(string(_params))
if err != nil {
return
}
if _, ok := params["channel_name"]; !ok {
return "", "", errors.New("channel_name not found")
}
if _, ok := params["socket_id"]; !ok {
return "", "", errors.New("socket_id not found")
}
return params["channel_name"][0], params["socket_id"][0], nil
}
func validUserId(userId string) bool {
length := len(userId)
return length > 0 && length < maxChannelNameSize
}
func validChannel(channel string) bool {
if len(channel) > maxChannelNameSize || !channelValidationRegex.MatchString(channel) {
return false
}
return true
}
func channelsAreValid(channels []string) bool {
for _, channel := range channels {
if !validChannel(channel) {
return false
}
}
return true
}
func isEncryptedChannel(channel string) bool {
if strings.HasPrefix(channel, "private-encrypted-") {
return true
}
return false
}
func validateUserData(userData map[string]interface{}) (err error) {
_id, ok := userData["id"]
if !ok || _id == nil {
return errors.New("Missing id in user data")
}
var id string
id, ok = _id.(string)
if !ok {
return errors.New("id field in user data is not a string")
}
if !validUserId(id) {
return fmt.Errorf("Invalid id in user data: '%s'", id)
}
return
}
func validateSocketID(socketID *string) (err error) {
if (socketID == nil) || socketIDValidationRegex.MatchString(*socketID) {
return
}
return errors.New("socket_id invalid")
}