This is a simply Node.js REST application with access control via keycloak.protect() and keycloak.enforcer() provide in ('keycloak-connect').
"dependencies": {
"keycloak-connect": "7.0.1"
} This application has endpoints protected based on Role of user on Keycloak.
| URL | Method | Roles |
|---|---|---|
| user | GET | not-specify |
| roles/admin | GET | admin |
| roles/advertiser | GET | customer-advertiser |
| roles/analyst | GET | customer-analyst |
| roles/adminOrAdvertiser | GET | admin, customer-advertiser |
This applications also has REST API to work with customers, campaigns and reports. We will protect all endpoints based on permissions are configured using Keycloak.
| URL | Method | Permission | Resource | Scope | Roles |
|---|---|---|---|---|---|
| /customers | POST | customer-create | customer | create | admin |
| /customers | GET | customer-view | customer | view | admin, customer-advertiser, customer-analyst |
| /campaigns | POST | campaign-create | campaign | create | admin, customer-advertiser |
| /campaigns | GET | campaign-view | campaign | view | admin, customer-advertiser, customer-analyst |
| /reports | POST | report-create | report | create | customer-analyst |
| /reports | GET | report-view | report | view | admin, customer-advertiser, customer-analyst |
Note: this configure has removed res: and scope: prefix of resource name and scope name from original design at https://github.com/v-ladynev/keycloak-nodejs-example
The application will use a combination of (resource, scope) to check a permission. We will configure Keycloak to use polices are based on roles. But for the application a combination of (resource, scope) is important only. We can configure Keycloak using something other than roles, without changing the application.
You can run already configured Keycloak, using Docker and this instruction:
Download the last version of Keycloak (this example uses 7.0.1) http://www.keycloak.org/downloads.html
Realm, Client and Polices configuration can be imported using this file: CAMPAIGN_REALM-realm.json
Users can be imported from this file: CAMPAIGN_REALM-users-0.json
You will need to select a file on the Add Realm page to import a realm .
https://www.keycloak.org/docs/latest/server_admin/index.html#_create-realm
Users can be imported via Manage -> Import
Export and import is triggered at server boot time and its parameters are passed in via Java system properties. https://www.keycloak.org/docs/latest/server_admin/index.html#_export_import
-
Run server using standalone.sh (standalone.bat)
-
You should now have the Keycloak server up and running. To check that it's working open http://localhost:8080. You will need to create a Keycloak admin user. Then click on
Admin Consolehttps://www.keycloak.org/docs/latest/server_admin/index.html#admin-console
When you define your initial admin account, you are creating an account in the master realm. Your initial login to the admin console will also be through the master realm. https://www.keycloak.org/docs/latest/server_admin/index.html#the-master-realm
-
Create a
CAMPAIGN_REALMrealm https://www.keycloak.org/docs/latest/server_admin/index.html#_create-realm -
Create realm roles:
admin,customer-advertiser,customer-analysthttps://www.keycloak.org/docs/latest/server_admin/index.html#realm-roles
Noitice: Each client can has their own "client roles", scoped only to the client https://www.keycloak.org/docs/latest/server_admin/index.html#client-roles -
Create users (don't forget to disable
Temporarypassword) https://www.keycloak.org/docs/latest/server_admin/index.html#_create-new-user
- login:
admin_user, password:admin_user - login:
advertiser_user, password:advertiser_user - login:
analyst_user, password:analyst_user
- Add roles to users:
admin_user—adminadvertiser_user—customer-advertiseranalyst_user—customer-analysthttps://www.keycloak.org/docs/latest/server_admin/index.html#user-role-mappings
- Create a
CAMPAIGN_CLIENThttps://www.keycloak.org/docs/latest/server_admin/index.html#oidc-clients
- Client ID:
CAMPAIGN_CLIENT - Client Protocol:
openid-connect - Access Type:
Confidential - Standard Flow Enabled:
ON - Implicit Flow Enabled:
OFF - Direct Access Grants Enabled:
ONImportant: it should beONfor the custom login (to provide login/password via an application login page) - Service Accounts Enabled:
ON - Authorization Enabled:
ONImportant: to add polices - Valid Redirect URIs:
http://localhost:3000/*. Keycloak will use this value to check redirect URL at least for logout. It can be just a wildcard*. - Web Origins:
*
Using Authorization -> Policies add role based polices
https://www.keycloak.org/docs/latest/authorization_services/index.html#_policy_rbac
| Policy | Role |
|---|---|
| Admin | admin |
| Advertiser | customer-advertiser |
| Analyst | customer-analyst |
| Admin or Advertiser or Analyst | Aggregated Policy* |
Aggregated Policy* This policy consist of an aggregation of other polices https://www.keycloak.org/docs/latest/authorization_services/index.html#_policy_aggregated
- Polycy name:
Admin or Advertiser or Analyst - Apply Policy:
Admin,Advertiser,Analyst - Decision Strategy:
Affirmative
Using Authorization -> Authorization Scopes add scopes
- create
- view
Using Authorization -> Resources add resources. Scopes should be entered in the Scopes field for every resource.
| Resource Name | Scopes |
|---|---|
| campaign | create, view |
| customer | create, view |
| report | create, view |
Using Authorization -> Permissions add scope-based permissions
https://www.keycloak.org/docs/latest/authorization_services/index.html#_permission_create_scope
Set decision strategy for every permission
- Decision Strategy:
Affirmative
| Permission | Resource | Scope | Polices |
|---|---|---|---|
| customer-create | customer | create | Admin |
| customer-view | customer | view | Admin or Advertiser or Analyst |
| campaign-create | campaign | create | Admin, Advertiser |
| campaign-view | campaign | view | Admin or Advertiser or Analyst |
| report-create | report | create | Analyst |
| report-view | report | view | Admin or Advertiser or Analyst |
- Download
keycloak.jsonusingCAMPAIGN_CLIENT -> Installation: https://www.keycloak.org/docs/latest/securing_apps/index.html#_nodejs_adapter
-
Clone this project https://github.com/piruin/keycloak-nodejs-example.git
-
Replace
keycloak.jsonin the root of this project with downloadedkeycloak.json. -
Run
npm installin the project directory to install Node.js libraries -
npm startto run node.js application -
Access this application at http://localhost:3000 on web browser or use prepared Postman collection
-
import Postman Collection and then
-
import Postman Environment and configure following variable
auth_urlbase url of your KeyCloak server, ex. http://localhost:8080client_secretKeyCloak generatedsecretatClients -> CAMPAIGN_CLIENT -> Credentials
-
Use one of request in
Loginfolder ofCAMPAIGN_REALMcollection -
Try request in both
Role-BaseandResource-Baseand see how KeyCloak's magic work!
This folked example project focus only about KeyCloak Node.Js Adapter ('keycloak-connect') and how it corresponding with Role, Resource and Scope on KeyCloak. Therefor, Some original content was removed. Such as,
See https://github.com/v-ladynev/keycloak-nodejs-example for full information