Is it secure to authenticate web socket connections using jwt?


Victor Lazzi

If client-side javascript can access JWT encryption, are they secure? If not, how can I use them to authenticate my web socket connection?

Pascal Lamers

tl;dr - yes, you can use JWT for web socket connections if you keep certain regulations in mind


Let's first summarize what a JSON Web Token ( JWT) is. A JWTconsists of three parts: Header , Payload and Signature .

  • The header contains the type of token and signature algorithm, ie, HS256 or RSA. The header is Base64Url encoded.

  • The payload contains the claims and can also include custom data like the user ID from the database. The payload is well Base64Url encoded.

  • The signature (the last part of the token) is created out of the signed header , in the payload , and Secretknown only to itself, using the specified algorithm.

So knowing this, we can say that the content of the token (headers and payload) is not secure and therefore should not contain sensitive data. However, the fact that the token is only signed secretwith what you know (your server) makes it very secure, for its purpose. Because even if the tokens are used from the client, only you can issue those tokens, and only you can create valid tokens. You can't simply forge a token that can be used in your api. Someone can still steal the token on the client side, but it's to your advantage to keep the token's expiry time short.


Authenticating websocket connections using JWT

As long as you don't use long lived tokens, or even infinitely valid tokens (expiration time), I'd say this is a solid solution. Typically JWTs are used in a REST API environment, so the user first authorizes using an authentication endpoint (such as username and password) and then distributes a valid JWT.

I would suggest to also establish a network socket connection (socket.io in your case). Like this (of course it depends on your backend):

// client

const axios = require('axios');
const io = require('socket.io-client');

const { token } = await axios.post('/auth/login', credentials);

const socket = io("ws://example.com/", {
  auth: {
    token: "123"
  }
});
// server
// socket.io middleware

io.use((socket, next) => {
  const token = socket.handshake.auth.token;
  // ... check token or whatever
});

Related


Authenticate socket io connection using JWT

el_pup_le : How to verify socket.io connection? My application uses the login endpoint from another server (python) to get the token, how can I use that token whenever the user opens a socket connection on the node side? io.on('connection', function(socket) {

Authenticate socket io connection using JWT

el_pup_le : How to verify socket.io connection? My application uses the login endpoint from another server (python) to get a token, how can I use that token whenever the user opens a socket connection on the node side? io.on('connection', function(socket) {

Authenticate socket io connection using JWT

el_pup_le : How to verify socket.io connection? My application uses the login endpoint from another server (python) to get a token, how can I use that token whenever the user opens a socket connection on the node side? io.on('connection', function(socket) {

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Unable to authenticate desktop APP using Web API OWIN JWT token

Ed Lopez I'm building a desktop APP using Windows Forms that needs to be authenticated via a WebAPI using token authentication. The API is proven to work because the mobile APP is using it and I can also get results using POSTMAN The problem is when I call the

Secure API using JWT

Caroline I'm currently learning how JWTs work and I'm using the API. I've made a middleware function so for every call to the secure route, the middleware is called and the accessToken is parsed. I am passing the token in the header of the HTTP request with th

Secure API using JWT

Caroline I'm currently learning how JWTs work and I'm using the API. I've made a middleware function so for every call to the secure route, the middleware is called and the accessToken is parsed. I am passing the token in the header of the HTTP request with th

Secure API using JWT

Caroline I'm currently learning how JWTs work and I'm using the API. I've made a middleware function so for every call to the secure route, the middleware is called and the accessToken is parsed. I am passing the token in the header of the HTTP request with th

Secure API using JWT

Caroline I'm currently learning how JWTs work and I'm using the API. I've made a middleware function so for every call to the secure route, the middleware is called and the accessToken is parsed. I am passing the token in the header of the HTTP request with th

Secure API using JWT

Caroline I'm currently learning how JWTs work and I'm using the API. I've made a middleware function so for every call to the secure route, the middleware is called and the accessToken is parsed. I am passing the token in the header of the HTTP request with th

Authenticate using JWT with RSA on Owin

Marcus Consider the following code in Web API 2 using Owin middleware: public class Startup { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); ConfigureAuthentication(app); app.UseCors(CorsOptio

Web Socket Secure URL Encryption

Michael van Rooijen Is the URL itself also encrypted when used wss://? For example, let's say you have a simple Sinatra web application that accepts web socket connections: class App < Sinatra::Base get "/ws/:api_key/room/:id" do |api_key, id| user = Us

Assign accounts to web socket connections

Largon I'm building a website where you can play a game of cards and I need to make sure the web socket connection is coming from the user. I'm using the express-session, connect-mongo, express and ws packages and I'm having trouble getting the session. Parse

Authenticate SSH connections using SSH keys

P ... I have connected two servers using SSH key exchange. Now I can log in to the remote server without a password. Now, I want to verify that the connection without the password really works. what should I do? I tried the following, where SSH should time out

Authenticate SSH connections using SSH keys

P ... I have connected two servers using SSH key exchange. Now I can log in to the remote server without a password. Now, I want to verify that the connection without the password really works. what do I do? I tried the following, where SSH should time out aft

Authenticate with JWT in Web2py

Rock I'm running a rest api using web2py in a controller, where my jwt and token are valid. In the model, I use a field with a default value, but since auth.user is None, the field is never auto-populated. I tried to initialize it manually, but it doesn't work

Authenticate with JWT in Web2py

Rock I'm running a rest api using web2py in a controller, where my jwt and token are valid. In the model, I use a field with a default value, but since auth.user is None, the field is never auto-populated. I tried to initialize it manually, but it doesn't work

Authenticate with JWT in Web2py

Rock I'm running a rest api using web2py in a controller, where my jwt and token are valid. In the model, I use a field with a default value, but since auth.user is None, the field is never auto-populated. I tried to initialize it manually, but it doesn't work

Authenticate with JWT in Web2py

Rock I'm running a rest api using web2py in a controller, where my jwt and token are valid. In the model, I use a field with a default value, but since auth.user is None, the field is never auto-populated. I tried to initialize it manually, but it doesn't work

Authenticate with JWT in Web2py

Rock I'm running a rest api using web2py in a controller, where my jwt and token are valid. In the model, I use a field with a default value, but since auth.user is None, the field is never auto-populated. I tried to initialize it manually, but it doesn't work

Authenticate with JWT in Web2py

Rock I'm running a rest api using web2py in a controller, where my jwt and token are valid. In the model, I use a field with a default value, but since auth.user is None, the field is never auto-populated. I tried to initialize it manually, but it doesn't work

Multiple socket connections using PHP

Rahul I have established a socket connection using php and it works perfectly in the following cases: code for a single socket, but not for multiple connections, only one connection is accepted at a time error_reporting(E_ALL); // sudo lsof -t -i:10000 /