<div dir="ltr">well in the latest keycloak.json you pasted a " is missing for the line : <br><br>"resource": <span style="color:rgb(80,0,80);font-size:12.8px">nodejs-connect</span>",<br><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 8, 2016 at 9:13 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">Hi Sebi,<div><br></div><div>I did the same thing and defined a new client/resource called "<span style="color:rgb(80,0,80);font-size:12.8px">nodejs-connect</span>" and set the access type "bearer-only" .<div><br><div><br></div><div>but when I am running my node server, it is throwing an error</div><div><span style="font-size:12.8px">"SyntaxError: </span><b style="font-size:12.8px">Unexpected token u</b><div style="font-size:12.8px"> at Object.parse (native)</div><div style="font-size:12.8px"> at Config.loadConfiguration (D:\Sample Projects\NodePrototypes\NodeSa<wbr>mple\no</div><div style="font-size:12.8px">de_modules\keycloak-connect\no<wbr>de_modules\keycloak-auth-utils<wbr>\lib\config.js:53:23</div><div style="font-size:12.8px">)</div><div style="font-size:12.8px"> at new Config (D:\Sample Projects\NodePrototypes\NodeSa<wbr>mple\node_modules\key</div><div style="font-size:12.8px">cloak-connect\node_modules\key<wbr>cloak-auth-utils\lib\config.<wbr>js:40:10)</div><div style="font-size:12.8px"> at new Keycloak (D:\Sample Projects\NodePrototypes\NodeSa<wbr>mple\node_modules\k</div><div style="font-size:12.8px">eycloak-connect\index.js:61:17<wbr>)"</div></div></div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><span style="font-size:small"> Can you look into this below keycloak.json file. If I have specified whether it is correct?</span><br></div><div><br></div><div><b>Keycloak.json</b></div><div><br></div><div><div>{</div><div> "realm": "<span style="color:rgb(80,0,80);font-size:12.8px">nodejs-example</span>",</div><div> "realm-public-key": "<wbr>MIGfMA0GCSqGSIb3DQEBAQUAA4GNAD<wbr>CBiQKBgQCtvV0qb8+<wbr>A0pxKoRpToHhc6srY4PyoX/<wbr>pwgmR7HyV0PeUw/<wbr>DgyyCI1Wmvw3T15kWw7Q84gX8IL0wD<wbr>NtfmbhMPmz5umVeul3LzacjU9qfDqG<wbr>96Wirn7+<wbr>5Je1VieH5wRX3mtyQ2TboRVpjFD0fw<wbr>d063FYOtCynfDSS0Uo6YgjWs8QwIDA<wbr>QAB",</div><div> <b> "bearer-only": true,</b></div><div> "auth-server-url": "<a href="http://localhost:9090/auth" target="_blank">http://localhost:9090/auth</a>",</div><div> "ssl-required": "none",</div><div> "resource": <span style="color:rgb(80,0,80);font-size:12.8px">nodejs-connect</span>",</div><div> "enable-cors" : true,</div><div> "credentials": {</div><div> "secret": "6b620304-b4a9-4007-8701-<wbr>d3abb3537598"</div><div> }</div><div> }</div></div><div><br></div><div><br></div><div>Thanks,</div><div>Deepak</div><div><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 8, 2016 at 12:34 PM, Sebastien Blanc <span dir="ltr"><<a href="mailto:sblanc@redhat.com" target="_blank">sblanc@redhat.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><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"><div><div>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></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div><div><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></div></div>______________________________<wbr>_________________<br>
keycloak-user mailing list<br>
<a href="mailto:keycloak-user@lists.jboss.org" target="_blank">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/mailma<wbr>n/listinfo/keycloak-user</a><br></blockquote></div><br></div>
</blockquote></div><br></div></div></div></div>
</blockquote></div><br></div>