npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auth0-socketio

v1.1.1

Published

A near-zero config middleware to use Auth0 JWT on your Socket.IO server

Downloads

80

Readme

auth0-socketio

A near-zero config middleware to use Auth0 JWT on your Socket.IO server. You only need your Auth0 domain and optionally an audience.

It is build around ease of use, when you just need a single functionality.

  • The factory will try to find the config automatically.
  • The errors are as detailed as possible to guide you to the solution.
  • And it only imports two dependencies!

Disclaimer: This is an open source project, not affiliated in any way with Auth0. Under GPL license. Use it at your own risk.

Config

Install with npm i auth0-socketio or visit the NPM package webpage

Usage

You need your Auth0 domain in most cases something like example-co.au.auth0.com and optionally an audience

Server side

Import auth0Middleware from auth0-socketio and create your middleware with you domain (and optionally audience). Then apply it to your server as a regular socket.io middleware.

import { Server } from 'socket.io';
import auth0Middleware from 'auth0-socketio';

const io = new Server();

const withAuthorization = auth0Middleware('example-co.au.auth0.com');

io.use(withAuthorization);

Client side

Follow Socket.IO documentation to include your JWT on the authentication handshake:

// plain object
const socket = io({
	auth: {
		token: 'abc'
	}
});

// or with a function
const socket = io({
	auth: (cb) => {
		cb({
			token: 'abc'
		});
	}
});

You can then access the token from the server side, that is what auth0-socketio exactly does.

It will validate the token against your domain and (optionally audience) returning an Error if the token is not valid or failed to be verified.

Validate Audience

To validate claims against an audience use it as second parameter:

import { Server } from 'socket.io';
import auth0Middleware from 'auth0-socketio';

const io = new Server();

const withAuthorization = auth0Middleware('example-co.au.auth0.com','example-audience);

io.use(withAuthorization);

Environment variables

It is usual to have your Auth0 domain and audience as environment variables used by other parts of your application. If no parameters are provided auth0-socketio will try to use AUTH0_DOMAIN and AUTH0_AUDIENCE from the environment or an .env file.

Missing

There are no test at the moment.

Alternatives

There a couple of alternatives that I tried before implementing my own, maybe they fit better your purpose:

  • socketio-jwt Possibly the closest to the original implementation, still active.
  • socketio-jwt-auth Similar to the previous one, a very good implementation.

However, I found both a bit cumbersome to use. You need to configure your secret and algorithm, for instance using jwksClient from jwks-rsa and your JWKS, and I wanted as little config as possible.

Also I personally didn't trust the JWT implementation they use. The former uses jsonwebtoken, while the latter uses jwt-simple both haven't been updated in years.

In addition neither of the packages implement claim verification, so you have to do the extra work on a very delicate area.

It is worth mentioning socketio-jwt the original implementation from Auth0 community, no longer maintained but still good as a reference.