~roadmr/canonical-identity-provider/u2f-db-fields

« back to all changes in this revision

Viewing changes to src/ubuntu_sso_saml/processors.py

  • Committer: Ubuntu One Auto Copilot
  • Author(s): Daniel Manrique
  • Date: 2017-10-26 17:47:22 UTC
  • mfrom: (1583.1.3 fix-saml-team-snafu)
  • Revision ID: otto-copilot@canonical.com-20171026174722-lx78ifbiyljqn6cb
Fix the "'AnonymousUser' object has no attribute 'person_in_any_team'" oops when a non-logged-in user tries to access a SAML remote with group restrictions.

This was fixed at Ricardo's suggestion by moving the group membership test to a _validate_user method (which is actually what django_saml2idp recommends, had I bothered to read the documentation), and it's my understanding in this method one should *never* get a non-logged-in user. But I left the check that protects against a User not having person_in_any_team anyway.

The test I wrote with the user checks in _validate_request reproduced the oops perfectly, even if moving it to _validate_user later changed the behavior (sending the user to the login page).


Merged from https://code.launchpad.net/~roadmr/canonical-identity-provider/fix-saml-team-snafu/+merge/332868

Show diffs side-by-side

added added

removed removed

Lines of Context:
331
331
        acs_url = self._request_params['ACS_URL']
332
332
        sp_config = self.get_config()
333
333
        if sp_config is None:
334
 
            msg = "Could not find saml config for ACS url '{}'."
 
334
            msg = "Could not find saml config for ACS URL '{}'."
335
335
            raise CannotHandleAssertion(msg.format(acs_url))
336
336
        # validate request domain
337
337
        acs_domain = sp_config.acs_domain
341
341
        if not (is_secure and is_valid_domain):
342
342
            msg = "AssertionConsumerService is not a {} URL."
343
343
            raise CannotHandleAssertion(msg.format(acs_domain))
 
344
 
 
345
        # success, so keep a reference to the config
 
346
        self._sp_config = sp_config
 
347
 
 
348
    def _validate_user(self):
 
349
        acs_url = self._request_params['ACS_URL']
 
350
        sp_config = self.get_config()
 
351
        if sp_config is None:
 
352
            msg = "Could not find saml config for ACS url '{}'."
 
353
            raise CannotHandleAssertion(msg.format(acs_url))
344
354
        # Check for team allowance/restriction.
345
355
        user = self._django_request.user
346
356
        allow_team_list = sp_config.get_allowed_teams_list()
347
357
        if allow_team_list and not user.person_in_any_team(allow_team_list):
348
358
            msg = "User not allowed to access ACS URL {}."
349
 
            raise CannotHandleAssertion(msg.format(acs_domain))
350
 
 
351
 
        # success, so keep a reference to the config
352
 
        self._sp_config = sp_config
 
359
            raise CannotHandleAssertion(msg.format(acs_url))
353
360
 
354
361
    # internal implementation
355
362