DEV Community

pnzrr for Phase Two

Posted on • Updated on • Originally published at phasetwo.io

Securing a Next.js Application with Keycloak

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 Next.js 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.

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.
  • Visit the sign-up page.
  • Enter an email, use a Github account, or use an existing Google account to register.

Register

  • Follow the register steps. This will include a sign-in link being sent to your email. Use that for password-less login.

Email Link

  • After creating an account, a realm is automatically created for you with all of the Phase Two enhancements. You need to create a Deployment in the Shared Phase Two infrastructure in order to gain access to the realm. Without a deployment created, the Create Shared Deployment modal will automatically pop up.
  • Create a Shared Deployment by providing a region (pick something close to your existing infrastructure), a name for the deployment, and selecting the default organization that was created for you upon account creation. Hit "Confirm" when ready. Standby while our robots get to work generating your deployment. This can take a few seconds.

Create shared deployment

  • After the deployment is created and active, you can access the Keycloak Admin console by clicking "Open Console" for that deployment. Open it now to see the console.

Deployments

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.


Setting up an OIDC Client

We need to create a OpenID Connect Client in Keycloak for the app to communicate with.

Details

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.

  1. Open the Admin UI by clicking Open Console in the Phase Two Dashboard.
  2. Click Clients in the menu.
  3. Click Create client.
  4. Leave Client type set to OpenID Connect.
  5. Enter a Client ID. This ID is an alphanumeric string that is used in OIDC requests and in the Keycloak database to identify the client.
  6. Supply a Name for the client.
  7. Click Next. General settings
  8. Under the Capability Config section, leave the defaults as selected. This can be configured further later.
  9. Client authentication to On.
  10. Authorization to Off.
  11. Standard flow checked. Direct access grants checked. All other items unchecked.
  12. Click Next. Capbility config
  13. Under Login settings we need to add a redirect URI and Web origin in order. Assuming you are using the example application:
    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.

    Valid redirect URI (allows redirect back to application)

    http://localhost:3000/*
    

    Web origins (allows for Token auth call)

    http://localhost:3000
    
  14. Click Save

    Login settings




OIDC Config

Details
We will need values to configure our application. To get these values follow the instructions below.
  1. Click Clients in the menu.
  2. Find the Client you just created and click on it. In the top right click the Action dropdown and select Download adapter config.
  3. Select Keycloak OIDC JSON in the format option. The details section will populate with the details we will need.
    • Note the realm, auth-server-url, and resource values. Adapter config
  4. You also need to copy the Client secret in the Credential tab for the client to use. Once on the Credential tab, click the copy button to copy the key to your clipboard. Save the key somewhere for use later in this tutorial Copy Credential

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
  1. Open the Admin UI by clicking Open Console in the Phase Two Dashboard.
  2. Click Users in the menu.
  3. Click Add user.
  4. Fill out the information for Email, First name, and Last name. Click Create.
  5. We will now set the password for this user manually. Click Credentials (tab) and click Set Password. Provide a password for this user. For our use case, as a tutorial, you can leave "Temporary" set to "Off".
  6. Click Save and confirm the password by clicking Save password


Setting up a Next.js Project

INFO
We will use the Phase Two Next.js example code here, but the logic could easily be applied to any existing application.

This example uses Next.js 13 and splits server and client components accordingly.

  1. Clone the Phase Two example repo.
  2. Open the Next.js folder within /frameworks/nextjs.
  3. Run npm install and then npm run dev. This example leverages NextAuth.js to provide hook and HOC support.
  4. NextAuth.js configures an API route that is uses for the Authentication of the Client. It generates the routes automatically for you. These are added to Next.js in the api/auth/[...nextauth]/route.ts file.
  5. Open the src/lib/auth.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 of clientSecret
const authServerUrl = "https://euc1.auth.ac/auth/";
const realm = "shared-deployment-001";
const clientId = "reg-example-1";
const clientSecret = "CLIENT_SECRET"; // Paste "Client secret" here. Use Environment variables in prod
Enter fullscreen mode Exit fullscreen mode

Those are used to popluate the AuthOptions config for the KeycloakProvider:

export const AuthOptions: NextAuthOptions = {
  providers: [
    KeycloakProvider({
      clientId,
      clientSecret,
      issuer: `${authServerUrl}realms/${realm}`,
    }),
  ],
};
Enter fullscreen mode Exit fullscreen mode

The config is then provided to the AuthProvider in the /src/app/layout.tsx file. Next.js uses this file to generate an HTML view for this page.

import { NextAuthProvider as AuthProvider } from "./providers";
...
<AuthProvider {...oidcConfig}>
  <App />
</AuthProvider>
Enter fullscreen mode Exit fullscreen mode

At this point, our entire application will be able to access all information and methods needed to perform authentication. View the providers.tsx file for additional information about how the SessionProvider is used. The SessionProvider enables use of Hooks to derive the authenticated state. View user.component.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 functions provided by NextAuth.

The logic using the hook to conditionally determine the Authenticated state, can be used to secure routes, components, and more.

  1. 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.

  1. 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 their Token.
  2. After your first log in, click Log out. Then click Log in again. Notice how this time you will not be redirected to sign in as your state is already in the browser. 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.

Top comments (0)