Skip to content

Commit

Permalink
Merge pull request #13 from bossan/feature/site-user-created-signal
Browse files Browse the repository at this point in the history
Added signal to create user cert when assigned to site
  • Loading branch information
bossan authored May 27, 2023
2 parents aecef84 + 10684a6 commit 32a0718
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,20 @@ Features:
- Self-service portal to generate client certificates and download device profiles for iOS/macOS
- Multi-site
- OCSP endpoint to validate the issued certificates

## Configuration

### Settings
```python

INSTALLED_APPS = [
...
'pki',
...
]

# Sign the iOS/macOS device profiles using SMIME
SIGN_PROFILES = True
# Generate a new user certificate when a user is assigned to a site
GENERATE_CERT_ON_CREATE = True
```
1 change: 1 addition & 0 deletions demo/demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

SIGN_PROFILES = True
GENERATE_CERT_ON_CREATE = True
2 changes: 1 addition & 1 deletion src/pki/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class PkiConfig(AppConfig):
name = 'pki'

def ready(self):
from .signals import after_site_created # Noqa F401
from .signals import after_site_created, after_site_user_created # Noqa F401
1 change: 1 addition & 0 deletions src/pki/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .site_created import after_site_created
from .site_user_created import after_site_user_created
2 changes: 1 addition & 1 deletion src/pki/signals/site_created.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@receiver(models.signals.post_save, sender=Site)
def after_site_created(_, instance: Site, created: bool, *args, **kwargs):
def after_site_created(sender, instance: Site, created: bool, *args, **kwargs): # noqa
if not created:
return

Expand Down
18 changes: 18 additions & 0 deletions src/pki/signals/site_user_created.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.conf import settings
from django.db import models
from django.dispatch import receiver

from pki.models import SiteUser, CertificateAuthority

import pki.services.certificate


@receiver(models.signals.post_save, sender=SiteUser)
def after_site_user_created(sender, instance: SiteUser, created: bool, *args, **kwargs): # noqa
if not created or not getattr(settings, 'GENERATE_CERT_ON_CREATE', False):
return

ca = CertificateAuthority.objects.filter(site_id=instance.site_id).first()

if ca:
pki.services.certificate.generate_cert_for_user(user=instance.user, ca=ca)

0 comments on commit 32a0718

Please sign in to comment.