Ejemplo n.º 1
0
    def test_delete_provider(self):
        """test that after a provider account deleted SocialAccounts must remain with their provider field == 'sentinel'
        while other SocialAccounts must remain intact"""
        from social.views import create_social_create_email_user
        _, socialAccount_1 = create_social_create_email_user(
            self.userId, self.email,
            SocialProvider.objects.get(social=SocialProvider.GOOGLE),
            self.is_email_verified)

        _, socialAccount_2 = create_social_create_email_user(
            self.userId_2, self.email_2,
            SocialProvider.objects.get(social=SocialProvider.GITHUB),
            self.is_email_verified)

        self.assertEqual(socialAccount_1.provider.social,
                         SocialProvider.GOOGLE)
        self.assertEqual(socialAccount_2.provider.social,
                         SocialProvider.GITHUB)

        SocialProvider.objects.get(social=SocialProvider.GOOGLE).delete()

        socialAccount_1.refresh_from_db()
        socialAccount_2.refresh_from_db()
        self.assertEqual(socialAccount_1.provider.social,
                         SocialProvider.SENTINEL)
        self.assertEqual(socialAccount_2.provider.social,
                         SocialProvider.GITHUB)
Ejemplo n.º 2
0
 def post(self, request):
     if super().get_id_info(request):
         userId = self.idinfo.get('sub', None)
         email = self.idinfo.get('email', None)
         isEmailVerified = self.idinfo.get('email_verified', None)
         try:
             from social.views import create_social_create_email_user
             create_social_create_email_user(
                 userId, email,
                 SocialProvider.objects.get(social=SocialProvider.GOOGLE),
                 isEmailVerified)
         except ValueError as err:
             print(err)
             return JsonResponse(
                 {
                     'status': 'false',
                     'error': 'already exist'
                 }, status=400)
     else:
         return JsonResponse(
             {
                 'status': 'false',
                 'error': 'denied by google'
             }, status=400)
     return JsonResponse({
         'status': 'ok',
     }, status=200)
Ejemplo n.º 3
0
    def test_create_social(self):
        """Test sign up with social account"""
        socialAccount = SocialAccount.objects.filter(
            social_provider_identifier=self.userId,
            provider=SocialProvider.objects.get(social=SocialProvider.GOOGLE),
            site=Site.objects.get_current())
        account_count = socialAccount.count()
        # if the google id has an account log in the user
        if account_count == 1:
            # there should be only one result back
            socialAccount = socialAccount[0]
        elif account_count == 0:
            try:
                from social.views import create_social_create_email_user
                emailUser, socialAccount = create_social_create_email_user(
                    self.userId, self.email,
                    SocialProvider.objects.get(social=SocialProvider.GOOGLE),
                    self.is_email_verified)
            except ValueError:
                pass
        else:
            """this is unexpected"""
            # server error occur now

        # log in the account
        self.client.force_login(socialAccount.user)

        self.assertEqual(socialAccount.email, self.email)
        self.assertEqual(socialAccount.social_provider_identifier, self.userId)
        self.assertEqual(socialAccount.site, Site.objects.get_current())
        self.assertEqual(socialAccount.provider.social, SocialProvider.GOOGLE)
        self.assertTrue(socialAccount.is_connected)
        self.assertEqual(socialAccount.user.is_email_verified,
                         self.is_email_verified)
        self.assertTrue(socialAccount.user.is_authenticated)
Ejemplo n.º 4
0
def google_callback_signup(request):

    if 'access_denied' == request.GET.get('error', ''):
        return redirect('google_error')

    state = request.GET.get('state', '')
    code = request.GET.get('code', '')
    redirect_uri = request.GET.get('redirect_uri',
                                   request.build_absolute_uri('?'))
    import google_auth_oauthlib.flow
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        GOOGLE_CLIENT_FILE_PATH, SCOPES, state=state)
    flow.redirect_uri = redirect_uri
    try:
        flow.fetch_token(code=code)
    except Exception as err:
        print("google_callback_signup: ", err)
        print("Unexpected error: ", sys.exc_info()[0])
        return redirect('google_error')

    # obligates https
    # authorization_response = request.build_absolute_uri()
    # flow.fetch_token(authorization_response=authorization_response)

    try:
        idinfo = authorize_by_google_api_profile(
            flow.credentials.client_id,
            flow.credentials.id_token,
        )
        userId = idinfo['sub']
        email = idinfo['email']
        isEmailVerified = idinfo['email_verified']
    except ValueError as err:
        print('google_callback_signup: ', err)
        return redirect('google_error')

    try:
        create_social_create_email_user(
            userId, email,
            SocialProvider.objects.get(social=SocialProvider.GOOGLE),
            isEmailVerified)
    except ValueError as err:
        print(err)
        return redirect('google_error')

    return redirect('FLEX:index', )
Ejemplo n.º 5
0
def google_callback_login_signup(request):
    """
    @param request:
    @return:

    if Google id is associated with a user log in the user
    if Google id is NOT associated with a user make the user and social account then  log in the user
    """
    if 'access_denied' == request.GET.get('error', ''):
        return redirect('google_error')

    state = request.GET.get('state', '')
    code = request.GET.get('code', '')
    redirect_uri = request.GET.get('redirect_uri', request.build_absolute_uri('?'))
    import google_auth_oauthlib.flow
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        GOOGLE_CLIENT_FILE_PATH,
        SCOPES,
        state=state)
    flow.redirect_uri = redirect_uri
    try:
        flow.fetch_token(code=code)
    except Exception as err:
        print("Exception:", err)
        return redirect('google_error')

    try:
        idinfo = authorize_by_google_api_profile(flow.credentials.client_id, flow.credentials.id_token, )
        userId = idinfo['sub']
        email = idinfo['email']
        isEmailVerified = idinfo['email_verified']
    except ValueError as err:
        print('google_callback_signup: ', err)
        return redirect('google_error')

    account = SocialAccount.objects.filter(social_id=userId,
                                           provider=SocialProvider.objects.get(social=SocialProvider.GOOGLE),
                                           site=Site.objects.get_current())

    # if the google id has an account log in the user
    if account:
        # there should be only one result back
        account = account[0]
        # LogIn the user with the social account
        login(request, account.user, )
        return redirect('google', )
    else:
        try:
            from social.views import create_social_create_email_user
            create_social_create_email_user(userId, email, SocialProvider.objects.get(social=SocialProvider.GOOGLE),
                                            isEmailVerified)
        except ValueError as err:
            print('google_callback_login_signup', err)
            return redirect('google_error')

        account = SocialAccount.objects.get(social_id=userId,
                                            provider=SocialProvider.objects.get(social=SocialProvider.GOOGLE),
                                            site=Site.objects.get_current())
        login(request, account.user, )

        return redirect('google', )
Ejemplo n.º 6
0
def google_callback_login_signup(request):
    """
    @param request:
    @return:

    if Google id is associated with a user log in the user
    if Google id is NOT associated with a user make the user and social account then  log in the user
    """
    if 'access_denied' == request.GET.get('error', ''):
        return redirect('google_error')

    state = request.GET.get('state', '')
    # code = request.GET.get('code', '')
    redirect_uri = request.GET.get('redirect_uri',
                                   request.build_absolute_uri('?'))
    flow = Flow.from_client_secrets_file(GOOGLE_CLIENT_FILE_PATH,
                                         SCOPES,
                                         state=state)
    flow.redirect_uri = redirect_uri

    authorization_response = request.get_raw_uri()

    try:
        # flow.fetch_token(code=code)
        flow.fetch_token(authorization_response=authorization_response)
    except Exception as err:
        print("google fetch_token error:", err)
        return redirect('google_error')

    try:
        idinfo = authorize_by_google_api_profile(
            flow.credentials.client_id,
            flow.credentials.id_token,
        )
        userId = idinfo['sub']
        email = idinfo['email']
        isEmailVerified = idinfo['email_verified']
    except ValueError as err:
        print('google_callback_signup: ', err)
        return redirect('google_error')

    account = SocialAccount.objects.filter(
        social_id=userId,
        provider_id=GOOGLE_SOCIAL_PROVIDER_ID,
        site_id=request.site.id).select_related('user').first()

    # if the google id has an account then -> log in the user
    if account and account.user:
        # LogIn the user with the social account
        login(
            request,
            account.user,
        )
        return redirect('FLEX:index', )
    else:
        try:
            emailUser, socialAccount = create_social_create_email_user(
                userId,
                email,
                GOOGLE_SOCIAL_PROVIDER_ID,
                isEmailVerified,
            )
        except ValueError as err:
            print('google_callback_login_signup:', err)
            return redirect('google_error')

        # account = SocialAccount.objects.get(
        #     social_id=userId,
        #     provider_id=GOOGLE_SOCIAL_PROVIDER_ID,
        #     site=Site.objects.get_current(),
        # )

        login(
            request,
            emailUser,
        )

        return redirect('FLEX:index', )