Skip to content

Commit

Permalink
refactor(socialaccount): Move certificate_key into settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Oct 25, 2023
1 parent 5fe150b commit 104416b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
6 changes: 5 additions & 1 deletion allauth/socialaccount/adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import absolute_import

import warnings

from django.core.exceptions import (
ImproperlyConfigured,
MultipleObjectsReturned,
Expand Down Expand Up @@ -255,11 +257,13 @@ def list_apps(self, request, provider=None, client_id=None):
"client_id",
"secret",
"key",
"certificate_key",
"settings",
]:
if field in config:
setattr(app, field, config[field])
if "certificate_key" in config:
warnings.warn("'certificate_key' should be moved into app.settings")
app.settings["certificate_key"] = config["certificate_key"]
if client_id and app.client_id != client_id:
continue
if (
Expand Down
7 changes: 0 additions & 7 deletions allauth/socialaccount/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ class SocialApp(models.Model):
# blank=True allows for disabling apps without removing them
sites = models.ManyToManyField("sites.Site", blank=True)

# We want to move away from storing secrets in the database. So, we're
# putting a halt towards adding more fields for additional secrets, such as
# the certificate some providers need. Therefore, the certificate is not a
# DB backed field and can only be set using the ``APP`` configuration key
# in the provider settings.
certificate_key = None

class Meta:
verbose_name = _("social application")
verbose_name_plural = _("social applications")
Expand Down
5 changes: 3 additions & 2 deletions allauth/socialaccount/providers/apple/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def generate_client_secret(self):
app = get_adapter(self.request).get_app(self.request, "apple")
if not app.key:
raise ImproperlyConfigured("Apple 'key' missing")
if not app.certificate_key:
certificate_key = app.settings.get("certificate_key")
if not certificate_key:
raise ImproperlyConfigured("Apple 'certificate_key' missing")
claims = {
"iss": app.key,
Expand All @@ -50,7 +51,7 @@ def generate_client_secret(self):
}
headers = {"kid": self.consumer_secret, "alg": "ES256"}
client_secret = jwt_encode(
payload=claims, key=app.certificate_key, algorithm="ES256", headers=headers
payload=claims, key=certificate_key, algorithm="ES256", headers=headers
)
return client_secret

Expand Down
4 changes: 3 additions & 1 deletion allauth/socialaccount/providers/apple/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ def sign_id_token(payload):
"client_id": "app123id",
"key": "apple",
"secret": "dummy",
"certificate_key": """-----BEGIN PRIVATE KEY-----
"settings": {
"certificate_key": """-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg2+Eybl8ojH4wB30C
3/iDkpsrxuPfs3DZ+3nHNghBOpmhRANCAAQSpo1eQ+EpNgQQyQVs/F27dkq3gvAI
28m95JEk26v64YAea5NTH56mru30RDqTKPgRVi5qRu3XGyqy3mdb8gMy
-----END PRIVATE KEY-----
""",
},
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions docs/socialaccount/providers/apple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ Add the following configuration to your settings:
# Prefix in your App ID.
"key": "MEMAPPIDPREFIX",
# The certificate you downloaded when generating the key.
"certificate_key": """-----BEGIN PRIVATE KEY-----
"settings": {
# The certificate you downloaded when generating the key.
"certificate_key": """-----BEGIN PRIVATE KEY-----
s3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr
3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3cr3ts3
c3ts3cr3t
-----END PRIVATE KEY-----
"""
}
}
}
}
Expand Down

0 comments on commit 104416b

Please sign in to comment.