<div dir="ltr"><div><div>Hi,<br><br></div>Is your NodeJS app just a REST backend without any frontend ? In this case you should put "bearer-only: true" and then it is the responsibility of your frontend or any other service to pass the token to your rest service. <br><br></div>Sebi<br><div> <br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 8, 2016 at 7:03 AM, Deepak Garg <span dir="ltr"><<a href="mailto:deepakgarg.garg@gmail.com" target="_blank">deepakgarg.garg@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span style="font-size:12.8px">I have created a rest api in node js and used keycloak-connect npm packge.</span><br style="font-size:12.8px"><span style="font-size:12.8px">I have mapped the nodejs middleware with keycloak middleware and just put</span><br style="font-size:12.8px"><span style="font-size:12.8px">keycloak.Protect() method in side api method.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">When the user is not logged in, it shows a login screen and ask for</span><br style="font-size:12.8px"><span style="font-size:12.8px">credential. After login, it shows the result. but I don't want to show a</span><br style="font-size:12.8px"><span style="font-size:12.8px">login screen if user is not already logged in. Instead of that i want to</span><br style="font-size:12.8px"><span style="font-size:12.8px">pass the token and get access based upon that token?</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Do i need to do anything in the API code so that it will accept the user</span><br style="font-size:12.8px"><span style="font-size:12.8px">token?</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">I like to use this api through User interface and set the access type</span><br style="font-size:12.8px"><span style="font-size:12.8px">bearer for this service in the keycloak admin.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">see the example:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px"> var express = require('express');</span><br style="font-size:12.8px"><span style="font-size:12.8px"> var apiRoutes = express.Router();</span><br style="font-size:12.8px"><span style="font-size:12.8px"> var User = require('../models/user');</span><br style="font-size:12.8px"><span style="font-size:12.8px"> var jwt = require('jsonwebtoken');</span><br style="font-size:12.8px"><span style="font-size:12.8px"> var faker = require('faker');</span><br style="font-size:12.8px"><span style="font-size:12.8px"> var session = require('express-session');</span><br style="font-size:12.8px"><span style="font-size:12.8px"> var Keycloak = require('keycloak-connect');</span><br style="font-size:12.8px"><span style="font-size:12.8px"> var hogan = require('hogan-express');</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px"> var memoryStore = new session.MemoryStore();</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px"> var keycloak = new Keycloak({store: memoryStore});</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px"> app.use(session({</span><br style="font-size:12.8px"><span style="font-size:12.8px"> secret: app.get('superSecret'),</span><br style="font-size:12.8px"><span style="font-size:12.8px"> resave: false,</span><br style="font-size:12.8px"><span style="font-size:12.8px"> saveUninitialized: true,</span><br style="font-size:12.8px"><span style="font-size:12.8px"> store: memoryStore</span><br style="font-size:12.8px"><span style="font-size:12.8px"> }));</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px"> app.use(keycloak.middleware({</span><br style="font-size:12.8px"><span style="font-size:12.8px"> logout: '/logout',</span><br style="font-size:12.8px"><span style="font-size:12.8px"> admin: '/'</span><br style="font-size:12.8px"><span style="font-size:12.8px"> }));</span><br style="font-size:12.8px"><span style="font-size:12.8px"> app.get('/api/user',* keycloak.protect()*, function (req, res) {</span><br style="font-size:12.8px"><span style="font-size:12.8px"> res.json({</span><br style="font-size:12.8px"><span style="font-size:12.8px"> name: faker.name.findName(),</span><br style="font-size:12.8px"><span style="font-size:12.8px"> email: faker.internet.email(),</span><br style="font-size:12.8px"><span style="font-size:12.8px"> address: faker.address.streetAddress(),</span><br style="font-size:12.8px"><span style="font-size:12.8px"> bio: faker.lorem.sentence(),</span><br style="font-size:12.8px"><span style="font-size:12.8px"> image: faker.image.avatar()</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px"> });</span><br style="font-size:12.8px"><span style="font-size:12.8px"> });</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Keycloak.json:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">{</span><br style="font-size:12.8px"><span style="font-size:12.8px"> "realm" : "nodejs-example",</span><br style="font-size:12.8px"><span style="font-size:12.8px"> "realm-public-key" :</span><br style="font-size:12.8px"><span style="font-size:12.8px">"</span><span style="font-size:12.8px">MIGfMA0GCSqGSIb3DQEBAQUAA4GNA<wbr>D</span><span style="font-size:12.8px">CBiQKBgQCrVrCuTtArbgaZzL1hvh0<wbr>x</span><span style="font-size:12.8px">tL5mc7o0NqPVnYXkLvgcwiC3BjLGw<wbr>1</span><span style="font-size:12.8px">tGEGoJaXDuSaRllobm53JBhjx33UN<wbr>v</span><span style="font-size:12.8px">+5z/</span><span style="font-size:12.8px">UMG4kytBWxheNVKnL6GgqlNab<wbr>MaFfP</span><span style="font-size:12.8px">LPCF8kAgKnsi79NMo+</span><span style="font-size:12.8px">n6KnSY8<wbr>YeUmec/</span><span style="font-size:12.8px">p2vjO2NjsSAVcWEQMVhJ31L<wbr>wIDAQAB</span><span style="font-size:12.8px">",</span><br style="font-size:12.8px"><span style="font-size:12.8px"> "auth-server-url" : "</span><a href="http://xxxx:9090/auth" rel="noreferrer" style="font-size:12.8px" target="_blank">http://xxxx:9090/auth</a><span style="font-size:12.8px">",</span><br style="font-size:12.8px"><span style="font-size:12.8px"> "ssl-required" : "external",</span><br style="font-size:12.8px"><span style="font-size:12.8px"> "resource" : "nodejs-connect",</span><br style="font-size:12.8px"><span style="font-size:12.8px"> "public-client" : true</span><br style="font-size:12.8px"><span style="font-size:12.8px">}</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Thanks,</span><br style="font-size:12.8px"><span style="font-size:12.8px">Deepak</span><br></div>
<br>______________________________<wbr>_________________<br>
keycloak-user mailing list<br>
<a href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" rel="noreferrer" target="_blank">https://lists.jboss.org/<wbr>mailman/listinfo/keycloak-user</a><br></blockquote></div><br></div>