Giter VIP home page Giter VIP logo

Comments (7)

germainlefebvre4 avatar germainlefebvre4 commented on June 7, 2024 2

Okey!!! I missed something... my bad

Service Accounts Roles was added to test-client instead of admin-cli.

from fastapi-keycloak.

yannicschroeer avatar yannicschroeer commented on June 7, 2024

Mhh, that's weird. Seems like no token was issued. Did you save and import the realm-export.json as described in the docker compose yaml? I can't reproduce the error with the setup provided in the quickstart

from fastapi-keycloak.

germainlefebvre4 avatar germainlefebvre4 commented on June 7, 2024

Yes I created a brand new reals name Test and imported the file on realm creation.

My keycloak lives on port 8080 and my app on port 8081.

Docker images used:

  • jboss/keycloak:16.1.0
  • postgres:12

Here is my docker-compose.yml:

version: '3'

networks:
  default:
    external: true
    name: maggiesfarm

services:
  db_keycloak:
    container_name: maggiesfarm_db_keycloak
    image: postgres:12
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: keycloak
      POSTGRES_USER: keycloak
      POSTGRES_PASSWORD: password
  keycloak:
    container_name: maggiesfarm_keycloak
    image: jboss/keycloak:16.1.0
    # image: quay.io/keycloak/keycloak:latest
    command:
      - "-b 0.0.0.0 -Dkeycloak.profile.feature.upload_scripts=enabled"
    environment:
      DB_VENDOR: POSTGRES
      DB_ADDR: db_keycloak
      DB_DATABASE: keycloak
      DB_USER: keycloak
      DB_SCHEMA: public
      DB_PASSWORD: password
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: password
      # Uncomment the line below if you want to specify JDBC parameters. The parameter below is just an example, and it shouldn't be used in production without knowledge. It is highly recommended that you read the PostgreSQL JDBC driver documentation in order to use it.
      #JDBC_PARAMS: "ssl=true"
    ports:
      - 8080:8080
    depends_on:
      - db_keycloak

volumes:
  postgres_data:
      driver: local

Somethin weird appeared on first keycloak login by fastapi and I needed to regenerate the "Client" secret and reported its new value in code:

app = FastAPI()
idp = FastAPIKeycloak(
    server_url="http://localhost:8080/auth",
    client_id="test-client",
    client_secret="z9vH17vqCWhngPj3IIEK5fTfePAdkR6e", # <<< new generated token for test-client
    admin_client_secret="hwBLV11xaGvkeQ24xLLzsASNFNPri9t4", # <<< new generated token for admin-cli
    realm="Test",
    callback_uri="http://localhost:8081/callback"
)

from fastapi-keycloak.

germainlefebvre4 avatar germainlefebvre4 commented on June 7, 2024

Before this step, with the intial copy/paste from your doc I had:

Traceback (most recent call last):
  File "/home/germainlef/.local/share/virtualenvs/auth-keycloak-WIW7vmvr/lib/python3.8/site-packages/fastapi_keycloak/api.py", line 285, in _get_admin_token
    self.admin_token = response.json()['access_token']
KeyError: 'access_token'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "try_fastapi-keycloak.py", line 10, in <module>
    idp = FastAPIKeycloak(
  File "/home/germainlef/.local/share/virtualenvs/auth-keycloak-WIW7vmvr/lib/python3.8/site-packages/fastapi_keycloak/api.py", line 125, in __init__
    self._get_admin_token()  # Requests an admin access token on startup
  File "/home/germainlef/.local/share/virtualenvs/auth-keycloak-WIW7vmvr/lib/python3.8/site-packages/fastapi_keycloak/api.py", line 289, in _get_admin_token
    raise KeycloakError(reason=f"The response did not contain an access_token: {response.json()}", status_code=403)
fastapi_keycloak.exceptions.KeycloakError: HTTP 403: The response did not contain an access_token: {'error': 'unauthorized_client', 'error_description': 'Invalid client secret'}

That's why I regerated the token

from fastapi-keycloak.

yannicschroeer avatar yannicschroeer commented on June 7, 2024

Please try to alter your docker compose yaml as follows:

volumes:
      - ./realm-export.json:/opt/jboss/keycloak/imports/realm-export.json
command:
      - "-b 0.0.0.0 -Dkeycloak.profile.feature.upload_scripts=enabled -Dkeycloak.import=/opt/jboss/keycloak/imports/realm-export.json"

You currently do not bind or import the config files. If you do not want to use our predefined config, please alter your realms admin-cli client:

  • Service Accounts enabled
  • Full Scope Allowed
  • Service Accounts Roles: all available roles for account and realm_management

as described in keycloak configuration.


However, thanks for opening this issue. It clearly shows that the error messages need a lot of improvement regarding explicity and exception handling in general.

from fastapi-keycloak.

jonra1993 avatar jonra1993 commented on June 7, 2024

Thanks, @germainlefebvre4 I had the same error. These are the configurations required on admin-cli

image

image

from fastapi-keycloak.

germainlefebvre4 avatar germainlefebvre4 commented on June 7, 2024

Absolutly!

I have scripted this part to avoid hands mistakes

from fastapi-keycloak.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.