oidc_provider: fix error trying to create RSA Key before migrations are run

This commit is contained in:
Jens Langhammer 2019-07-05 15:27:04 +02:00
parent 23d277eaf1
commit 8623a2c3fc
1 changed files with 11 additions and 7 deletions

View File

@ -2,6 +2,7 @@
from logging import getLogger from logging import getLogger
from django.apps import AppConfig from django.apps import AppConfig
from django.db.utils import InternalError, OperationalError, ProgrammingError
from django.urls import include, path from django.urls import include, path
LOGGER = getLogger(__name__) LOGGER = getLogger(__name__)
@ -14,13 +15,16 @@ class PassbookOIDCProviderConfig(AppConfig):
verbose_name = 'passbook OIDC Provider' verbose_name = 'passbook OIDC Provider'
def ready(self): def ready(self):
from Cryptodome.PublicKey import RSA try:
from oidc_provider.models import RSAKey from Cryptodome.PublicKey import RSA
if not RSAKey.objects.exists(): from oidc_provider.models import RSAKey
key = RSA.generate(2048) if not RSAKey.objects.exists():
rsakey = RSAKey(key=key.exportKey('PEM').decode('utf8')) key = RSA.generate(2048)
rsakey.save() rsakey = RSAKey(key=key.exportKey('PEM').decode('utf8'))
LOGGER.info("Created key") rsakey.save()
LOGGER.info("Created key")
except (OperationalError, ProgrammingError, InternalError):
pass
from passbook.root import urls from passbook.root import urls
urls.urlpatterns.append( urls.urlpatterns.append(
path('application/oidc/', include('oidc_provider.urls', namespace='oidc_provider')), path('application/oidc/', include('oidc_provider.urls', namespace='oidc_provider')),