Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#45] Attempt at refactoring config class #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions mozilla_django_oidc_db/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@
from .models import OpenIDConnectConfig


class OpenIDConnectConfigForm(forms.ModelForm):
required_endpoints = [
"oidc_op_authorization_endpoint",
"oidc_op_token_endpoint",
"oidc_op_user_endpoint",
]
class CleanUrlsFormMixin:
required_endpoints = []
oidc_mapping = OIDC_MAPPING

class Meta:
model = OpenIDConnectConfig
fields = "__all__"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -65,3 +57,16 @@ def clean(self):
self.add_error(field, _("This field is required."))

return cleaned_data


class OpenIDConnectConfigForm(CleanUrlsFormMixin, forms.ModelForm):
required_endpoints = [
"oidc_op_authorization_endpoint",
"oidc_op_token_endpoint",
"oidc_op_user_endpoint",
]
oidc_mapping = OIDC_MAPPING

class Meta:
model = OpenIDConnectConfig
fields = "__all__"
61 changes: 35 additions & 26 deletions mozilla_django_oidc_db/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Dict, List

import django
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.postgres.fields import JSONField
Expand Down Expand Up @@ -103,30 +102,6 @@ class OpenIDConnectConfigBase(SingletonModel):
),
)

oidc_rp_client_id = models.CharField(
_("OpenID Connect client ID"),
max_length=1000,
help_text=_("OpenID Connect client ID provided by the OIDC Provider"),
)
oidc_rp_client_secret = models.CharField(
_("OpenID Connect secret"),
max_length=1000,
help_text=_("OpenID Connect secret provided by the OIDC Provider"),
)
oidc_rp_sign_algo = models.CharField(
_("OpenID sign algorithm"),
max_length=50,
help_text=_("Algorithm the Identity Provider uses to sign ID tokens"),
default="HS256",
)
oidc_rp_scopes_list = ArrayField(
verbose_name=_("OpenID Connect scopes"),
base_field=models.CharField(_("OpenID Connect scope"), max_length=50),
default=get_default_scopes,
blank=True,
help_text=_("OpenID Connect scopes that are requested during login"),
)

oidc_op_discovery_endpoint = models.URLField(
_("Discovery endpoint"),
max_length=1000,
Expand Down Expand Up @@ -170,6 +145,38 @@ class OpenIDConnectConfigBase(SingletonModel):
blank=True,
)

class Meta:
abstract = True

def __str__(self) -> str:
return force_text(self._meta.verbose_name)


class OpenIDConnectClientBaseConfig(models.Model):
oidc_rp_client_id = models.CharField(
_("OpenID Connect client ID"),
max_length=1000,
help_text=_("OpenID Connect client ID provided by the OIDC Provider"),
)
oidc_rp_client_secret = models.CharField(
_("OpenID Connect secret"),
max_length=1000,
help_text=_("OpenID Connect secret provided by the OIDC Provider"),
)
oidc_rp_sign_algo = models.CharField(
_("OpenID sign algorithm"),
max_length=50,
help_text=_("Algorithm the Identity Provider uses to sign ID tokens"),
default="HS256",
)
oidc_rp_scopes_list = ArrayField(
verbose_name=_("OpenID Connect scopes"),
base_field=models.CharField(_("OpenID Connect scope"), max_length=50),
default=get_default_scopes,
blank=True,
help_text=_("OpenID Connect scopes that are requested during login"),
)

@property
def oidc_rp_scopes(self) -> str:
"""
Expand All @@ -184,7 +191,9 @@ def __str__(self) -> str:
return force_text(self._meta.verbose_name)


class OpenIDConnectConfig(CachingMixin, OpenIDConnectConfigBase):
class OpenIDConnectConfig(
CachingMixin, OpenIDConnectClientBaseConfig, OpenIDConnectConfigBase
):
"""
Configuration for authentication/authorization via OpenID connect
"""
Expand Down