Securing Remix Apps with Keycloak
pnzrr
Posted on May 7, 2024
Remix is an open source web framework that makes developing web applications easier.
In this article we'll be using Keycloak to quickly augment an application with user management and SSO. We will demonstrate the integration by securing a page for logged-in users. This quickly provides a jump-off point to more complex integrations.
Phase Two is a Keycloak as a Service provider enabling SaaS builders to accelerate time-to-market with powerful enterprise features like SSO, identity, and user management features. Phase Two enhances Keycloak through a variety of open-source extentions for modern SaaS use cases. Phase Two supports both hosted and on-premise deployment options.
What is Keycloak?
Keycloak has been a leader in the Identity and Access Management world since its launch almost 8 years ago. It is an open-source offering under the stewardship of Red Hat
INFO
If you just want to skip to the code, visit the Phase Two Remix example. We also have a plain React example.
TOC
Setting up a Keycloak Instance
TIP
If you already have a functioning Keycloak instance, you can skip to the next section.
At this point, move on to the next step in the tutorial. We'll be coming back to the Admin Console when its time to start connecting our App to the Keycloak instance.Keycloak Setup Details
Rather than trying to set up a "from scratch" instance of Keycloak, we're going to short-circuit that process by leveraging a Phase Two free Keycloak starter instance. The Starter provides a free hosted instance of Phase Two's enhanced Keycloak ready for light production use cases.
Setting up an OIDC Client
We need to create a OpenID Connect Client in Keycloak for the app to communicate with.
Keycloak's docs provide steps for how to create an OIDC client and all the various configurations that can be introduced. Follow the steps below to create a client and get the right information necessary for app configuration. Under Login settings we need to add a redirect URI and Web origin in order. Assuming you are using the example application: Valid redirect URI (allows redirect back to application) Web origins (allows for Token auth call)Details
URI and Origin Details
The choice of localhost
is arbitrary. If you are using an example application running locally, this will apply. If you are using an app that you actually have deployed somewhere, then you will need to substitute the appropriate URI for that.
http://localhost:3000/*
http://localhost:3000
OIDC Config
We will need values to configure our application. To get these values follow the instructions below.Details
Adding a Non-Admin User
INFO
It is bad practice to use your Admin user to sign in to an Application.
Since we do not want to use our Admin user for signing into the app we will build, we need to add another non-admin user.
Details
Setting up a Remix Project
INFO
We will use the Phase Two Remix example code here, but the logic could easily be applied to any existing application.
- Clone the Phase Two example repo.
- Open the Remix folder within
/frameworks/remix
. - Run
npm install
and thennpm run dev -- --port 3000
. This example leverages remix-auth and remix-keycloak to provide HOC support. - Open the
app/services/keycloak.server.ts
file. This is a server only file. We will be updating a few values from the prior section where we set up our OIDC client. Taking the values from the OIDC Client Config section, set those values in the code. While it is recommended to use Environment variables for the secret, for the purpose of this tutorial, paste in the Client secret from the OIDC client creation section for the value ofclientSecret
:
const kcConfig = {
useSSL: true,
domain: "usw2.auth.ac/auth",
realm: "shared-deployment-001",
clientID: "reg-example-1",
clientSecret: "CLIENT_SECRET", // Paste "Client secret" here. Use Environment variables in prod
callbackURL: "http://localhost:3000/auth/keycloak/callback",
};
Those are used to popluate the config for the KeycloakStrategy
:
export default new KeycloakStrategy(kcConfig, ({ profile }) => profile);
The config is then used with the Authenticator instance in the app/services/auth.server.ts
file. The authenticator instance uses the Session Storage to manage the state of authentication via a cookie.
import { Authenticator } from "remix-auth";
import keycloakServer from "./keycloak.server";
import { sessionStorage } from "~/services/session.server";
export const authenticator = new Authenticator(sessionStorage);
authenticator.use(keycloakServer);
At this point our entire application will be able to access all information and methods needed to perform authentication. View the session.server.ts
file for additional information about how the SessionStorage
is used. The SessionStorage
stores the Keycloak token and is used to derive the authenticated state. View user.tsx
for exactly how the code is authenticating your user. The sections rendering the Log in and Log out buttons are conditional areas based on the authenticated context. The buttons invoke server-side APIs provided by remix-auth
.
The logic using the authenticator to conditionally determine the Authenticated state, can be used to secure routes, components, and more.
- Open localhost:3000. You will see the Phase Two example landing page. You current state should be Not authenticated. Click Log In. This will redirect you to your login page.
INFO
Use the non-admin user created in the previous section to sign in.
- Enter the credentials of the non-admin user you created. Click Submit. You will then be redirected to the application. The Phase Two example landing page now loads your Authenticated state, displaying your user's email and name.
- Neat! If you clear the browser state for that tab, then you will have to be redirected away to sign-in again.
Learning more
Phase Two's enhanced Keycloak provides many ways to quickly control and tweak the log in and user management experience. Our blog has many use cases from customizing login pages, setting up magic links (password-less sign in), and Organization workflows.
Posted on May 7, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.