mirror of
https://github.com/suitenumerique/meet.git
synced 2026-07-27 04:09:26 +00:00
3fd5a4404c
We need to integrate with external applications. Objective: enable them to securely generate room links with proper ownership attribution. Proposed solution: Following the OAuth2 Machine-to-Machine specification, we expose an endpoint allowing external applications to exchange a client_id and client_secret pair for a JWT. This JWT is valid only within a well-scoped, isolated external API, served through a dedicated viewset. This commit introduces a model to persist application records in the database. The main challenge lies in generating a secure client_secret and ensuring it is properly stored. The restframework-apikey dependency was discarded, as its approach diverges significantly from OAuth2. Instead, inspiration was taken from oauthlib and django-oauth-toolkit. However, their implementations proved either too heavy or not entirely suitable for the intended use case. To avoid pulling in large dependencies for minimal utility, the necessary components were selectively copied, adapted, and improved. A generic SecretField was introduced, designed for reuse and potentially suitable for upstream contribution to Django. Secrets are exposed only once at object creation time in the Django admin. Once the object is saved, the secret is immediately hashed, ensuring it can never be retrieved again. One limitation remains: enforcing client_id and client_secret as read-only during edits. At object creation, marking them read-only excluded them from the Django form, which unintentionally regenerated new values. This area requires further refinement. The design prioritizes configurability while adhering to the principle of least privilege. By default, new applications are created without any assigned scopes, preventing them from performing actions on the API until explicitly configured. If no domain is specified, domain delegation is not applied, allowing tokens to be issued for any email domain.
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""
|
|
Core application fields
|
|
"""
|
|
|
|
from logging import getLogger
|
|
|
|
from django.contrib.auth.hashers import identify_hasher, make_password
|
|
from django.db import models
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
class SecretField(models.CharField):
|
|
"""CharField that automatically hashes secrets before saving.
|
|
|
|
Use for API keys, client secrets, or tokens that should never be stored
|
|
in plain text. Already-hashed values are preserved to prevent double-hashing.
|
|
|
|
Inspired by: https://github.com/django-oauth-toolkit/django-oauth-toolkit
|
|
"""
|
|
|
|
def pre_save(self, model_instance, add):
|
|
"""Hash the secret if not already hashed, otherwise preserve it."""
|
|
|
|
secret = getattr(model_instance, self.attname)
|
|
|
|
try:
|
|
hasher = identify_hasher(secret)
|
|
logger.debug(
|
|
"%s: %s is already hashed with %s.",
|
|
model_instance,
|
|
self.attname,
|
|
hasher,
|
|
)
|
|
except ValueError:
|
|
logger.debug(
|
|
"%s: %s is not hashed; hashing it now.", model_instance, self.attname
|
|
)
|
|
hashed_secret = make_password(secret)
|
|
setattr(model_instance, self.attname, hashed_secret)
|
|
return hashed_secret
|
|
|
|
return super().pre_save(model_instance, add)
|