Keycloak Linking

I created Parse client App based on REST Guide. And I used Keycloak as auth. I used this doc to do the linking: https://docs.parseplatform.org/rest/guide/#signing-up-and-logging-in. I also used this doc to get authData: https://docs.parseplatform.org/parse-server/guide/#oauth-and-3rd-party-authentication.

But I get error on the server:

Unexpected token u in JSON at position 0

this is my body:

{ "authData": { "keycloak": { "access_token": "eyJhbGciOiJSUzI1NiIsInR5...", "id": "lake", "roles": [], "groups": [] } } }

Could anyone help? Please.
or is there any tutorial how to link Keycloak from SDK REST Client to Parse Server?

Could you share the code for this request which is firing the error?

I tried post the params using Talend API Tester.
This is the request screenshoot:

and this is the Parse Server config:

{ "databaseURI": "mongodb://admin:[email protected]/pserver?authSource=admin", "appId": "APP_ID", "masterKey": "MASTER_KEY", "readOnlyMasterKey": "READ_ONLY_MASTER_KEY", "restAPIKey": "REST_API_KEY", "javascriptKey": "JAVASCRIPT_KEY", "serverURL": "http://localhost:1337/parse", "publicServerURL": "http://localhost:1337/parse", "mountGraphQL": true, "mountPlayground": true, "auth": { "keycloak": { "config": { "realm": "laker", "auth-server-url": "http://localhost:8080/auth/", "ssl-required": "none", "resource": "lake", "public-client": true, "confidential-port": 0 } } } }

Where do you see the error?

In parse-server log (terminal)

It looks the error is coming from this line: parse-server/keycloak.js at master · parse-community/parse-server · GitHub

Maybe we should log the string in the case it cannot be parsed. Anyways, it looks you are getting some error when trying to authenticate to Keycloak server (which will be hard to debug because of this json parse problem I mentioned before).

Is this address right? “auth-server-url”: “http://localhost:8080/auth/

Maybe you can try to perform this api call and see the error:parse-server/keycloak.js at master · parse-community/parse-server · GitHub

Yes the address is correct.
I am not sure to perform.
But thank you for your information.

After trying to solve:
The problem is: while request to keycloak server. I should use get option format manually like this:

{ host: "keycloak-semarang.sccic.cloudns.asia", port: 80, method: 'GET', path: "/auth/realms/myrealm/protocol/openid-connect/userinfo", headers: { Authorization: 'Bearer eyJ...' } }

is there any method to do that without manually?
Could any one help?

In order to achieve that, I believe you should set your Parse Server config with auth-server-url to keycloak-semarang.sccic.cloudns.asia and realm to myrealm.

It doesnt work. The problem is in httpRequest, I have tried manually add to the file. The problem is just my Parse server cannot send the request except use manual code (like I wrote before). When I write auth-server-url config using base url, then the path /auth doesnt included.

I didn’t get it. Would you mind to share your current config and what you get?

this is the snippet code from lib/Adapters/Auth/httpsRequest.js:

``
function get(options, noJSON = false) {

// console.log(options);

options = {

host: "keycloak-semarang.sccic.cloudns.asia",

port: 80,

method: 'GET',

path: "/auth/realms/laker/protocol/openid-connect/userinfo",

headers: {

  Authorization: 'Bearer eyJ...'

}

}

return new Promise((resolve, reject) => {

https.get(options, makeCallback(resolve, reject, noJSON)).on('error', reject);

});

}
``

I replaced default options with my code manually, and it works. But if I remove my code, it doesnt work. even I modify my config like you said before.

Have you tried with with auth-server-url to keycloak-semarang.sccic.cloudns.asia/auth and realm to laker ?

I’m not sure if that’s possible to use keycloak-semarang.sccic.cloudns.asia/auth as a host, but /auth is missing in the beginning of the path.

Hello I’ve exactly the same error.
It’s working well when I get the user with the token on keycloak server.
http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/userinfo
hearders : Authorization Bearer eyJ…

But when I send a link request to parse server I get this error on parse server console
{“stack”:“SyntaxError: Unexpected token u in JSON at position 0\n at JSON.par
se ()\n at handleAuth (C:\Users\Alex\AppData\Roaming\npm\nod
e_modules\parse-server\lib\Adapters\Auth\keycloak.js:92:24)\n at process
TicksAndRejections (internal/process/task_queues.js:95:5)\n at async Promise.
all (index 0)”}
SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse ()
at handleAuth (C:\Users\Alex\AppData\Roaming\npm\node_modules\parse-server\l
ib\Adapters\Auth\keycloak.js:92:24)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Promise.all (index 0)

here’s the request
POST http://localhost:1337/parse/users
headers: X-Parse-Application-Id:APPLICATION_ID, Content-Type:application/json,X-Parse-Rev:1
Json Body

{
“authData”:{
“keycloak”: {
“access_token”: “eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJtN1lsX3ZjNnZDNkVfa3poRC1MT3JMZFEza0lURDdWbGRRLWVoSktpRUpFIn0.eyJleHAiOjE2NDAzNjUyMjEsImlhdCI6MTY0MDM2NDkyMSwiYXV0aF90aW1lIjoxNjQ…”,
“id”: “backend”,
“roles”: ,
“groups”:
}
}
}

Please do you have any ideas to solve this issue ?
I’ve already tried keycloak-semarang.sccic.cloudns.asia[/auth] but it doesn’t change the error.

It looks that the error is coming from this line: parse-server/keycloak.js at alpha · parse-community/parse-server · GitHub

It is a problem in the code that is avoiding you to see the real error but it probably comes from this request:

const response = await httpsRequest.get({
      host: config['auth-server-url'],
      path: `/realms/${config['realm']}/protocol/openid-connect/userinfo`,
      headers: {
        Authorization: 'Bearer ' + access_token,
      },
    });

Do you have auth-server-url and realm set in your options? Would you mind to share your parse server settings?

ok this is my parse server config

const parseServer = new ParseServer({
  databaseURI: 'mongodb://localhost:27017/test',
  appId: 'APPLICATION_ID',
  masterKey: 'MASTER_KEY',
  serverURL: 'http://localhost:1337/parse',
  publicServerURL: 'http://localhost:1337/parse',
  appName: 'Back',
  auth: {  keycloak: {   config: require(`./auth/keycloak.json`) } }
});

Here’s keycloack.json

{
  "realm": "Myrealm",
  "auth-server-url": "http://localhost:8180/auth/",
  "ssl-required": "external",
  "resource": "backend",
  "public-client": true,
  "confidential-port": 0

}

If I change the realm name in the config it says that the realm doesn’t exist which it make me imagine that’s not realm that cause the issue

I’d try to find this line of code on node_modules and console.log the error.

e.text is undefined that’s why const error = JSON.parse(e.text); fails

the config is ok i’ve checked the config object.

The error is throw when

const response = await httpsRequest.get({
      host: config['auth-server-url'],
      path: `/realms/${config['realm']}/protocol/openid-connect/userinfo`,
      headers: {
        Authorization: 'Bearer ' + access_token
      }
    });

The exception contains this message
Error: getaddrinfo ENOTFOUND http://localhost:8180/auth/

same error with keycloak-semarang.sccic.cloudns.asia/auth/
Error: getaddrinfo ENOTFOUND Welcome to Keycloak

EDIT:
I understand the error
in host you should not have any protocole or path
and i’m using httpsRequest on a non https server so It would never work
Thanks for your help

1 Like