I have to integrate node.js application with keycloak.The application is in express.But the policies are not enforcing.It grants permission for all the users to access all the api. For /test api: Only users with 'chief' role has the access.I have given those policies in keycloak admin console.But those are not reflecting.Why?
User without 'chief' role is also accessing /test
app.js:
'use strict';
const Keycloak = require('keycloak-connect');
const express = require('express');
const session = require('express-session');
const expressHbs = require('express-handlebars');
const app = express();
app.engine('hbs', expressHbs({extname:'hbs',
defaultLayout:'layout.hbs',
relativeTo: __dirname}));
app.set('view engine', 'hbs');
var memoryStore = new session.MemoryStore();
var keycloak = new Keycloak({ store: memoryStore });
app.use(session({
secret:'thisShouldBeLongAndSecret',
resave: false,
saveUninitialized: true,
store: memoryStore
}));
app.use(keycloak.middleware());
app.get('/*', keycloak.protect('user'), function(req, res){
res.send("User has base permission");
});
app.get('/test', keycloak.protect(), function(req, res){
res.send("access granted");
});
app.get('/',function(req,res){
res.send("hello world");
});
app.use( keycloak.middleware( { logout: '/'} ));
app.listen(3000, function () {
console.log('Listening at http://localhost:3000');
});
keycloak.json:
{
"realm": "nodejs-example",
"auth-server-url": "http://localhost:8180/auth",
"ssl-required": "external",
"resource": "nodejs-connect",
"credentials": {
"secret": "451317a2-09a1-48b8-b036-e578051687dd"
},
"use-resource-role-mappings": true,
"confidential-port": 0,
"policy-enforcer": {
"enforcement-mode":"PERMISSIVE",
}
}
You have enforcement-mode in your keycloak.json set to PERMISSIVE, this should be ENCFORCE I believe.