Dynamically protecting url using node.js Keycloak adapter











up vote
0
down vote

favorite












The code from here works for the basic case when urls are known in advance.
How to handle dynamic protection of urls by Keycloak after middleware was added and express app started?
I was thinking about handling reading new urls from file by some node.js module which will emit the event and then the code below would handle the event. In the code of event handler, the call to app.all('/new url', keycloak.protect()) will be added.
I tried that but it doesn't work as expected because of the app.use('/lap', [some_midleware]) is before the new app.all('/new url', keycloak.protect()
The only way i think of is modifying app._router.stack by inserting the new middleware before the some_midleware



var Keycloak = require('keycloak-connect');
var hogan = require('hogan-express');
var express = require('express');
var session = require('express-session');
var fs = require()
var app = express();

var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});



var memoryStore = new session.MemoryStore();

app.use(session({
secret: 'mySecret',
resave: false,
saveUninitialized: true,
store: memoryStore
}));


var keycloak = new Keycloak({
store: memoryStore
});


app.use(keycloak.middleware({
logout: '/logout',
admin: '/',
protected: '/protected/resource'
}));

app.all('/url', keycloak.protect())

app.all('*', [some_midleware])









share|improve this question




























    up vote
    0
    down vote

    favorite












    The code from here works for the basic case when urls are known in advance.
    How to handle dynamic protection of urls by Keycloak after middleware was added and express app started?
    I was thinking about handling reading new urls from file by some node.js module which will emit the event and then the code below would handle the event. In the code of event handler, the call to app.all('/new url', keycloak.protect()) will be added.
    I tried that but it doesn't work as expected because of the app.use('/lap', [some_midleware]) is before the new app.all('/new url', keycloak.protect()
    The only way i think of is modifying app._router.stack by inserting the new middleware before the some_midleware



    var Keycloak = require('keycloak-connect');
    var hogan = require('hogan-express');
    var express = require('express');
    var session = require('express-session');
    var fs = require()
    var app = express();

    var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);
    });



    var memoryStore = new session.MemoryStore();

    app.use(session({
    secret: 'mySecret',
    resave: false,
    saveUninitialized: true,
    store: memoryStore
    }));


    var keycloak = new Keycloak({
    store: memoryStore
    });


    app.use(keycloak.middleware({
    logout: '/logout',
    admin: '/',
    protected: '/protected/resource'
    }));

    app.all('/url', keycloak.protect())

    app.all('*', [some_midleware])









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The code from here works for the basic case when urls are known in advance.
      How to handle dynamic protection of urls by Keycloak after middleware was added and express app started?
      I was thinking about handling reading new urls from file by some node.js module which will emit the event and then the code below would handle the event. In the code of event handler, the call to app.all('/new url', keycloak.protect()) will be added.
      I tried that but it doesn't work as expected because of the app.use('/lap', [some_midleware]) is before the new app.all('/new url', keycloak.protect()
      The only way i think of is modifying app._router.stack by inserting the new middleware before the some_midleware



      var Keycloak = require('keycloak-connect');
      var hogan = require('hogan-express');
      var express = require('express');
      var session = require('express-session');
      var fs = require()
      var app = express();

      var server = app.listen(3000, function () {
      var host = server.address().address;
      var port = server.address().port;
      console.log('Example app listening at http://%s:%s', host, port);
      });



      var memoryStore = new session.MemoryStore();

      app.use(session({
      secret: 'mySecret',
      resave: false,
      saveUninitialized: true,
      store: memoryStore
      }));


      var keycloak = new Keycloak({
      store: memoryStore
      });


      app.use(keycloak.middleware({
      logout: '/logout',
      admin: '/',
      protected: '/protected/resource'
      }));

      app.all('/url', keycloak.protect())

      app.all('*', [some_midleware])









      share|improve this question















      The code from here works for the basic case when urls are known in advance.
      How to handle dynamic protection of urls by Keycloak after middleware was added and express app started?
      I was thinking about handling reading new urls from file by some node.js module which will emit the event and then the code below would handle the event. In the code of event handler, the call to app.all('/new url', keycloak.protect()) will be added.
      I tried that but it doesn't work as expected because of the app.use('/lap', [some_midleware]) is before the new app.all('/new url', keycloak.protect()
      The only way i think of is modifying app._router.stack by inserting the new middleware before the some_midleware



      var Keycloak = require('keycloak-connect');
      var hogan = require('hogan-express');
      var express = require('express');
      var session = require('express-session');
      var fs = require()
      var app = express();

      var server = app.listen(3000, function () {
      var host = server.address().address;
      var port = server.address().port;
      console.log('Example app listening at http://%s:%s', host, port);
      });



      var memoryStore = new session.MemoryStore();

      app.use(session({
      secret: 'mySecret',
      resave: false,
      saveUninitialized: true,
      store: memoryStore
      }));


      var keycloak = new Keycloak({
      store: memoryStore
      });


      app.use(keycloak.middleware({
      logout: '/logout',
      admin: '/',
      protected: '/protected/resource'
      }));

      app.all('/url', keycloak.protect())

      app.all('*', [some_midleware])






      node.js express middleware keycloak






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 7:52

























      asked Nov 19 at 18:34









      user1264304

      95562252




      95562252
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          From what I understand, you want to dynamically add new routes that get handled before the last route in your example (app.use('*', ...)).



          You could do that with a separate router:



          app.all('/url', keycloak.protect())

          const router = express.Router();
          app.use(router);

          app.use('*', [some_midleware])


          Then, to add new route handlers, you'd add them to router, not app:



          router.get('/new url', keycloak.protect());


          Because router is added before app.use('*', ...), it will always get to handle requests first. Only if requests don't match any handlers will be pass the request on the handler on the last line.






          share|improve this answer























          • sorry, i modified the answer app.all('*', [some_midleware]) is used
            – user1264304
            Nov 20 at 7:53






          • 1




            @user1264304 I've edited my answer accordingly, but the method proposed remains the same :D
            – robertklep
            Nov 20 at 7:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380703%2fdynamically-protecting-url-using-node-js-keycloak-adapter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          From what I understand, you want to dynamically add new routes that get handled before the last route in your example (app.use('*', ...)).



          You could do that with a separate router:



          app.all('/url', keycloak.protect())

          const router = express.Router();
          app.use(router);

          app.use('*', [some_midleware])


          Then, to add new route handlers, you'd add them to router, not app:



          router.get('/new url', keycloak.protect());


          Because router is added before app.use('*', ...), it will always get to handle requests first. Only if requests don't match any handlers will be pass the request on the handler on the last line.






          share|improve this answer























          • sorry, i modified the answer app.all('*', [some_midleware]) is used
            – user1264304
            Nov 20 at 7:53






          • 1




            @user1264304 I've edited my answer accordingly, but the method proposed remains the same :D
            – robertklep
            Nov 20 at 7:54















          up vote
          1
          down vote



          accepted










          From what I understand, you want to dynamically add new routes that get handled before the last route in your example (app.use('*', ...)).



          You could do that with a separate router:



          app.all('/url', keycloak.protect())

          const router = express.Router();
          app.use(router);

          app.use('*', [some_midleware])


          Then, to add new route handlers, you'd add them to router, not app:



          router.get('/new url', keycloak.protect());


          Because router is added before app.use('*', ...), it will always get to handle requests first. Only if requests don't match any handlers will be pass the request on the handler on the last line.






          share|improve this answer























          • sorry, i modified the answer app.all('*', [some_midleware]) is used
            – user1264304
            Nov 20 at 7:53






          • 1




            @user1264304 I've edited my answer accordingly, but the method proposed remains the same :D
            – robertklep
            Nov 20 at 7:54













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          From what I understand, you want to dynamically add new routes that get handled before the last route in your example (app.use('*', ...)).



          You could do that with a separate router:



          app.all('/url', keycloak.protect())

          const router = express.Router();
          app.use(router);

          app.use('*', [some_midleware])


          Then, to add new route handlers, you'd add them to router, not app:



          router.get('/new url', keycloak.protect());


          Because router is added before app.use('*', ...), it will always get to handle requests first. Only if requests don't match any handlers will be pass the request on the handler on the last line.






          share|improve this answer














          From what I understand, you want to dynamically add new routes that get handled before the last route in your example (app.use('*', ...)).



          You could do that with a separate router:



          app.all('/url', keycloak.protect())

          const router = express.Router();
          app.use(router);

          app.use('*', [some_midleware])


          Then, to add new route handlers, you'd add them to router, not app:



          router.get('/new url', keycloak.protect());


          Because router is added before app.use('*', ...), it will always get to handle requests first. Only if requests don't match any handlers will be pass the request on the handler on the last line.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 7:54

























          answered Nov 20 at 7:51









          robertklep

          134k17231240




          134k17231240












          • sorry, i modified the answer app.all('*', [some_midleware]) is used
            – user1264304
            Nov 20 at 7:53






          • 1




            @user1264304 I've edited my answer accordingly, but the method proposed remains the same :D
            – robertklep
            Nov 20 at 7:54


















          • sorry, i modified the answer app.all('*', [some_midleware]) is used
            – user1264304
            Nov 20 at 7:53






          • 1




            @user1264304 I've edited my answer accordingly, but the method proposed remains the same :D
            – robertklep
            Nov 20 at 7:54
















          sorry, i modified the answer app.all('*', [some_midleware]) is used
          – user1264304
          Nov 20 at 7:53




          sorry, i modified the answer app.all('*', [some_midleware]) is used
          – user1264304
          Nov 20 at 7:53




          1




          1




          @user1264304 I've edited my answer accordingly, but the method proposed remains the same :D
          – robertklep
          Nov 20 at 7:54




          @user1264304 I've edited my answer accordingly, but the method proposed remains the same :D
          – robertklep
          Nov 20 at 7:54


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380703%2fdynamically-protecting-url-using-node-js-keycloak-adapter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?