Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
express-jwt-fusionauth
Advanced tools
Express middleware for JWT-based authentication against FusionAuth
Express middleware for JSON Web Token (JWT)-based authentication against FusionAuth. It provides these main functions:
While most of the mechanics of JWT (RFC 7519) and OAuth 2.0 (RFC 6749) are standard across identity providers, this implementation focuses on specifics of FusionAuth and its best practices to make integration as safe and simple as possible. For example, the TypeScript definition of the JWT claims interface contains only the subset of registered claims used by FusionAuth, as well as the private claims it adds.
npm install express-jwt-fusionauth
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import express from 'express';
import { ExpressJwtFusionAuth } from 'express-jwt-fusionauth';
// environment-specific settings and secrets
const { FUSIONAUTH_URL = 'http://fusionauth:9011' } = process.env;
const { JWT_ISSUER = 'acme.com' } = process.env;
const { OAUTH_CLIENT_ID = '31d7b8e8-f67e-4fb0-9c0b-872b793cda7a' } = process.env;
const { OAUTH_CLIENT_SECRET = 'VYKsyjndsJ7lTnS2Z5vuz4SM-8Dvy1-4_yvqEoALMfY' } = process.env;
const { OAUTH_REDIRECT_URI = 'http://localhost:3000/oauth' } = process.env;
const { OAUTH_COOKIE_DOMAIN = 'localhost' } = process.env;
const oauthConfig = {
clientId: OAUTH_CLIENT_ID,
clientSecret: OAUTH_CLIENT_SECRET,
redirectUri: OAUTH_REDIRECT_URI,
cookieConfig: {
domain: OAUTH_COOKIE_DOMAIN
}
};
const jwtOptions = {
oauthConfig,
required: true,
alwaysLogin: false,
browserLogin: true,
verifyOptions: {
issuer: JWT_ISSUER,
audience: OAUTH_CLIENT_ID
}
};
// create the middleware/handler factory
const auth = new ExpressJwtFusionAuth(FUSIONAUTH_URL);
const app = express();
// add the cookie-parser middleware to extract JWTs from the access_token cookie
app.use(cookieParser());
// add a route corresponding to the OAuth redirect URI,
// used to exchange an authorization code for a JWT/access token
app.get('/oauth', auth.oauthCompletion(oauthConfig));
app.post('/oauth', bodyParser.urlencoded({ extended: true }), auth.oauthCompletion(oauthConfig));
// sample route requiring JWT authentication and the "root" or "admin" application role;
// for demonstration purposes, it just dumps the JWT claims as JSON
app.get('/authed',
auth.jwt(jwtOptions),
auth.jwtRole(['root', 'admin']),
(req: express.Request, res) => res.json(req.jwt!));
// sample route with optional JWT authentication
app.get('/opt-authed',
auth.jwt({ ...jwtOptions, required: false }),
(req: express.Request, res) => res.send(req.jwt ? req.jwt.email : 'nobody'));
app.listen();
Provides factory methods for Express middleware/handlers used to obtain and validate JSON Web Tokens (JWTs).
Kind: global class
Creates a middleware factory that communicates with FusionAuth at the given URL.
Param | Type | Description |
---|---|---|
fusionAuthUrl | string | the base URL of the FusionAuth application (e.g. |
Returns a middleware/handler that checks whether a request has a JWT attached, validates the JWT, and associates the JWT contents with the request object. By default, if the client appears to be a web browser, it will be redirected to the FusionAuth OAuth 2.0 login URL. However, this behavior can be enabled or disabled for all clients.
Kind: instance method of ExpressJwtFusionAuth
Param | Type | Description |
---|---|---|
options | JwtOptions | the JWT acquisition and verification options |
Requests and parses/validates a new JWT/access token using a refresh token
obtained from a prior OAuth login or JWT refresh.
Note that the middleware/handler returned by jwt()
will do this automatically
for an expired access token if a refresh token is available.
This function is provided for cases where an application needs to refresh explicitly,
such as when exchanging a FusionAuth JWT for an application-generated JWT.
Kind: instance method of ExpressJwtFusionAuth
Param | Description |
---|---|
refreshToken | the refresh token from the prior login or refresh |
oldToken | the original, expired access token (for JWT Refresh webhook event) |
context | the refresh context, optionally containing JWT transform options and Express request/response |
Returns a middleware/handler that checks whether a request has a valid JWT
attached that has at least one of the given application roles.
The request must have already had the JWT parsed and validated by the
ExpressJwtFusionAuth.jwt
middleware. If the JWT is not present or does
not have one of the required roles, the request is failed with HTTP 403 Forbidden.
Kind: instance method of ExpressJwtFusionAuth
Param | Type | Description |
---|---|---|
roleOrRoles | string | the role or roles to check for |
Returns a handler for the OAuth 2.0 redirection endpoint that exchanges an authorization code for a JWT/access token and optional refresh token.
Kind: instance method of ExpressJwtFusionAuth
Param | Type | Description |
---|---|---|
config | OAuthConfig | the OAuth 2.0 configuration settings |
express-jwt-fusionauth
is available under the ISC license.
FAQs
Express middleware for JWT-based authentication against FusionAuth
The npm package express-jwt-fusionauth receives a total of 23 weekly downloads. As such, express-jwt-fusionauth popularity was classified as not popular.
We found that express-jwt-fusionauth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.