示例#1
0
文件: user.py 项目: tonytamsf/mygpo
    def get(self, request):

        if request.GET.get('error'):
            messages.error(request, _('Login failed.'))
            return HttpResponseRedirect(reverse('login'))

        code = request.GET.get('code')
        if not code:
            messages.error(request, _('Login failed.'))
            return HttpResponseRedirect(reverse('login'))

        flow = get_google_oauth_flow(request)

        try:
            credentials = flow.step2_exchange(code)
        except FlowExchangeError:
            messages.error(request,
                           _('Login with Google is currently not possible.'))
            logger.exception('Login with Google failed')
            return HttpResponseRedirect(reverse('login'))

        email = self._get_email(credentials.token_response)

        # Connect account
        if request.user.is_authenticated:
            request.user.google_email = email
            request.user.save()
            messages.success(
                request,
                _('Your account has been connected with '
                  '{google}. Open Settings to change this.'.format(
                      google=email)),
            )
            return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)

        # Check if Google account is connected
        User = get_user_model()
        try:
            user = User.objects.get(profile__google_email=email)

        except User.DoesNotExist:
            # Connect account
            messages.error(
                request,
                _('No account connected with your Google '
                  'account %s. Please log in to connect.' % email),
            )
            return HttpResponseRedirect('{login}?next={connect}'.format(
                login=reverse('login'), connect=reverse('login-google')))

        # Log in user
        # TODO: this should probably be replaced with a call to authenticate()
        # http://stackoverflow.com/questions/6034763/django-attributeerror-user-object-has-no-attribute-backend-but-it-does
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
        return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)
示例#2
0
文件: users.py 项目: Korvox/mygpo
    def get(self, request):

        if request.GET.get("error"):
            messages.error(request, _("Login failed."))
            return HttpResponseRedirect(reverse("login"))

        code = request.GET.get("code")
        if not code:
            messages.error(request, _("Login failed."))
            return HttpResponseRedirect(reverse("login"))

        flow = get_google_oauth_flow(request)

        try:
            credentials = flow.step2_exchange(code)
        except FlowExchangeError:
            messages.error(request, _("Login with Google is currently not possible."))
            logger.exception("Login with Google failed")
            return HttpResponseRedirect(reverse("login"))

        email = self._get_email(credentials.token_response)

        # Connect account
        if request.user.is_authenticated():
            request.user.google_email = email
            request.user.save()
            messages.success(
                request,
                _(
                    "Your account has been connected with "
                    "{google}. Open Settings to change this.".format(google=email)
                ),
            )
            return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)

        # Check if Google account is connected
        User = get_user_model()
        try:
            user = User.objects.get(profile__google_email=email)

        except User.DoesNotExist:
            # Connect account
            messages.error(
                request, _("No account connected with your Google " "account %s. Please log in to connect." % email)
            )
            return HttpResponseRedirect(
                "{login}?next={connect}".format(login=reverse("login"), connect=reverse("login-google"))
            )

        # Log in user
        # TODO: this should probably be replaced with a call to authenticate()
        # http://stackoverflow.com/questions/6034763/django-attributeerror-user-object-has-no-attribute-backend-but-it-does
        user.backend = "django.contrib.auth.backends.ModelBackend"
        login(request, user)
        return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)
示例#3
0
文件: users.py 项目: fk-lx/mygpo
    def get(self, request):

        if request.GET.get('error'):
            messages.error(request, _('Login failed.'))
            return HttpResponseRedirect(reverse('login'))

        code = request.GET.get('code')
        if not code:
            messages.error(request, _('Login failed.'))
            return HttpResponseRedirect(reverse('login'))

        flow = get_google_oauth_flow(request)

        try:
            credentials = flow.step2_exchange(code)
        except FlowExchangeError:
            messages.error(request, _('Login with Google is currently not possible.'))
            logger.exception('Login with Google failed')
            return HttpResponseRedirect(reverse('login'))

        email = credentials.token_response['id_token']['email']

        # Connect account
        if request.user.is_authenticated():
            set_users_google_email(request.user, email)
            messages.success(request, _('Your account has been connected with '
                    '{google}. Open Settings to change this.'.format(
                        google=email)))
            return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)

        # Check if Google account is connected
        user = user_by_google_email(email)

        if not user:
            # Connect account
            messages.error(request, _('No account connected with your Google '
                        'account %s. Please log in to connect.' % email))
            return HttpResponseRedirect('{login}?next={connect}'.format(
                login=reverse('login'), connect=reverse('login-google')))

        # Log in user
        login(request, user)
        return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT)
示例#4
0
 def get(self, request):
     flow = get_google_oauth_flow(request)
     auth_uri = flow.step1_get_authorize_url()
     return HttpResponseRedirect(auth_uri)
示例#5
0
文件: users.py 项目: 4johndoe/mygpo
 def get(self, request):
     flow = get_google_oauth_flow(request)
     auth_uri = flow.step1_get_authorize_url()
     return HttpResponseRedirect(auth_uri)