KeyCloak-Nodejs - enters an endless loop after returning the authorization code and status

I am establishing an Open Id connection for a dummy node application using KeyCloak . I am using the nodejs adapter suggested here in key cloak docs .

Here is the node.'s routes.js file:

'use strict'; /** * Module dependencies. */ const home = require('../app/controllers/home'); /** * Expose */ module.exports = function (app, passport) { var session = require('express-session'); var Keycloak = require('keycloak-connect'); var memoryStore = new session.MemoryStore(); var keycloak = new Keycloak({ store: memoryStore }) // app.use(session({ // secret: 'mySecret', // resave: false, // saveUninitialized: true, // store: memoryStore // })); app.use( keycloak.middleware() ); // console.log("In Routes.js. Compare with post auth message"); app.get('/', keycloak.protect(), home.index); // app.get('/redirect', keycloak.protect(), home.index); // app.get('/venky', keycloak.protect(), function(request, response) { // response.send("Hello World"); // }); app.get('/redirecta', home.index); /** * Error handling */ app.use(function (err, req, res, next) { // treat as 404 if (err.message && (~err.message.indexOf('not found') || (~err.message.indexOf('Cast to ObjectId failed')))) { return next(); } console.error(err.stack); // error page res.status(500).render('500', { error: err.stack }); }); // assume 404 since no middleware responded app.use(function (req, res, next) { res.status(404).render('404', { url: req.originalUrl, error: 'Not found' }); }); }; 

When I access the root url of localhost:3000 , I first redirect to the KeyCloak login KeyCloak . After logging in, I get the following error: enter image description here

Here is the node app server log: enter image description here

As you can see, it goes into an endless cycle.

Additional Information I found:

  • In the first redirection after logging in, the state value matches the value before logging in. As can be seen from the above screenshot of the server log, the state value changes with each redirection after that.

  • From what I have found out so far, the redirect occurs in post-auth.js (line number 49) , resulting in an infinite loop. Here is a link to post-auth.js - https://github.com/keycloak/keycloak-nodejs-connect/blob/master/middleware/post-auth.js

  • sessionId The getGrantFromCode function in index.js is undefined. Here is the function:

 Keycloak.prototype.getGrantFromCode = function (code, request, response) { if (this.stores.length < 2) { // bearer-only, cannot do this; throw new Error('Cannot exchange code for grant in bearer-only mode'); } var sessionId = request.session.id; // console.log(sessionId, "BBB BEGIN NNNN - in index.js getGrantFromCode"); // console.log(request, "sessionId from getGrantFromCode function"); // console.log("EEE END DDD - in index.js getGrantFromCode"); var self = this; return this.grantManager.obtainFromCode(request, code, sessionId) .then(function (grant) { self.storeGrant(grant, request, response); // console.log(grant, "iS this the grant from getGrantFromCode?"); // console.log("Inside the return function of getGrantFromCode"); return grant; }); }; 

Source Code Link: https://github.com/keycloak/keycloak-nodejs-connect/blob/master/index.js

  1. Below is an example that I get when I register it with the provision function mentioned above: https://pastebin.com/eqUaiAvb

Questions:

  • How can I fix this problem?
  • Where can I find keycloak (jboss) logs? Now I can only see keycloak logs only in case of error. I am using mvn -f testsuite/integration/pom.xml exec:java -Pkeycloak-server to run keycloak. I have never worked on java before.
  • Where is the request (using the authorization code) for the id token?

Thank you very much in advance.

+8
express openid-connect keycloak
source share

No one has answered this question yet.

See related questions:

4
node.js and express: how to wait for udp response
2
Keycloak redirecting with OIDC URL parameters gives an invalid request
2
Socket IO Connection to ws: // someAddress was interrupted during page load
one
do not get the expected result when using a passport in nodejs for authentication
one
SequelizeConnectionRefusedError json mysql
one
Node emitters with express
0
Why can't I update the client session of the node inside the async waterfall function?
0
Undefined _id is populated with an expression, moongose ​​/ MongoDB on Nodejs
0
Why does this error occur when starting nodejs server?
-2
js express routing node [Unable to GET: / collection #]

All Articles