You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/backend/auth/keycloak.rst
+109-7Lines changed: 109 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,14 @@ KeyCloak Auth provider
5
5
6
6
Description
7
7
-----------
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.
8
10
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.
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:
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`.
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.
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:
0 commit comments