Skip to content

Keycloak authentication #8243

Open
@mohamad-rai

Description

@mohamad-rai

New Issue Checklist

  • I am not disclosing a vulnerability.
    I am not just asking a question.
    I have searched through existing issues.
    I can reproduce the issue with the latest version of Parse Server.

Issue Description

Hi
my problem is that I can't find out how to login with keycloak, I searched a lot and couldn't make it work.
I know in backend, parse server should config for support Keycloak and in front user should login/signup with method linkWith. but the problem is that I get [403] -> unauthorized error with any payload.

Steps to reproduce

Backend:

const fs = require('fs');
const express = require('express');
const ParseServer = require('parse-server').ParseServer;
const cors = require('cors');

const app = express();
app.use(express.json());
app.use(express.text());

const api = new ParseServer({
  databaseURI: 'mongodb://admin:admin@localhost:27017/', // Connection string for your MongoDB database
  cloud: './cloud/main.js', // Path to your Cloud Code
  appId: 'myAppId',
  masterKey: 'myMasterKey', // Keep this key secret!
  fileKey: 'optionalFileKey',
  serverURL: 'http://localhost:8080/parse', // Don't forget to change to https if needed
  auth: {
    keycloak: { 
         config: {
    "realm": "BenjaminR",
    "auth-server-url": "http://localhost:8081/",
    "ssl-required": "external",
    "resource": "my-kc",
    "verify-token-audience": true,
    "credentials": {
      "secret": "KVuKQ47Re25rewsd0RE8lUlaziDtfcDSH"
    },
    "use-resource-role-mappings": true,
    "confidential-port": 0
}
    }
  }
});
app.use(cors('*'));

// Serve the Parse API on the /parse URL prefix
app.use(cors('*'));
app.use('/parse', api);

app.listen(8080, function() {
  console.log('parse-server-example running on port 1337.');
});

Client:

const token = 'keycloak valid and tested token'
const user = new Parse.User();
    await user.linkWith('keycloak', {
      authData: {
        access_token: token,
        id: "cwq613e9-a220-2g7a-bfx7-72722978wc02",
        roles: ["roles..."],
        groups: ["user-groups"]
      }
    });

Actual Outcome

{ "error": "unauthorized" }

Expected Outcome

expected response in document

Server

  • Parse Server version: 5.2.8
  • Operating system: windows 10
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): local

Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: 6.0.2
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): local (docker)

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): javascript
  • SDK version: 3.4.4

Logs

no log

Activity

parse-github-assistant

parse-github-assistant commented on Oct 16, 2022

@parse-github-assistant

Thanks for opening this issue!

  • ❌ Please edit your post and use the provided template when creating a new issue. This helps everyone to understand your post better and asks for essential information to quicker review the issue.
added
type:bugImpaired feature or lacking behavior that is likely assumed
on Oct 16, 2022
mohamad-rai

mohamad-rai commented on Oct 17, 2022

@mohamad-rai
Author

I found the problem, it was for app.use(express.text()); in the backend, it made all requests unauthorized but there is a conflict.
if I don't use app.use(express.text()); I will get Unexpected token u in JSON... for linking the user (above client code) and if I use it I will get an unauthorized error!!!
what should I do???

mtrezza

mtrezza commented on Oct 17, 2022

@mtrezza
Member

I'm closing this as it does not seem to be a Parse Server issue.

added
type:questionSupport or code-level question
and removed
type:bugImpaired feature or lacking behavior that is likely assumed
on Oct 17, 2022
mohamad-rai

mohamad-rai commented on Oct 18, 2022

@mohamad-rai
Author

there was a bug in the keycloak.js adapter, also in document, you said with keycloak.json we can connect but in your HTTP request module, you used https that which separated the host and protocol, and path. it has two problems.

  1. in local, we don't have https, and no one couldn't find this problem in error messages.
  2. my online keycloak gave me a URL with protocol and path that https package doesn't accept in one URI string!!

also, you didn't say we must have roles and groups in keycloak userinfo response!
I fixed the issues in the parse-server source for our company but I believe it should be fixed in the source to be able to get updates.

dcelix

dcelix commented on Oct 18, 2022

@dcelix

Hi @mohamad-rai, we are facing the same issue, and also solved it on our source.

I'm willing to make our changes available upstream if you are not planning on opening a PR with your fix. On our side, we are not using http connection, just allowing https connections with self-signed certificates for dev environments.

mtrezza

mtrezza commented on Oct 19, 2022

@mtrezza
Member

I fixed the issues in the parse-server source for our company but I believe it should be fixed in the source to be able to get updates.

Yes, would you want to open a PR?

added
type:bugImpaired feature or lacking behavior that is likely assumed
and removed
type:questionSupport or code-level question
on Oct 19, 2022
mohamad-rai

mohamad-rai commented on Oct 20, 2022

@mohamad-rai
Author

I fixed the issue and created a branch for it but unfortunately, in Iran, we don't have a stable connection to GitHub, poor connection from inside and I think forbidden access to GitHub from outside made a beautiful programming life for us!
I can send the changes to your email if you want.

mtrezza

mtrezza commented on Oct 20, 2022

@mtrezza
Member

Could you share a dropbox / google drive / etc link? Also, maybe try the 1.1.1.1 app, the DoH encryption may not allow the domain firewall to detect your target domain.

mohamad-rai

mohamad-rai commented on Oct 22, 2022

@mohamad-rai
Author
mtrezza

mtrezza commented on Oct 23, 2022

@mtrezza
Member

Thanks for the archive; which files did you change, so I can open a PR?

mohamad-rai

mohamad-rai commented on Oct 26, 2022

@mohamad-rai
Author

httpsRequest.js utils.js (added, maybe you should move it somewhere else), keycloak.js all in \src\Adapters\Auth

linked a pull request that will close this issue on Oct 26, 2022
mtrezza

mtrezza commented on Oct 26, 2022

@mtrezza
Member

@mohamad-rai I've opened #8259. Any code changes you can make there directly by adding a review with a code suggestion or posting them there as a comment. I see that a test case is missing there, we'd need that too. We'll use the PR thread to discuss any changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugImpaired feature or lacking behavior that is likely assumed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @dcelix@mtrezza@mohamad-rai

      Issue actions

        Keycloak authentication · Issue #8243 · parse-community/parse-server