Exemple #1
0
def authorized():
    """ 授权回调路由
    此路由地址:/oauth2/coding/authorized
    """
    # 换取access_token
    resp = coding.authorized_response()
    print "authorized_response:", resp
    if resp and isinstance(resp, dict) and "access_token" in resp:
        # 根据access_token获取用户基本信息
        user = coding.get_userinfo(resp["access_token"])
        if user["code"] != 0:
            flash(user["msg"].keys())
            return redirect(url_for("front.index"))
        user = user["data"]
        # 处理第三方登录逻辑
        auth = Authentication(g.mysql, g.redis)
        # 第三方账号登录入口`oauth2_go`
        avatar = "https://coding.net" + user["avatar"] if user[
            "avatar"].startswith("/") else user["avatar"]
        goinfo = auth.oauth2_go(name=name,
                                signin=g.signin,
                                tokeninfo=resp,
                                userinfo=dict(openid=user["id"],
                                              nick_name=user["name"],
                                              gender=oauth2_genderconverter(
                                                  user["sex"]),
                                              avatar=avatar,
                                              domain_name=user["global_key"],
                                              signature=user["slogan"],
                                              location=user.get("location")),
                                uid=g.uid)
        goinfo = dfr(goinfo)
        if goinfo["pageAction"] == "goto_signIn":
            """ 未登录流程->执行登录 """
            # 记录登录日志
            auth.brush_loginlog(dict(identity_type=oauth2_name2type(name),
                                     uid=goinfo["goto_signIn_data"]["guid"],
                                     success=True),
                                login_ip=request.headers.get(
                                    'X-Real-Ip', request.remote_addr),
                                user_agent=request.headers.get("User-Agent"))
            # 设置登录态
            return coding.goto_signIn(uid=goinfo["goto_signIn_data"]["guid"])
        elif goinfo["pageAction"] == "goto_signUp":
            """ 未登录流程->执行注册绑定功能 """
            return coding.goto_signUp(
                openid=goinfo["goto_signUp_data"]["openid"])
        else:
            # 已登录流程->反馈绑定结果
            if goinfo["success"]:
                # 绑定成功,返回原页面
                flash(u"已绑定")
            else:
                # 绑定失败,返回原页面
                flash(goinfo["msg"])
            # 跳回原页面
            return redirect(url_for("front.index"))
    else:
        flash(
            u'Access denied: reason=%s error=%s' %
            (request.args.get('error'), request.args.get('error_description')))
    return redirect(url_for("front.index"))
Exemple #2
0
def authorized():
    """ 授权回调路由
    此路由地址:/oauth2/weibo/authorized
    """
    # 加密的sso参数值
    sso = request.args.get("sso") or None
    # 换取access_token
    resp = weibo.authorized_response()
    if resp and isinstance(resp, dict) and "access_token" in resp:
        # 根据access_token获取用户唯一标识
        openid = weibo.get_openid(resp["access_token"]).get("uid")
        # 根据access_token获取用户基本信息
        user = weibo.get_userinfo(resp["access_token"], uid=openid)
        if user.get("error_code"):
            flash("error_code: %s, error_description: %s" %
                  (user.get("error_code"), user.get("error")))
            return redirect(g.redirect_uri)
        # 处理第三方登录逻辑
        auth = Authentication(g.mysql, g.redis)
        # 第三方账号登录入口`oauth2_go`
        goinfo = auth.oauth2_go(name=name,
                                signin=g.signin,
                                tokeninfo=resp,
                                userinfo=dict(openid=openid,
                                              nick_name=user["screen_name"],
                                              gender=oauth2_genderconverter(
                                                  user["gender"]),
                                              avatar=user["profile_image_url"],
                                              domain_name=user["domain"],
                                              signature=user["description"],
                                              location=user.get("location")),
                                uid=g.uid)
        goinfo = dfr(goinfo)
        if goinfo["pageAction"] == "goto_signIn":
            """ 未登录流程->已经绑定过账号,需要设置登录态 """
            uid = goinfo["goto_signIn_data"]["guid"]
            # 记录登录日志
            auth.brush_loginlog(dict(identity_type=oauth2_name2type(name),
                                     uid=uid,
                                     success=True),
                                login_ip=g.ip,
                                user_agent=request.headers.get("User-Agent"))
            # 设置登录态
            return weibo.goto_signIn(uid=uid, sso=sso)
        elif goinfo["pageAction"] == "goto_signUp":
            """ 未登录流程->执行注册绑定功能 """
            return weibo.goto_signUp(
                openid=goinfo["goto_signUp_data"]["openid"], sso=sso)
        else:
            # 已登录流程->正在绑定第三方账号:反馈绑定结果
            if goinfo["success"]:
                # 绑定成功,返回原页面
                flash(u"已绑定")
            else:
                # 绑定失败,返回原页面
                flash(goinfo["msg"])
            # 跳回绑定设置页面
            return redirect(url_for("front.userset", _anchor="bind"))
    else:
        flash(
            u'Access denied: reason=%s error=%s' %
            (request.args.get('error'), request.args.get('error_description')))
    return redirect(g.redirect_uri)
Exemple #3
0
def authorized():
    """ 授权回调路由
    此路由地址:/oauth2/qq/authorized
    """
    # 换取access_token
    resp = qq.authorized_response()
    if "callback" in resp:
        resp = json.loads(resp[10:-3])
    else:
        resp = qq.url_code(resp)
    print "authorized_response:",resp
    if resp and isinstance(resp, dict) and "access_token" in resp:
        # 获取用户唯一标识
        openid = json.loads(qq.get_openid(resp["access_token"])[10:-3]).get("openid")
        # 根据access_token获取用户基本信息
        user = qq.get_userinfo(resp["access_token"], openid=openid, oauth_consumer_key=PLUGINS[name]["APP_ID"])
        if int(user.get("ret", 0)) < 0:
            flash(user.get("msg"))
            return redirect(url_for("front.index"))
        # 处理第三方登录逻辑
        auth = Authentication(g.mysql, g.redis)
        # 第三方账号登录入口`oauth2_go`
        goinfo = auth.oauth2_go(name=name, signin=g.signin, tokeninfo=resp, userinfo=dict(openid=openid, nick_name=user["nickname"], gender=oauth2_genderconverter(user["gender"]), avatar=user["figureurl_qq_1"], location="%s %s" %(user.get("province"), user.get("city"))), uid=g.uid)
        goinfo = dfr(goinfo)
        if goinfo["pageAction"] == "goto_signIn":
            """ 未登录流程->执行登录 """
            # 记录登录日志
            auth.brush_loginlog(dict(identity_type=oauth2_name2type(name), uid=goinfo["goto_signIn_data"]["guid"], success=True), login_ip=request.headers.get('X-Real-Ip', request.remote_addr), user_agent=request.headers.get("User-Agent"))
            # 设置登录态
            return qq.goto_signIn(uid=goinfo["goto_signIn_data"]["guid"])
        elif goinfo["pageAction"] == "goto_signUp":
            """ 未登录流程->执行注册绑定功能 """
            return qq.goto_signUp(openid=goinfo["goto_signUp_data"]["openid"])
        else:
            # 已登录流程->反馈绑定结果
            if goinfo["success"]:
                # 绑定成功,返回原页面
                flash(u"已绑定")
            else:
                # 绑定失败,返回原页面
                flash(goinfo["msg"])
            # 跳回原页面
            return redirect(url_for("front.index"))
    else:
        flash(u'Access denied: reason=%s error=%s' % (
            resp.get('error'),
            resp.get('error_description')
        ))
    return redirect(url_for("front.index"))