Authentication
You can check for authentication and force-close a client at any state of your application, on Server.OnConnect or through event callbacks for example. The recommended way is to do that on the HTTP handshake, before upgrade to the websocket subprotocol, but it really depends on your app's flow and requirements.
Let's see three small examples here, two with net/http and one more real-world example using JWT authentication and Iris.
The first one is simple enough, it does the authentication before the websocket server's handler execution, through a middleware using the std net/http library.
Let's assume that the isAuthenticated(r *http.Request) bool function is our method to check if a client request is authenticated or not.
// [websocketServer := neffos.New...]
router := http.NewServeMux()
router.HandleFunc("/websocket_endpoint", func(w http.ResponseWriter, r *http.Request){
if !isAuthenticated(r) {
statusCode := http.StatusUnauthorized // 401.
http.Error(w, http.StatusText(statusCode), statusCode)
return
}
// if succeed continue to our websocket app.
websocketServer.ServeHTTP(w,r)
})
http.ListenAndServe(":8080", router)The second one is simple too, we just move the authentication process to the Server.OnConnect instead. You can get the original http.Request by Conn.Socket().Request().
Remember, if a Server.OnConnect event returns a non-nil error then the server immediately closes the connection before everything else. The client-side will receive this error through its client, err := neffos.Dial(...) for Go client and catch callback of the javascript's neffos.dial(...) function(see below).
Finally, let's continue with a more real-world example using JWT and Iris. Iris offers a community-driven jwt middleware to use.
1. Install it by executing the following shell command:
2. This example extracts the token through a "token" url parameter. Authenticated clients should be designed to set that with a signed token. This middleware has two methods, the first one is the Serve method - it is an iris.Handler and the second one is the CheckJWT(iris.Context) bool.
3. To register it before websocket upgrade process, you simply prepend the jwt j middleware to the registered websocket endpoint route, before the websocket.Handler(websocketServer).
3.1. To register it on an event you can use jwt middleware's CheckJWT(iris.Context) error method and return any error to the event callback.
4. To get the claims/the payload inside a websocket event callback:
when you have access to the jwt middleware you can just use its
Getmethod.
otherwise use the
ctx.Values().Get("jwt")which is the key that the*jwt.Tokenvalue is stored on authenticated requests.
5. And the client-side, at this case a browser, might look something like that.
Of course in production, your server side must contain routes that their job will be to create the tokens for each of your users and then the client-side must store it and send it as HTTP Header on each request to the server, the default jwt's behavior to extract a token value is by the
Authentication: Bearer $TOKENheader.
Last updated