Overview
The WSO2 Identity Server Express SDK (@asgardeo/express) adds authentication to Express applications. It is built on the WSO2 Identity Server Node SDK and runs the OpenID Connect authorization code flow on the server. It adds sign-in and sign-out routes to your app, keeps the user's session in a cookie, and gives you middleware to protect routes.
Tip
Express apps should use this SDK rather than the Node SDK. Use the Node SDK only for Node.js apps that don't use Express.
Installation¶
Use one of the following options to install the WSO2 Identity Server Express SDK. The SDK reads the session cookie from req.cookies, so also install a cookie parser such as cookie-parser.
The SDK works with Express 4.21.2 or later.
How it works¶
AsgardeoExpressClient.getInstance(config)creates the client.AsgardeoExpressClient.asgardeoExpressAuth()returns a router that adds a sign-in route (/loginby default) and a sign-out route (/logoutby default).- When the user opens the sign-in route, the SDK sets the
ASGARDEO_SESSION_IDcookie and redirects them to WSO2 Identity Server. WSO2 Identity Server redirects them back to the same route, where the SDK gets the tokens, stores them against the session ID, and calls youronSignIncallback. AsgardeoExpressClient.protectRoute()returns middleware that only lets signed-in users through.- In your routes,
req.asgardeoAuthis the client. Pass it the session ID fromreq.cookies.ASGARDEO_SESSION_IDto read the user's information and tokens.
Example¶
Register an application in WSO2 Identity Server as a traditional web application, and add http://localhost:3000/login and http://localhost:3000/logout as authorized redirect URLs.
import express from "express";
import cookieParser from "cookie-parser";
import { AsgardeoExpressClient } from "@asgardeo/express";
const app = express();
app.use(cookieParser());
AsgardeoExpressClient.getInstance({
appURL: "http://localhost:3000",
baseUrl: "https://localhost:9443",
clientId: "<your-app-client-id>",
clientSecret: "<your-app-client-secret>"
});
// Adds the /login and /logout routes.
app.use(
AsgardeoExpressClient.asgardeoExpressAuth(
(res, tokenResponse) => {
res.redirect("/profile");
},
(res) => {
res.redirect("/");
},
(res, error) => {
res.status(500).send(error.message);
}
)
);
// Only lets signed-in users through.
const requireAuth = AsgardeoExpressClient.protectRoute((res, reason) => {
res.redirect("/login");
return true;
});
app.get("/", (req, res) => {
res.send(`<a href="/login">Sign in</a>`);
});
app.get("/profile", requireAuth, async (req, res) => {
const user = await req.asgardeoAuth.getUser(req.cookies.ASGARDEO_SESSION_ID);
res.send(`Hello ${user.username}. <a href="/logout">Sign out</a>`);
});
app.listen(3000);
The sign-in route is also the redirect URL that WSO2 Identity Server sends the authorization code to, and the sign-out route is where WSO2 Identity Server sends the user after sign-out. Both must be authorized redirect URLs of the application.
APIs¶
The SDK provides the following APIs.
AsgardeoExpressClient¶
AsgardeoExpressClient is the entry point of the SDK. Create the client with AsgardeoExpressClient.getInstance(), add its middleware to your app, and use it to read tokens and user information.
AsgardeoExpressClient– Methods for authentication, tokens and user information.- Configuration – The options that
getInstance()accepts. - Session cookie – The cookie the SDK sets and how to change its options.
Middleware¶
asgardeoExpressAuth()– Adds the sign-in and sign-out routes.protectRoute()– Lets only signed-in users reach a route.
Guides¶
- Protecting routes – Make routes available only to signed-in users.
- Accessing protected APIs – Get the user's access token and call APIs with it.
Logging¶
The SDK writes debug logs to the console when the LOG_LEVEL environment variable is set to DEBUG, INFO, WARN or ERROR. Logging is off by default.