File tree 1 file changed +22
-2
lines changed
1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change 1
1
package web
2
2
3
3
import (
4
+ "encoding/json"
4
5
"net/http"
5
6
6
7
"github.com/gorilla/websocket"
@@ -24,14 +25,33 @@ var upgrader = websocket.Upgrader{
24
25
25
26
func (s * Server ) socketHandler (endpointHandle SocketHandle , options HandleOptions ) httprouter.Handle {
26
27
return func (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
28
+ var userData interface {}
29
+
30
+ if options .AuthenticateMethod != nil {
31
+ userData = options .AuthenticateMethod (r )
32
+ if isUserdataNil (userData ) {
33
+ if options .UnauthorizedMethod == nil {
34
+ s .log .Warn ("Rejected authenticated request" )
35
+ w .Header ().Set ("Content-Type" , "application/json" )
36
+ w .WriteHeader (http .StatusUnauthorized )
37
+ json .NewEncoder (w ).Encode (Error {401 , "Unauthorized" })
38
+ return
39
+ }
40
+
41
+ options .UnauthorizedMethod (w , r )
42
+ return
43
+ }
44
+ }
45
+
27
46
conn , err := upgrader .Upgrade (w , r , nil )
28
47
if err != nil {
29
48
s .log .Error ("Error upgrading client for websocket connection: %s" , err .Error ())
30
49
return
31
50
}
32
51
endpointHandle (Request {
33
- Params : ps ,
34
- log : s .log ,
52
+ Params : ps ,
53
+ UserData : userData ,
54
+ log : s .log ,
35
55
}, WSConn {
36
56
c : conn ,
37
57
})
You can’t perform that action at this time.
0 commit comments