Beispiel #1
0
    def test_is_vouched(self, rget):
        """Test a user with vouched status"""

        def mocked_get(url, **options):
            if 'vouched' in url:
                return MozillianResponse(VOUCHED_MOZILLIAN)
            if 'not_vouched' in url:
                return MozillianResponse(NOT_VOUCHED_MOZILLIAN)
            if 'trouble' in url:
                return MozillianResponse('Failed', status_code=500)

        rget.side_effect = mocked_get

        ok_(mozillians.is_vouched('*****@*****.**'))
        ok_(not mozillians.is_vouched('*****@*****.**'))

        self.assertRaises(
            mozillians.BadStatusCodeError,
            mozillians.is_vouched,
            '*****@*****.**')

        try:
            mozillians.is_vouched('*****@*****.**')
            raise
        except mozillians.BadStatusCodeError, msg:
            ok_(settings.MOZILLIANS_API_KEY not in str(msg))
Beispiel #2
0
    def test_is_vouched(self, rget):
        """Test a user with vouched status"""

        def mocked_get(url, **options):
            if 'vouched' in url:
                return MozillianResponse(VOUCHED_MOZILLIAN)
            if 'not_vouched' in url:
                return MozillianResponse(NOT_VOUCHED_MOZILLIAN)
            if 'trouble' in url:
                return MozillianResponse('Failed', status_code=500)
        rget.side_effect = mocked_get

        ok_(mozillians.is_vouched('*****@*****.**'))
        ok_(not mozillians.is_vouched('*****@*****.**'))

        self.assertRaises(
            mozillians.BadStatusCodeError,
            mozillians.is_vouched,
            '*****@*****.**')

        try:
            mozillians.is_vouched('*****@*****.**')
            raise
        except mozillians.BadStatusCodeError, msg:
            ok_(settings.MOZILLIANS_API_KEY not in str(msg))
Beispiel #3
0
    def create_user(self, claims):
        """ Override create_user method to anonymize user data
        based on privacy settings in mozillians.org.
        """
        email = claims.get('email')
        try:
            data = is_vouched(email)
        except BadStatusCodeError:
            data = None

        # Add vouched mozillians to our db please.
        if data and data['is_vouched']:
            user = User.objects.create_user(username=USERNAME_ALGO(email),
                                            email=email)
            # Due to privacy settings, this might be missing
            if 'full_name' not in data:
                data['full_name'] = 'Anonymous Mozillian'
            else:
                user.userprofile.mozillian_username = data['username']
                user.userprofile.save()

            first_name, last_name = (data['full_name'].split(' ', 1)
                                     if ' ' in data['full_name']
                                     else ('', data['full_name']))
            user.first_name = first_name
            user.last_name = last_name
            user.save()
            user.groups.add(Group.objects.get(name='Mozillians'))
            return user
Beispiel #4
0
    def create_user(self, email):
        try:
            data = is_vouched(email)
        except BadStatusCodeError:
            data = None

        # Add vouched mozillians to our db please.
        if data and data['is_vouched']:
            user = User.objects.create_user(username=USERNAME_ALGO(email),
                                            email=email)
            # Due to privacy settings, this might be missing
            if 'full_name' not in data:
                data['full_name'] = 'Anonymous Mozillian'
            else:
                user.userprofile.mozillian_username = data['username']
                user.userprofile.save()

            first_name, last_name = (data['full_name'].split(' ', 1)
                                     if ' ' in data['full_name'] else
                                     ('', data['full_name']))
            user.first_name = first_name
            user.last_name = last_name
            user.save()
            user.groups.add(Group.objects.get(name='Mozillians'))
            return user
Beispiel #5
0
    def form_valid(self, form):
        """
        Custom BrowserID verifier for ReMo users
        and vouched mozillians.
        """
        self.assertion = form.cleaned_data['assertion']
        self.audience = get_audience(self.request)
        result = verify(self.assertion, self.audience)
        _is_valid_login = False

        if result:
            if User.objects.filter(email=result['email']).exists():
                _is_valid_login = True
            else:
                try:
                    data = is_vouched(result['email'])
                except BadStatusCodeError:
                    msg = ('Email (%s) authenticated but unable to '
                           'connect to Mozillians to see if you are vouched' %
                           result['email'])
                    return self.login_failure(message=msg)

                if data and data['is_vouched']:
                    _is_valid_login = True
                    user = User.objects.create_user(
                        username=USERNAME_ALGO(data['email']),
                        email=data['email'])
                    # Due to privacy settings, this might be missing
                    if 'full_name' not in data:
                        data['full_name'] = 'Anonymous Mozillian'
                    else:
                        user.userprofile.mozillian_username = data['username']
                        user.userprofile.save()

                    first_name, last_name = (
                        data['full_name'].split(' ', 1)
                        if ' ' in data['full_name']
                        else ('', data['full_name']))
                    user.first_name = first_name
                    user.last_name = last_name
                    user.save()
                    user.groups.add(
                        Group.objects.get(name='Mozillians'))

            if _is_valid_login:
                try:
                    self.user = auth.authenticate(assertion=self.assertion,
                                                  audience=self.audience)
                    auth.login(self.request, self.user)
                except BrowserIDException as e:
                    return self.login_failure(error=e)

                if self.request.user and self.request.user.is_active:
                    return self.login_success()

        return self.login_failure()
Beispiel #6
0
    def form_valid(self, form):
        """
        Custom BrowserID verifier for ReMo users
        and vouched mozillians.
        """
        self.assertion = form.cleaned_data['assertion']
        self.audience = get_audience(self.request)
        result = verify(self.assertion, self.audience)
        _is_valid_login = False

        if result:
            if User.objects.filter(email=result['email']).exists():
                _is_valid_login = True
            else:
                try:
                    data = is_vouched(result['email'])
                except BadStatusCodeError:
                    msg = ('Email (%s) authenticated but unable to '
                           'connect to Mozillians to see if you are vouched' %
                           result['email'])
                    return self.login_failure(message=msg)

                if data and data['is_vouched']:
                    _is_valid_login = True
                    user = User.objects.create_user(username=USERNAME_ALGO(
                        data['email']),
                                                    email=data['email'])
                    # Due to privacy settings, this might be missing
                    if not 'full_name' in data:
                        data['full_name'] = 'Anonymous Mozillian'
                    else:
                        user.userprofile.mozillian_username = data['username']
                        user.userprofile.save()

                    first_name, last_name = (data['full_name'].split(' ', 1)
                                             if ' ' in data['full_name'] else
                                             ('', data['full_name']))
                    user.first_name = first_name
                    user.last_name = last_name
                    user.save()
                    user.groups.add(Group.objects.get(name='Mozillians'))

            if _is_valid_login:
                try:
                    self.user = auth.authenticate(assertion=self.assertion,
                                                  audience=self.audience)
                    auth.login(self.request, self.user)
                except BrowserIDException as e:
                    return self.login_failure(error=e)

                if self.request.user and self.request.user.is_active:
                    return self.login_success()

        return self.login_failure()
Beispiel #7
0
    def test_is_vouched(self, rget):
        def mocked_get(url, **options):
            if "vouched" in url:
                return MozillianResponse(VOUCHED_MOZILLIAN)
            if "not_vouched" in url:
                return MozillianResponse(NOT_VOUCHED_MOZILLIAN)
            if "trouble" in url:
                return MozillianResponse("Failed", status_code=500)
            raise NotImplementedError(url)

        rget.side_effect = mocked_get

        ok_(mozillians.is_vouched("*****@*****.**"))
        ok_(not mozillians.is_vouched("*****@*****.**"))

        self.assertRaises(mozillians.BadStatusCodeError, mozillians.is_vouched, "*****@*****.**")

        try:
            mozillians.is_vouched("*****@*****.**")
            raise
        except mozillians.BadStatusCodeError, msg:
            ok_(settings.MOZILLIANS_API_KEY not in str(msg))
Beispiel #8
0
def mozilla_browserid_verify(request):
    """
    Custom BrowserID verifier for ReMo users
    and vouched mozillians.
    """
    form = BrowserIDForm(request.POST)
    if form.is_valid():
        assertion = form.cleaned_data['assertion']
        audience = get_audience(request)
        result = verify(assertion, audience)
        try:
            _is_valid_login = False
            if result:
                if User.objects.filter(email=result['email']).exists():
                    _is_valid_login = True
                else:
                    data = is_vouched(result['email'])
                    if data and data['is_vouched']:
                        _is_valid_login = True
                        user = User.objects.create_user(
                            username=USERNAME_ALGO(data['email']),
                            email=data['email'])

                        first_name, last_name = (
                            data['full_name'].split(' ', 1)
                            if ' ' in data['full_name']
                            else ('', data['full_name']))
                        user.first_name = first_name
                        user.last_name = last_name
                        user.save()
                        user.groups.add(Group.objects.get(name='Mozillians'))

            if _is_valid_login:
                user = auth.authenticate(assertion=assertion,
                                         audience=audience)
                auth.login(request, user)
                return redirect('dashboard')

        except BadStatusCodeError:
            message = ('Email (%s) authenticated but unable to '
                       'connect to Mozillians to see if you are vouched' %
                       result['email'])
            return login_failed(request, message)

    return redirect(settings.LOGIN_REDIRECT_URL_FAILURE)
Beispiel #9
0
def check_mozillian_username():
    mozillians = User.objects.filter(groups__name="Mozillians")

    for user in mozillians:
        data = is_vouched(user.email)
        if data and data["is_vouched"] and "full_name" in data:
            first_name, last_name = (
                data["full_name"].split(" ", 1) if " " in data["full_name"] else ("", data["full_name"])
            )
            user.first_name = first_name
            user.last_name = last_name
            user.userprofile.mozillian_username = data["username"]
        else:
            user.first_name = "Anonymous"
            user.last_name = "Mozillian"
            user.userprofile.mozillian_username = ""
        user.save()
        user.userprofile.save()
Beispiel #10
0
def check_mozillian_username():
    mozillians = User.objects.filter(groups__name='Mozillians')

    for user in mozillians:
        data = is_vouched(user.email)
        if data and data['is_vouched'] and 'full_name' in data:
            first_name, last_name = (data['full_name'].split(' ', 1)
                                     if ' ' in data['full_name'] else
                                     ('', data['full_name']))
            user.first_name = first_name
            user.last_name = last_name
            user.userprofile.mozillian_username = data['username']
        else:
            user.first_name = 'Anonymous'
            user.last_name = 'Mozillian'
            user.userprofile.mozillian_username = ''
        user.save()
        user.userprofile.save()
Beispiel #11
0
def mozilla_browserid_verify(request):
    """
    Custom BrowserID verifier for ReMo users
    and vouched mozillians.
    """
    form = BrowserIDForm(request.POST)
    if form.is_valid():
        assertion = form.cleaned_data['assertion']
        audience = get_audience(request)
        result = verify(assertion, audience)
        try:
            _is_valid_login = False
            if result:
                if User.objects.filter(email=result['email']).exists():
                    _is_valid_login = True
                else:
                    data = is_vouched(result['email'])
                    if data and data['is_vouched']:
                        _is_valid_login = True
                        user = User.objects.create_user(username=USERNAME_ALGO(
                            data['email']),
                                                        email=data['email'])

                        first_name, last_name = (data['full_name'].split(
                            ' ', 1) if ' ' in data['full_name'] else
                                                 ('', data['full_name']))
                        user.first_name = first_name
                        user.last_name = last_name
                        user.save()
                        user.groups.add(Group.objects.get(name='Mozillians'))

            if _is_valid_login:
                user = auth.authenticate(assertion=assertion,
                                         audience=audience)
                auth.login(request, user)
                return redirect('dashboard')

        except BadStatusCodeError:
            message = ('Email (%s) authenticated but unable to '
                       'connect to Mozillians to see if you are vouched' %
                       result['email'])
            return login_failed(request, message)

    return redirect(settings.LOGIN_REDIRECT_URL_FAILURE)
Beispiel #12
0
def check_mozillian_username():
    mozillians = User.objects.filter(groups__name='Mozillians')

    for user in mozillians:
        data = is_vouched(user.email)
        if data and data['is_vouched'] and 'full_name' in data:
            first_name, last_name = (
                data['full_name'].split(' ', 1)
                if ' ' in data['full_name']
                else ('', data['full_name']))
            user.first_name = first_name
            user.last_name = last_name
            user.userprofile.mozillian_username = data['username']
        else:
            user.first_name = 'Anonymous'
            user.last_name = 'Mozillian'
            user.userprofile.mozillian_username = ''
        user.save()
        user.userprofile.save()