Esempio n. 1
0
    def test_get_github_user_token(self):
        """Test the github utility get_github_user_token method."""
        data = {
            'access_token': self.user_oauth_token,
            'scope': 'read:user,user:email'
        }
        params = {
            'code': self.callback_code,
            'client_id': settings.GITHUB_CLIENT_ID,
            'client_secret': settings.GITHUB_CLIENT_SECRET,
        }
        params = urlencode(params, quote_via=quote_plus)
        url = settings.GITHUB_TOKEN_URL + '?' + params
        responses.add(responses.GET,
                      settings.GITHUB_TOKEN_URL,
                      json=data,
                      headers=JSON_HEADER,
                      status=200)
        responses.add(responses.GET,
                      settings.GITHUB_TOKEN_URL,
                      json={},
                      headers=JSON_HEADER,
                      status=200)
        result = get_github_user_token(self.callback_code)
        result_no_token = get_github_user_token(self.callback_code)

        assert responses.calls[0].request.url == url
        assert responses.calls[1].request.url == url
        assert result == self.user_oauth_token
        assert result_no_token is None
Esempio n. 2
0
    def test_get_github_user_token(self):
        """Test the github utility get_github_user_token method."""
        data = {'access_token': self.user_oauth_token, 'scope': 'read:user,user:email'}
        params = {
            'code': self.callback_code,
            'client_id': settings.GITHUB_CLIENT_ID,
            'client_secret': settings.GITHUB_CLIENT_SECRET,
        }
        params = urlencode(params, quote_via=quote_plus)
        url = settings.GITHUB_TOKEN_URL + '?' + params
        responses.add(responses.GET, settings.GITHUB_TOKEN_URL,
                      json=data, headers=JSON_HEADER, status=200)
        responses.add(responses.GET, settings.GITHUB_TOKEN_URL,
                      json={}, headers=JSON_HEADER, status=200)
        result = get_github_user_token(self.callback_code)
        result_no_token = get_github_user_token(self.callback_code)

        assert responses.calls[0].request.url == url
        assert responses.calls[1].request.url == url
        assert result == self.user_oauth_token
        assert result_no_token is None
Esempio n. 3
0
def github_callback(request):
    """Handle the Github authentication callback."""
    # Get request parameters to handle authentication and the redirect.
    code = request.GET.get('code', None)
    redirect_uri = request.GET.get('redirect_uri')

    if not code or not redirect_uri:
        raise Http404

    # Get OAuth token and github user data.
    access_token = get_github_user_token(code)
    github_user_data = get_github_user_data(access_token)
    handle = github_user_data.get('login')
    ip_address = '24.210.224.38' if settings.DEBUG else get_real_ip(request)
    geolocation_data = {}

    if ip_address:
        geolocation_data = get_location_from_ip(ip_address)

    if handle:
        # Create or update the Profile with the github user data.
        user_profile, _ = Profile.objects.update_or_create(
            handle=handle,
            defaults={
                'data': github_user_data or {},
                'email': get_github_primary_email(access_token),
                'github_access_token': access_token
            })

        # Update the user's session with handle and email info.
        session_data = {
            'handle': user_profile.handle,
            'email': user_profile.email,
            'access_token': user_profile.github_access_token,
            'profile_id': user_profile.pk,
            'name': user_profile.data.get('name', None),
            'access_token_last_validated': timezone.now().isoformat(),
        }
        for k, v in session_data.items():
            request.session[k] = v

        # record a useraction for this
        UserAction.objects.create(profile=user_profile,
                                  action='Login',
                                  metadata={},
                                  ip_address=ip_address,
                                  location_data=geolocation_data)

    response = redirect(redirect_uri)
    response.set_cookie('last_github_auth_mutation', int(time.time()))
    return response
Esempio n. 4
0
def github_callback(request):
    """Handle the Github authentication callback."""
    # Get request parameters to handle authentication and the redirect.
    code = request.GET.get('code', None)
    redirect_uri = request.GET.get('redirect_uri')

    if not code or not redirect_uri:
        raise Http404

    # Get OAuth token and github user data.
    access_token = get_github_user_token(code)
    github_user_data = get_github_user_data(access_token)
    handle = github_user_data.get('login')
    github_repos = get_github_repos(access_token)

    if handle:
        # Create or update the Profile with the github user data.
        # user_profile, _ = Profile.objects.update_or_create(
        #     handle=handle,
        #     defaults={
        #         'data': github_user_data or {},
        #         'email': get_github_primary_email(access_token),
        #         'github_access_token': access_token
        #     })

        # Update the user's session with handle and email info.
        session_data = {
            'handle': handle,
            'user_repos': github_repos,
            'email': get_github_primary_email(access_token),
            'access_token': access_token,
            'name': github_user_data.get('name', None),
            'access_token_last_validated': timezone.now().isoformat(),
        }
        for k, v in session_data.items():
            request.session[k] = v

        # # record a useraction for this
        # UserAction.objects.create(
        #     profile=user_profile,
        #     action='Login',
        #     metadata={},
        #     )


    response = redirect(redirect_uri)
    response.set_cookie('last_github_auth_mutation', int(time.time()))
    return response
Esempio n. 5
0
def github_callback(request):
    """Handle the Github authentication callback."""
    # Get request parameters to handle authentication and the redirect.
    code = request.GET.get('code', None)
    redirect_uri = request.GET.get('redirect_uri')

    if not code or not redirect_uri:
        raise Http404

    # Get OAuth token and github user data.
    access_token = get_github_user_token(code)
    github_user_data = get_github_user_data(access_token)
    handle = github_user_data.get('login')
    github_repos = get_github_repos(access_token)

    if handle:
        # Create or update the Profile with the github user data.
        # user_profile, _ = Profile.objects.update_or_create(
        #     handle=handle,
        #     defaults={
        #         'data': github_user_data or {},
        #         'email': get_github_primary_email(access_token),
        #         'github_access_token': access_token
        #     })

        # Update the user's session with handle and email info.
        session_data = {
            'handle': handle,
            'user_repos': github_repos,
            'email': get_github_primary_email(access_token),
            'access_token': access_token,
            'name': github_user_data.get('name', None),
            'access_token_last_validated': timezone.now().isoformat(),
        }
        for k, v in session_data.items():
            request.session[k] = v

        # # record a useraction for this
        # UserAction.objects.create(
        #     profile=user_profile,
        #     action='Login',
        #     metadata={},
        #     )

    response = redirect(redirect_uri)
    response.set_cookie('last_github_auth_mutation', int(time.time()))
    return response
Esempio n. 6
0
def github_callback(request):
    """Handle the Github authentication callback."""
    # Get request parameters to handle authentication and the redirect.
    code = request.GET.get('code', None)
    redirect_uri = request.GET.get('redirect_uri')

    if not code or not redirect_uri:
        raise Http404

    # Get OAuth token and github user data.
    access_token = get_github_user_token(code)
    github_user_data = get_github_user_data(access_token)
    handle = github_user_data.get('login')

    if handle:
        # Create or update the Profile with the github user data.
        user_profile, _ = Profile.objects.update_or_create(
            handle=handle,
            defaults={
                'data': github_user_data or {},
                'email': get_github_primary_email(access_token),
                'github_access_token': access_token
            })

        # Update the user's session with handle and email info.
        session_data = {
            'handle': user_profile.handle,
            'email': user_profile.email,
            'access_token': user_profile.github_access_token,
            'profile_id': user_profile.pk,
            'access_token_last_validated': timezone.now().isoformat(),
        }
        for k, v in session_data.items():
            request.session[k] = v

    response = redirect(redirect_uri)
    response.set_cookie('last_github_auth_mutation', int(time.time()))
    return response