[keycloak-user] CORS error in Keycloak Admin REST API - Unable to create realms from React JS app

Vinay Matam vinay at dailykit.org
Fri Nov 15 07:42:11 EST 2019


Hi,
I need help with a situation.
We are trying to create new realms in Keycloak dynamically from a ReactJS
based application with the help of the Keycloak Admin Rest API. Here's what
we have done so far:

Created a client in the master realm, "realm-creator", type = confidential,
Service Accounts Enabled = true, Scope = admin & create-realm selected,
Service Account Roles = create-realm.

I then fetch an access token of the above client using the grant_type =
client_credentials, Authorization = BASIC and using the client id and
client secret as username and password. I am successfully getting the
access token.

Now, as a next step, I am using this access token to create new realms by
calling the Admin REST API endpoint to create new realm,
https://<keycloakserver>/auth/admin/realms
Authorization: Bearer <accesstoken> // Access token generated from the
above step using "realm-creator" client
and sending the JSON body representation of the realm representation.

Now, everything is working fine as expected when I test this from postman.
Realm is getting created successfully.

But when I try to implement this from a ReactJS app, I am getting a CORS
error.
For clients, we have an option of "Web Origins" and we can configure a "*"
or the URL we want, to solve the CORS issue. But here the client
"realm-creator" does not have "Standard Flow" Enabled and I am not seeing
the option of "WebOrigins".

Where should I configure the CORS setting for the Keycloak Admin REST API
to avoid CORS error ?
Please help.

Thank you!


More information about the keycloak-user mailing list