This project demonstrates how to write a RESTful service with Node.js that is secured with Keycloak.
There are 3 endpoints exposed by the service:
public
- requires no authenticationsecured
- can be invoked by users with theuser
roleadmin
- can be invoked by users with theadmin
role
The endpoints are very simple and will only return a simple message stating what endpoint was invoked.
Before you start: Make sure you have Docker installed.
From a terminal start Keycloak with the following command:
docker run -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:18.0.2 start-dev
This will start Keycloak exposed on the local port 8080. It will also create an initial admin user with username admin and password admin.
Go to the Keycloak Admin Console and login with the username and password you created earlier.
Prior to running the project you need to create a realm and configure a client. For that: https://www.keycloak.org/getting-started/getting-started-docker
-
Open a terminal and navigate to the root directory.
-
The following shows the command to run the project:
npm install npm start
- public - http://localhost:3000/service/public
- secured - http://localhost:3000/service/secured
- admin - http://localhost:3000/service/admin
You can open the public endpoint directly in the browser to test the service. The two other endpoints require invoking with a bearer token.