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

✨(backend) add option to configure list of required OIDC claims #525

Merged
merged 1 commit into from
Dec 24, 2024

Conversation

sampaccoud
Copy link
Member

Purpose

We want to be able to refuse connection for users who have missing claims from a list of required keys.

Proposal

Add option to configure list of required OIDC claims: USER_OIDC_REQUIRED_CLAIMS.
Defaults to an empty list.

@sampaccoud sampaccoud self-assigned this Dec 21, 2024
@sampaccoud sampaccoud added python Pull requests that update Python code backend enhancement New feature or request labels Dec 21, 2024
We want to be able to refuse connection for users who have missing
claims from a list of required keys.
@sampaccoud sampaccoud force-pushed the add-setting-to-make-oidc-claims-required branch from 67d1163 to dca001e Compare December 24, 2024 16:01
@sampaccoud sampaccoud enabled auto-merge (rebase) December 24, 2024 16:01
@sampaccoud sampaccoud merged commit c879f82 into main Dec 24, 2024
15 checks passed
@sampaccoud sampaccoud deleted the add-setting-to-make-oidc-claims-required branch December 24, 2024 16:10
Comment on lines +60 to +71
# Validate required claims
missing_claims = [
claim
for claim in settings.USER_OIDC_REQUIRED_CLAIMS
if claim not in userinfo
]
if missing_claims:
raise SuspiciousOperation(
_("Missing required claims in user info: %(claims)s")
% {"claims": ", ".join(missing_claims)}
)

Copy link
Collaborator

@lebaudantoine lebaudantoine Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to the spec, these claims should be referred as essential claims. Please take a look here https://openid.net/specs/openid-connect-core-1_0.html

Essential Claim
Claim specified by the Client as being necessary to ensure a smooth authorization experience for the specific task requested by the End-User.

Copy link
Collaborator

@lebaudantoine lebaudantoine Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might prefer to override the base method, and call it in get_or_create_user, as proposed in the initial implementation. get_userinfo should have a single responsibility, getting the data.

    def verify_claims(self, claims):
        """Verify the provided claims to decide if authentication should be allowed."""

        # Verify claims required by default configuration
        scopes = self.get_settings("OIDC_RP_SCOPES", "openid email")
        if "email" in scopes.split():
            return "email" in claims

        LOGGER.warning(
            "Custom OIDC_RP_SCOPES defined. "
            "You need to override `verify_claims` for custom claims verification."
        )

        return True

https://github.com/mozilla/mozilla-django-oidc/blob/2c2334fdc9b2fc72a492b5f0e990b4c30de68363/mozilla_django_oidc/auth.py#L84

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capture d’écran 2024-12-27 à 16 40 05

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! 🙏
Note that checking the presence of the "sub" claim was already wrong then 🤔
Let's fix that before it is released!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lebaudantoine Here you go: #531

Copy link
Collaborator

@lebaudantoine lebaudantoine Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of, checking the presence of "sub" was naive and simple.
As soon as we introduce more complexity (a list check, a new setting, …) I would stick with the Mozilla django oidc philosophy

@AntoLC AntoLC mentioned this pull request Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend enhancement New feature or request python Pull requests that update Python code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants