Beispiel #1
0
def authorize(request):
    manager = None
    type = request.GET.get('type', None)
    if not type:
        return HttpResponseRedirect('/')
    manager = get_manager_by_type(type)
    if not manager:
        return HttpResponseRedirect('/')
    code = request.GET.get('code', None)
    rsp = manager.get_access_token_by_code(code)
    nexturl = request.GET.get('next_url', None)
    if not nexturl:
        nexturl = '/'
    if not rsp:
        return HttpResponseRedirect(manager.get_authorization_url(nexturl))
    user = manager.get_oauth_userinfo()
    if user:
        if not user.nikename:
            import datetime
            user.nikename = "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S')
        try:
            temp = OAuthUser.objects.get(type=type, openid=user.openid)
            temp.picture = user.picture
            temp.matedata = user.matedata
            temp.nikename = user.nikename
            user = temp
        except ObjectDoesNotExist:
            pass
        # facebook的token过长
        if type == 'facebook':
            user.token = ''
        email = user.email
        if email:
            author = None
            try:
                author = get_user_model().objects.get(id=user.author_id)
            except ObjectDoesNotExist:
                pass
            if not author:
                result = get_user_model().objects.get_or_create(email=user.email)
                author = result[0]
                if result[1]:
                    author.username = user.nikename
                    author.save()

            user.author = author
            user.save()

            oauth_user_login_signal.send(sender=authorize.__class__, id=user.id)
            login(request, author)
            return HttpResponseRedirect(nexturl)
        if not email:
            user.save()
            url = reverse('oauth:require_email', kwargs={
                'oauthid': user.id
            })

            return HttpResponseRedirect(url)
    else:
        return HttpResponseRedirect(nexturl)
Beispiel #2
0
def emailconfirm(request, id, sign):
    if not sign:
        return HttpResponseForbidden()
    if not get_md5(settings.SECRET_KEY + str(id) +
                   settings.SECRET_KEY).upper() == sign.upper():
        return HttpResponseForbidden()
    oauthuser = get_object_or_404(OAuthUser, pk=id)
    with transaction.atomic():
        if oauthuser.author:
            author = get_user_model().objects.get(pk=oauthuser.author_id)
        else:
            result = get_user_model().objects.get_or_create(
                email=oauthuser.email)
            author = result[0]
            if result[1]:
                author.source = 'emailconfirm'
                author.username = oauthuser.nikename.strip(
                ) if oauthuser.nikename.strip(
                ) else "djangoblog" + datetime.datetime.now().strftime(
                    '%y%m%d%I%M%S')
                author.save()
        oauthuser.author = author
        oauthuser.save()
    oauth_user_login_signal.send(sender=emailconfirm.__class__,
                                 id=oauthuser.id)
    login(request, author)

    site = get_current_site().domain
    content = '''
     <p>Congratulations, you have successfully bound your mailbox, you can use {type} to directly log in to this website without a password. Welcome to continue to pay attention to this site, the address is</p>

                <a href="{url}" rel="bookmark">{url}</a>

                Thank you again!
                <br />
                If the link above cannot be opened, please copy this link to your browser.
                {url}
    '''.format(type=oauthuser.type, url='http://' + site)

    send_email(emailto=[
        oauthuser.email,
    ],
               title='Congratulations on your successful linking!',
               content=content)
    url = reverse('oauth:bindsuccess', kwargs={'oauthid': id})
    url = url + '?type=success'
    return HttpResponseRedirect(url)
Beispiel #3
0
def emailconfirm(request, id, sign):
    print(1111111111111)
    if not sign:
        return HttpResponseForbidden()
    if not get_md5(
            settings.SECRET_KEY +
            str(id) +
            settings.SECRET_KEY).upper() == sign.upper():
        return HttpResponseForbidden()
    oauthuser = get_object_or_404(OAuthUser, pk=id)
    with transaction.atomic():
        if oauthuser.author:
            author = get_user_model().objects.get(pk=oauthuser.author_id)
        else:
            result = get_user_model().objects.get_or_create(email=oauthuser.email)
            author = result[0]
            if result[1]:
                author.source = 'emailconfirm'
                author.username = oauthuser.nikename.strip() if oauthuser.nikename.strip(
                ) else "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S')
                author.save()
        oauthuser.author = author
        oauthuser.save()
    oauth_user_login_signal.send(
        sender=emailconfirm.__class__,
        id=oauthuser.id)
    login(request, author)

    site = get_current_site().domain
    content = '''
         <p>恭喜您,您已经成功绑定您的邮箱,您可以使用{type}来直接免密码登录本网站.欢迎您继续关注本站,地址是</p>

                <a href="{url}" rel="bookmark">{url}</a>

                再次感谢您!
                <br />
                如果上面链接无法打开,请将此链接复制至浏览器。
                {url}
    '''.format(type=oauthuser.type, url='http://' + site)

    send_email(emailto=[oauthuser.email, ], title='恭喜您绑定成功!', content=content)
    url = reverse('oauth:bindsuccess', kwargs={
        'oauthid': id
    })
    url = url + '?type=success'
    return HttpResponseRedirect(url)
Beispiel #4
0
def authorize(request):
    type = request.GET.get('type', None)
    if not type:
        return HttpResponseRedirect('/')
    manager = get_manager_by_type(type)
    if not manager:
        return HttpResponseRedirect('/')
    code = request.GET.get('code', None)
    try:
        rsp = manager.get_access_token_by_code(code)
    except OAuthAccessTokenException as e:
        logger.warning("OAuthAccessTokenException:" + str(e))
        return HttpResponseRedirect('/')
    except Exception as e:
        logger.error(e)
        rsp = None
    nexturl = get_redirecturl(request)
    if not rsp:
        return HttpResponseRedirect(manager.get_authorization_url(nexturl))
    user = manager.get_oauth_userinfo()
    if user:
        if not user.nikename or not user.nikename.strip():
            import datetime
            user.nikename = "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S')
        try:
            temp = OAuthUser.objects.get(type=type, openid=user.openid)
            temp.picture = user.picture
            temp.matedata = user.matedata
            temp.nikename = user.nikename
            user = temp
        except ObjectDoesNotExist:
            pass
        # facebook的token过长
        if type == 'facebook':
            user.token = ''
        if user.email:
            with transaction.atomic():
                author = None
                try:
                    author = get_user_model().objects.get(id=user.author_id)
                except ObjectDoesNotExist:
                    pass
                if not author:
                    result = get_user_model().objects.get_or_create(email=user.email)
                    author = result[0]
                    if result[1]:
                        try:
                            get_user_model().objects.get(username=user.nikename)
                        except ObjectDoesNotExist:
                            author.username = user.nikename
                        else:
                            author.username = "******" + datetime.datetime.now().strftime('%y%m%d%I%M%S')
                        author.source = 'authorize'
                        author.save()

                user.author = author
                user.save()

                oauth_user_login_signal.send(
                    sender=authorize.__class__, id=user.id)
                login(request, author)
                return HttpResponseRedirect(nexturl)
        else:
            user.save()
            url = reverse('oauth:require_email', kwargs={
                'oauthid': user.id
            })

            return HttpResponseRedirect(url)
    else:
        return HttpResponseRedirect(nexturl)
Beispiel #5
0
def authorize(request):
    type = request.GET.get('type', None)
    logger.info(type)
    if not type:
        return HttpResponseRedirect('/')
    manager = get_manager_by_type(type)
    logger.info(manager)
    if not manager:
        return HttpResponseRedirect('/')
    code = request.GET.get('code', None)
    try:
        rsp = manager.get_access_token_by_code(code)
    except OAuthAccessTokenException as e:
        logger.warning("OAuthAccessTokenException:" + str(e))
        return HttpResponseRedirect('/')
    except Exception as e:
        logger.error(e)
        rsp = None
    nexturl = get_redirecturl(request)
    if not rsp:
        return HttpResponseRedirect(manager.get_authorization_url(nexturl))
    user = manager.get_oauth_userinfo()
    if user:
        if not user.nikename or not user.nikename.strip():
            import datetime
            user.nikename = "djangoblog" + datetime.datetime.now().strftime(
                '%y%m%d%I%M%S')
        try:
            temp = OAuthUser.objects.get(type=type, openid=user.openid)
            temp.picture = user.picture
            temp.matedata = user.matedata
            temp.nikename = user.nikename
            user = temp
        except ObjectDoesNotExist:
            pass
        # facebook的token过长
        if type == 'facebook':
            user.token = ''
        if user.email:
            with transaction.atomic():  #在执行上下文里面的内容时候时,遇到错误执行回滚操作,类似mysql回滚函数
                author = None
                try:
                    author = get_user_model().objects.get(
                        id=user.author_id
                    )  #通过认证模型的外键或者邮箱来查找用户模型,认证模型和用户模型邮箱名相同
                except ObjectDoesNotExist:
                    pass
                if not author:
                    result = get_user_model().objects.get_or_create(
                        email=user.email
                    )  #Returns a tuple of (object, created)
                    author = result[0]
                    if result[1]:
                        author.username = user.nikename
                        author.source = 'authorize'
                        author.save()

                user.author = author  #给认证模型绑定用户
                user.save()

                oauth_user_login_signal.send(
                    sender=authorize.__class__,
                    id=user.id)  #这个信号用于登陆前,删除上一个用户的缓存信息
                login(request,
                      author)  #用户模型登陆,先删除上一个用户的缓存,再通过外键或者邮箱得到用户模型,最终用用户模型登陆。
                return HttpResponseRedirect(nexturl)
        else:
            user.save()
            url = reverse('oauth:require_email', kwargs={'oauthid': user.id})

            return HttpResponseRedirect(url)
    else:
        return HttpResponseRedirect(nexturl)