Skip to content

Commit 2fed973

Browse files
author
maxim-lixakov
committed
[DOP-19933] - update worker, scheduler, keycloak documentation
1 parent 950fb35 commit 2fed973

16 files changed

+221
-40
lines changed

.env.docker

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ SYNCMASTER__SERVER__SESSION__SECRET_KEY=session_secret_key
2121
# Postgres
2222
SYNCMASTER__DATABASE__URL=postgresql+asyncpg://syncmaster:changeme@db:5432/syncmaster
2323

24-
# TODO: add to KeycloakAuthProvider documentation about creating new realms, add users, etc.
2524
# KEYCLOAK Auth
2625
SYNCMASTER__AUTH__KEYCLOAK__SERVER_URL=http://keycloak:8080
2726
SYNCMASTER__AUTH__KEYCLOAK__REALM_NAME=manually_created
127 KB
Loading
130 KB
Loading
336 KB
Loading
119 KB
Loading
383 KB
Loading
116 KB
Loading
151 KB
Loading
86.6 KB
Loading

docs/backend/auth/keycloak.rst

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ KeyCloak Auth provider
55

66
Description
77
-----------
8+
Keycloak auth provider uses `python-keycloak <https://pypi.org/project/python-keycloak/>`_ library to interact with Keycloak server. During the authentication process,
9+
KeycloakAuthProvider redirects user to Keycloak authentication page.
810

9-
TODO:
11+
After successful authentication, Keycloak redirects user back to Syncmaster with authorization code.
12+
Then KeycloakAuthProvider exchanges authorization code for an access token and uses it to get user information from Keycloak server.
13+
If user is not found in Syncmaster database, KeycloakAuthProvider creates it. Finally, KeycloakAuthProvider returns user with access token.
1014

11-
Strategies
12-
----------
13-
14-
TODO:
15+
You can follow interaction schema below.
1516

1617
Interaction schema
1718
------------------
@@ -76,6 +77,107 @@ Interaction schema
7677
Basic configuration
7778
-------------------
7879

79-
.. autopydantic_model:: syncmaster.settings.auth.keycloak.KeycloakProviderSettings
80-
.. autopydantic_model:: syncmaster.settings.auth.jwt.JWTSettings
80+
.. autopydantic_model:: syncmaster.backend.settings.auth.keycloak.KeycloakAuthProviderSettings
81+
.. autopydantic_model:: syncmaster.backend.settings.auth.keycloak.KeycloakSettings
82+
.. autopydantic_model:: syncmaster.backend.settings.auth.jwt.JWTSettings
83+
84+
85+
Local installation and testing
86+
-----------------------------
87+
88+
You can test Keycloak auth locally with docker compose:
89+
90+
91+
.. code-block:: console
92+
93+
$ docker compose -f docker-compose.test.yml up keycloak -d
94+
95+
96+
At first, you have to go to `http://localhost:8080/admin <http://localhost:8080/admin>`_ and login via login: `admin`, password: `admin` (by default) to create realms.
97+
98+
.. image:: images/keycloak-login.png
99+
:width: 400px
100+
:align: center
101+
102+
103+
Create new realm:
104+
105+
.. image:: images/keycloak-new-realm.png
106+
:width: 400px
107+
:align: center
108+
109+
110+
Create new realm: pass realm name value. Then pass it to `SYNCMASTER__AUTH__KEYCLOAK__REALM_NAME` environment variable:
111+
112+
.. code-block:: console
113+
114+
$ export SYNCMASTER__AUTH__KEYCLOAK__REALM_NAME=fastapi_realm # as on screen below
115+
116+
.. image:: images/keycloak-new-realm_name.png
117+
:width: 400px
118+
:align: center
119+
120+
121+
Create new client in created realm:
122+
123+
.. image:: images/keycloak-new-client.png
124+
:width: 400px
125+
:align: center
126+
127+
128+
Create new client in created realm: pass client name value. Then pass it to `SYNCMASTER__AUTH__KEYCLOAK__CLIENT_ID` environment variable:
129+
130+
.. code-block:: console
131+
132+
$ export SYNCMASTER__AUTH__KEYCLOAK__REALM_NAME=fastapi_client # as on screen below
133+
134+
.. image:: images/keycloak-new-client_name.png
135+
:width: 400px
136+
:align: center
137+
138+
139+
Set client_authentication **ON** to receive client_secret:
140+
141+
.. image:: images/keycloak-client-authentication.png
142+
:width: 400px
143+
:align: center
144+
145+
Configure Redirect URI:
146+
147+
To configure the redirect URI where the browser will redirect to exchange the code provided from Keycloak for an access token, set the `SYNCMASTER__AUTH__KEYCLOAK__REDIRECT_URI` environment variable. The default value for local development is `http://localhost:8000/auth/callback`.
148+
149+
.. code-block:: console
150+
151+
$ export SYNCMASTER__AUTH__KEYCLOAK__REDIRECT_URI=http://localhost:8000/auth/callback
152+
153+
Ensure that this URI is also configured as a valid redirect URI in your Keycloak client settings. This allows the browser to redirect to your application after the user successfully authenticates with Keycloak.
154+
155+
.. image:: images/keycloak-client-redirect_uri.png
156+
:width: 400px
157+
:align: center
158+
159+
Now go to **Credentials** tab and add the client secret to the `SYNCMASTER__AUTH__KEYCLOAK__CLIENT_SECRET` environment variable:
160+
161+
.. code-block:: console
162+
163+
$ export SYNCMASTER__AUTH__KEYCLOAK__CLIENT_SECRET=6x6gn8uJdWSBmP8FqbNRSoGdvaoaFeez # as on screen below
164+
165+
.. image:: images/keycloak-client-secret.png
166+
:width: 400px
167+
:align: center
168+
169+
Now you can use create users in this realms, check `keycloak documentation <https://www.keycloak.org/docs/latest/server_admin/#assembly-managing-users_server_administration_guide>`_ on how to manage users creation.
170+
171+
After this you can user `KeycloakAuthProvider` in your application with provided environment variables:
172+
173+
174+
.. code-block:: console
81175
176+
$ export SYNCMASTER__AUTH__KEYCLOAK__SERVER_URL=http://keycloak:8080
177+
$ export SYNCMASTER__AUTH__KEYCLOAK__REDIRECT_URI=http://localhost:8000/auth/callback
178+
$ export SYNCMASTER__AUTH__KEYCLOAK__REALM_NAME=fastapi_realm
179+
$ export SYNCMASTER__AUTH__KEYCLOAK__CLIENT_ID=fastapi_client
180+
$ export SYNCMASTER__AUTH__KEYCLOAK__CLIENT_SECRET=6x6gn8uJdWSBmP8FqbNRSoGdvaoaFeez
181+
$ export SYNCMASTER__AUTH__KEYCLOAK__SCOPE=email
182+
$ export SYNCMASTER__AUTH__KEYCLOAK__VERIFY_SSL=False
183+
$ export SYNCMASTER__AUTH__PROVIDER=syncmaster.backend.providers.auth.keycloak_provider.KeycloakAuthProvider

0 commit comments

Comments
 (0)