# Copyright (c) 2017 Ansible, Inc.
# All Rights Reserved.

from social.strategies.django_strategy import DjangoStrategy


class AWXDjangoStrategy(DjangoStrategy):
    """A DjangoStrategy for python-social-auth containing
       fixes and updates from social-app-django

       TODO: Revert back to using the default DjangoStrategy after
       we upgrade to social-core / social-app-django. We will also
       want to ensure we update the SOCIAL_AUTH_STRATEGY setting.
    """

    def request_port(self):
        """Port in use for this request
           https://github.com/python-social-auth/social-app-django/blob/master/social_django/strategy.py#L76
        """
        try:  # django >= 1.9
            return self.request.get_port()
        except AttributeError:  # django < 1.9
            host_parts = self.request.get_host().split(':')
            try:
                return host_parts[1]
            except IndexError:
                return self.request.META['SERVER_PORT']

# Copyright (c) 2017 Ansible, Inc.
# All Rights Reserved.

# Django
from django.conf import settings

# Python social auth
from social.strategies.django_strategy import DjangoStrategy


class AWXDjangoStrategy(DjangoStrategy):
    """A DjangoStrategy for python-social-auth containing
       fixes and updates from social-app-django

       TODO: Revert back to using the default DjangoStrategy after
       we upgrade to social-core / social-app-django and upgrade Django
       to 1.9 and above. We will also want to ensure we update the
       SOCIAL_AUTH_STRATEGY setting.
    """

    def request_port(self):
        """Port in use for this request
           https://github.com/python-social-auth/social-app-django/blob/master/social_django/strategy.py#L76
        """
        try:  # django >= 1.9
            return self.request.get_port()
        except AttributeError:  # django < 1.9
            host_parts = self.request.get_host().split(':')
            try:
                return host_parts[1]
            except IndexError:
                if settings.USE_X_FORWARDED_PORT and 'HTTP_X_FORWARDED_PORT' in self.request.META:
                    return self.request.META['HTTP_X_FORWARDED_PORT']
                else:
                    return self.request.META['SERVER_PORT']

Fix SAML auth behind load balancer issue.
Fix SAML auth behind load balancer issue.

Relates to #7586 of ansible-tower as a follow-up of fix #420 of tower.

The original fix works for Django version 1.9 and above, this PR
expanded the solution to Django verison 1.8 and below.

Signed-off-by: Aaron Tan <555f4d7ad24fb44b9d01fe2d4954b2d18e7bdbd4@gmail.com>
