Skip to content

Commit d8f9573

Browse files
SSO OIDC custom auth and connecting with Neo4j Python driver (#1151)
* SSO custom auth and SSO with neo4j python driver docs * PR changes * PR changes * update link --------- Co-authored-by: katarinasupe <[email protected]> Co-authored-by: Katarina Supe <[email protected]>
1 parent 6402e8e commit d8f9573

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

pages/client-libraries/python.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Once the database is running and the client is installed or available in Python,
177177
- [Connect without authentication (default)](#connect-without-authentication-default)
178178
- [Connect with authentication](#connect-with-authentication)
179179
- [Connect with self-signed certificate](#encrypted-database-connection-with-self-signed-certificate)
180+
- [Connect with Single sign-on (SSO)](#connect-with-single-sign-on-sso)
180181

181182
#### Connect without authentication (default)
182183

@@ -269,6 +270,31 @@ with GraphDatabase.driver(URI, auth=AUTH) as client:
269270
print(record["name"])
270271
```
271272

273+
#### Connect with Single sign-on (SSO)
274+
275+
<Callout type="warning">
276+
This is currently only supported for OIDC SSO.
277+
</Callout>
278+
279+
To use SSO with the Python driver you need to get the access and id tokens yourself.
280+
One simple way to do it is to use the authlib library and follow the official [tutorial](https://docs.authlib.org/en/latest/client/oauth2.html).
281+
282+
To connect to the Memgraph database you have to use the `custom_auth` class with the `scheme` parameter set as `oidc-entra-id`, `oidc-okta` or `oidc-custom` depending on which scheme you are using,
283+
`credentials` parameter set to contain both access and id tokens in the format shown in the example below. Finally set `principal` and `realm` parameters to `None`.
284+
285+
Below is an example of connecting to the Memgraph database using OIDC SSO with custom auth scheme.
286+
```python
287+
with neo4j.GraphDatabase.driver(
288+
"bolt://localhost:7687",
289+
auth=neo4j.custom_auth(
290+
scheme="oidc-custom",
291+
credentials=f"access_token={token['access_token']};id_token={token['id_token']}",
292+
principal=None,
293+
realm=None,
294+
)
295+
) as driver:
296+
```
297+
272298
### Query the database
273299

274300
After connecting your client to Memgraph, you can start running queries. The simplest way to run queries is by using the `execute_query()` method which has an automatic transaction management.

pages/database-management/authentication-and-authorization/auth-system-integrations.mdx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,31 @@ Role mapping is described [here](#single-sign-on).
295295

296296
Issuer is `https://{your-okta-domain}.okta.com/oauth2/default/`. You can find the client ID on the Admin panel -> Applications -> General. You can find the authorization server on the Admin panel -> Security -> API -> Authorization Servers -> Audience. By default, it is set to `api://default`.
297297

298+
##### Custom auth
299+
300+
<Callout type="warning">
301+
This is currently only supported through the Neo4j drivers.
302+
</Callout>
303+
304+
If you are using an OIDC provider which is not listed above you can use you the custom auth scheme.
305+
The only requirement is that your OIDC provider supports verifying the tokens through RSA algorithm (public & private key).
306+
307+
Setup the following environmental variables:
308+
309+
```mdx
310+
MEMGRAPH_SSO_CUSTOM_OIDC_PUBLIC_KEY_ENDPOINT=`URI where the public key for validating the tokens is exposed`
311+
MEMGRAPH_SSO_CUSTOM_OIDC_ACCESS_TOKEN_AUDIENCE=`access token audience`
312+
MEMGRAPH_SSO_CUSTOM_OIDC_ID_TOKEN_AUDIENCE=`id token audience`
313+
MEMGRAPH_SSO_CUSTOM_OIDC_ROLE_FIELD=`access token field to be used in the role mapping`
314+
MEMGRAPH_SSO_CUSTOM_OIDC_USERNAME=
315+
MEMGRAPH_SSO_CUSTOM_OIDC_ROLE_MAPPING=
316+
```
317+
318+
Usernames are described below and role mappings are described [here](#single-sign-on).
319+
One way to deduce the audience of the access and id tokens is to decode them using a tool like `jwt.io`, check the `aud` field and deduce what it is.
320+
Often time access and id token will the use the same audience. For example in MS Entra ID both tokens use the client ID as audience.
321+
322+
298323
##### Username
299324
The username variable tells the OIDC module what to use as the username. It has the format `token-type:field`.
300325
Token type can be `id` or `access` depending on whether you want to use a field from the access or the ID token for the username. See the following to learn more about [access](https://www.okta.com/identity-101/access-token/) and [id](https://developer.okta.com/docs/guides/validate-id-tokens/main/#id-tokens-vs-access-tokens) tokens.
@@ -306,6 +331,12 @@ For Okta one commonly used field is `access:sub` which is usually the email of t
306331
OIDC is by default enabled using the Memgraph `oidc.py` module. To use a custom auth module use the `--auth-module-mappings` [flag](/database-management/configuration#auth-module) like the following:
307332
`--auth-module-mappings=oidc-entra-id:/path/to/oidc-entra-module;oidc-okta:/path/to/oidc-okta-module` depending on the SSO provider you want to use.
308333

334+
#### Using OIDC SSO with the Neo4j Python driver
335+
336+
Connecting using SSO is supported with the Neo4j Python driver. For the
337+
instructions on how to connect, check the [Python driver
338+
docs](/client-libraries/python#connect-with-single-sign-on-sso).
339+
309340
## Basic (username + password) auth
310341

311342
When Memgraph is set up to use the external auth module for basic authentication

0 commit comments

Comments
 (0)