I had a problem using middleware connect-keycloak with NodeJS, and there seemed to be very few online documents for people who used it. This is based on the โComplete Exampleโ from white papers found here: http://keycloak.imtqy.com/keycloak-nodejs/connect/
I get an unexpected error while testing with curl related to undefined 'keyclay-token'. I cannot find references to this in my code or source, nor to anyone else with the same problem on the Internet. Can anyone see what I'm doing wrong?
The connect-keyclloak object is included and created as expected:
// app.js: // module dependencies var request = require('sync-request'); var fs = require('fs'); var restify = require('restify'); var Keycloak = require('connect-keycloak'); var session = require('express-session'); var memoryStore = new session.MemoryStore(); // Keycloak var keycloak = new Keycloak({ store: memoryStore });
And middleware is used:
var server = restify.createServer({ name: 'name', version: '1.0.0' }); server.use(restify.acceptParser(server.acceptable)); server.use(restify.queryParser()); server.use(restify.bodyParser()); server.use(keycloak.middleware({ logout: '/logout', admin: '/' })); server.use(session({ secret: 'secret', resave: false, saveUninitialized: true, store: memoryStore }));
And the keycloak.protect method is used:
server.get(/.*/, keycloak.protect(), restify.serveStatic({ 'directory': './html', 'default': 'index.html' }));
However, this twisting test:
curl -H "Content-Type: application/json" -X POST -d '{"query":"test"}' http://localhost:3000/trust-me-on-the-url-being-correct/thanks -i
Throws this unusual error (and not the error I was hoping for):
POST -d '{"query":"car"}' http://localhost:3000/rest/keywords -i HTTP/1.1 500 Internal Server Error Content-Type: application/json Content-Length: 87 Date: Thu, 03 Dec 2015 02:05:40 GMT Connection: keep-alive {"code":"InternalError","message":"Cannot read property 'keycloak-token' of undefined"}[ addamnilemartin@localhost keyword]$
Keycloak.json is included in the same directory as app.js, and should definitely not be the cause of the problem.
Any ideas? Thanks.
UPDATE:
I realized this was missing and added:
// set session for keycloak server.use(session({ secret: 'fsd78d7gdfgds', resave: false, saveUninitialized: true, store: memoryStore }));
Now the answer, when my POST had keycloak.protect (), failed:
curl -H "Content-Type: application/json" -X POST -d '{"query":"car"}' http://localhost:3000/blah/blah -i curl: (52) Empty reply from server
Without keycloak.protect, the answer is the expected JSON, of course, since there is no attempt to auth.
Any thoughts? Thanks again.