Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7e3c821

Browse files
author
Hugo
committed
doc: add django-oauth-toolkit to oidc doc
Signed-off-by: Hugo Delval <[email protected]>
1 parent 10671da commit 7e3c821

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

changelog.d/10192.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add documentation on how to connect Django with synapse using oidc and django-oauth-toolkit. Contributed by @HugoDelval.

docs/openid.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,51 @@ The synapse config will look like this:
446446
config:
447447
email_template: "{{ user.email }}"
448448
```
449+
450+
## Django OAuth Toolkit
451+
452+
[django-oauth-toolkit](https://github.com/jazzband/django-oauth-toolkit) is a
453+
Django application providing out of the box all the endpoints, data and logic
454+
needed to add OAuth2 capabilities to your Django projects. It supports
455+
[OpenID Connect too](https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html).
456+
457+
**Configuration on Django's side:**
458+
459+
1. Add an application: https://example.com/admin/oauth2_provider/application/add/ and choose parameters like this:
460+
* `Redirect uris`: https://synapse.example.com/_synapse/client/oidc/callback
461+
* `Client type`: `Confidential`
462+
* `Authorization grant type`: `Authorization code`
463+
* `Algorithm`: `HMAC with SHA-2 256`
464+
2. You can [customize the claims](https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html#customizing-the-oidc-responses) Django gives to synapse (optional):
465+
<details>
466+
<summary>Code sample</summary>
467+
468+
```python
469+
class CustomOAuth2Validator(OAuth2Validator):
470+
471+
def get_additional_claims(self, request):
472+
return {
473+
"sub": request.user.email,
474+
"email": request.user.email,
475+
"first_name": request.user.first_name,
476+
"last_name": request.user.last_name,
477+
}
478+
```
479+
</details>
480+
3. Your synapse config is then:
481+
482+
```yaml
483+
oidc_providers:
484+
- idp_id: django_example
485+
idp_name: "Django Example"
486+
issuer: "https://example.com/o/"
487+
client_id: "your-client-id" # CHANGE ME
488+
client_secret: "your-client-secret" # CHANGE ME
489+
scopes: ["openid"]
490+
user_profile_method: "userinfo_endpoint" # needed because oauth-toolkit does not include user information in the authorization response
491+
user_mapping_provider:
492+
config:
493+
localpart_template: "{{ user.email.split('@')[0] }}"
494+
display_name_template: "{{ user.first_name }} {{ user.last_name }}"
495+
email_template: "{{ user.email }}"
496+
```

0 commit comments

Comments
 (0)