Ejemplo n.º 1
0
def _save_user_and_token(token_dict, user_info, openid_type):
    ua = UserAlias.get(openid_type, user_info.get_user_id())
    if not ua:
        if not g.user:
            ua = UserAlias.create_new_user(openid_type,
                    user_info.get_user_id(), user_info.get_nickname())
        else:
            ua = UserAlias.bind_to_exists_user(g.user, 
                    openid_type, user_info.get_user_id())
    if not ua:
        return None

    ##设置个人资料(头像等等)
    u = User.get(ua.user_id)
    u.set_avatar_url(user_info.get_avatar())
    u.set_icon_url(user_info.get_icon())

    ##保存access token
    if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]:
        OAuth2Token.add(ua.id, token_dict.get("access_token"), 
                token_dict.get("access_token_secret", ""))
    else:
        OAuth2Token.add(ua.id, token_dict.get("access_token"), 
                token_dict.get("refresh_token", ""))
    ##set cookie,保持登录状态
    if not g.user:
        g.user = User.get(ua.user_id)
        set_user_cookie(g.user, session)
    
    return g.user
Ejemplo n.º 2
0
    def __init__(self,
                 alias=None,
                 apikey=None,
                 apikey_secret=None,
                 redirect_uri=None,
                 token=None,
                 token_secret=None,
                 openid=None,
                 openkey=None):

        self.consumer_key = apikey or config.APIKEY_DICT[
            config.OPENID_QQ]['key']
        self.consumer_secret = apikey_secret or config.APIKEY_DICT[
            config.OPENID_QQ]['secret']
        self.callback = redirect_uri or config.APIKEY_DICT[
            config.OPENID_QQ]['redirect_uri']

        self.token = token
        self.token_secret = token_secret
        #XXX:no use?
        self.openid = openid
        self.openkey = openkey

        self.alias = alias
        if alias:
            self.user_alias = UserAlias.get(
                config.OPENID_TYPE_DICT[config.OPENID_QQ], alias)
        else:
            self.user_alias = None
Ejemplo n.º 3
0
    def __init__(self,
                 alias=None,
                 apikey=None,
                 apikey_secret=None,
                 redirect_uri=None,
                 token=None,
                 token_secret=None):

        d = config.APIKEY_DICT[config.OPENID_TWITTER]

        self.consumer_key = apikey or d['key']
        self.consumer_secret = apikey_secret or d['secret']
        self.callback = redirect_uri or d['redirect_uri']

        self.token = token
        self.token_secret = token_secret

        self.alias = alias
        if alias:
            self.user_alias = UserAlias.get(
                config.OPENID_TYPE_DICT[config.OPENID_TWITTER], alias)
        else:
            self.user_alias = None

        self.auth = tweepy.OAuthHandler(self.consumer_key,
                                        self.consumer_secret, self.callback)
        if self.token and self.token_secret and self.auth:
            self.auth.set_access_token(self.token, self.token_secret)
Ejemplo n.º 4
0
    def __init__(self, alias, apikey=None, apikey_secret=None, access_token=None, access_token_secret=None):
        ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_TWITTER], alias)
        self.apikey = apikey if apikey is not None else config.APIKEY_DICT[config.OPENID_TWITTER].get("key")
        self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[config.OPENID_TWITTER].get("secret")
        
        token = OAuth2Token.get(ua.id)
        self.access_token = access_token if access_token is not None else token.access_token
        self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token

        self.auth = tweepy.OAuthHandler(self.apikey, self.apikey_secret)        
        self.auth.set_access_token(self.access_token, self.access_token_secret)
Ejemplo n.º 5
0
    def __init__(self, alias, apikey=None, apikey_secret=None, access_token=None, access_token_secret=None):
        ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_QQ], alias)
        
        self.apikey = apikey if apikey is not None else config.APIKEY_DICT[config.OPENID_QQ].get("key")
        self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[config.OPENID_QQ].get("secret")

        ##TODO:这里的OAuth2Token也变相的存储了OAuth1的token和secret,需要后续改一改
        token = OAuth2Token.get(ua.id)
        self.access_token = access_token if access_token is not None else token.access_token
        self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token

        self.auth = QQOAuth1Login(self.apikey, self.apikey_secret, 
                token=self.access_token, token_secret=self.access_token_secret)
Ejemplo n.º 6
0
def _save_user_and_token(token_dict, thirdparty_user, openid_type):
    first_connect = False
    ua = UserAlias.get(openid_type, thirdparty_user.get_user_id())
    if not ua:
        if not g.user:
            ua = UserAlias.create_new_user(openid_type,
                                           thirdparty_user.get_user_id(),
                                           thirdparty_user.get_nickname())
        else:
            ua = UserAlias.bind_to_exists_user(g.user, openid_type,
                                               thirdparty_user.get_user_id())
        first_connect = True
    if not ua:
        return None

    ##设置个人资料(头像等等)
    u = User.get(ua.user_id)
    u.set_avatar_url(thirdparty_user.get_avatar())
    u.set_icon_url(thirdparty_user.get_icon())

    ##把各个第三方的uid保存到profile里面
    k = openid_type
    v = {
        "uid": thirdparty_user.get_uid(),
        "name": thirdparty_user.get_nickname(),
        "intro": thirdparty_user.get_intro(),
        "signature": thirdparty_user.get_signature(),
        "avatar": thirdparty_user.get_avatar(),
        "icon": thirdparty_user.get_icon(),
        "email": thirdparty_user.get_email(),
        "first_connect": "Y" if first_connect else "N",
    }
    u.set_profile_item(k, json_encode(v))

    ##保存access token
    if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]:
        OAuth2Token.add(ua.id, token_dict.get("access_token"),
                        token_dict.get("access_token_secret", ""))
    else:
        OAuth2Token.add(ua.id, token_dict.get("access_token"),
                        token_dict.get("refresh_token", ""))
    ##set cookie,保持登录状态
    if not g.user:
        g.user = User.get(ua.user_id)
        set_user_cookie(g.user, session)

    return g.user
Ejemplo n.º 7
0
def _save_user_and_token(token_dict, thirdparty_user, openid_type):
    first_connect = False
    ua = UserAlias.get(openid_type, thirdparty_user.get_user_id())
    if not ua:
        if not g.user:
            ua = UserAlias.create_new_user(openid_type,
                    thirdparty_user.get_user_id(), thirdparty_user.get_nickname())
        else:
            ua = UserAlias.bind_to_exists_user(g.user, 
                    openid_type, thirdparty_user.get_user_id())
        first_connect = True
    if not ua:
        return None

    ##设置个人资料(头像等等)
    u = User.get(ua.user_id)
    u.set_avatar_url(thirdparty_user.get_avatar())
    u.set_icon_url(thirdparty_user.get_icon())

    ##把各个第三方的uid保存到profile里面
    k = openid_type
    v = {
        "uid": thirdparty_user.get_uid(), 
        "name": thirdparty_user.get_nickname(), 
        "intro": thirdparty_user.get_intro(),
        "signature": thirdparty_user.get_signature(),
        "avatar": thirdparty_user.get_avatar(),
        "icon": thirdparty_user.get_icon(),
        "email": thirdparty_user.get_email(),
        "first_connect": "Y" if first_connect else "N",
    }
    u.set_profile_item(k, json_encode(v))

    ##保存access token
    if openid_type == config.OPENID_TYPE_DICT[config.OPENID_TWITTER]:
        OAuth2Token.add(ua.id, token_dict.get("access_token"), 
                token_dict.get("access_token_secret", ""))
    else:
        OAuth2Token.add(ua.id, token_dict.get("access_token"), 
                token_dict.get("refresh_token", ""))
    ##set cookie,保持登录状态
    if not g.user:
        g.user = User.get(ua.user_id)
        set_user_cookie(g.user, session)
    
    return g.user
Ejemplo n.º 8
0
    def __init__(self,
                 alias,
                 apikey=None,
                 apikey_secret=None,
                 access_token=None,
                 access_token_secret=None):
        ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_TWITTER],
                           alias)
        self.apikey = apikey if apikey is not None else config.APIKEY_DICT[
            config.OPENID_TWITTER].get("key")
        self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[
            config.OPENID_TWITTER].get("secret")

        token = OAuth2Token.get(ua.id)
        self.access_token = access_token if access_token is not None else token.access_token
        self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token

        self.auth = tweepy.OAuthHandler(self.apikey, self.apikey_secret)
        self.auth.set_access_token(self.access_token, self.access_token_secret)
Ejemplo n.º 9
0
    def __init__(self, alias=None, 
            apikey=None, apikey_secret=None, redirect_uri=None, 
            token=None, token_secret=None, openid=None, openkey=None):

        self.consumer_key = apikey or config.APIKEY_DICT[config.OPENID_QQ]['key']
        self.consumer_secret = apikey_secret or config.APIKEY_DICT[config.OPENID_QQ]['secret']
        self.callback = redirect_uri or config.APIKEY_DICT[config.OPENID_QQ]['redirect_uri']

        self.token = token
        self.token_secret = token_secret
        #XXX:no use?
        self.openid = openid
        self.openkey = openkey

        self.alias=alias
        if alias:
            self.user_alias = UserAlias.get(
                    config.OPENID_TYPE_DICT[config.OPENID_QQ], alias)
        else:
            self.user_alias = None
Ejemplo n.º 10
0
    def __init__(self, provider=None, apikey=None, apikey_secret=None, redirect_uri=None, 
            scope=None, state=None, display=None, 
            alias=None, access_token=None, refresh_token=None):

        self.provider = provider
        self.apikey = apikey
        self.apikey_secret = apikey_secret
        self.redirect_uri = redirect_uri

        self.scope = scope
        self.state = state
        self.display = display

        self.alias = alias
        if alias:
            self.user_alias = UserAlias.get(
                    config.OPENID_TYPE_DICT[provider], alias)
        else:
            self.user_alias = None
        self.access_token = access_token
        self.refresh_token = refresh_token
Ejemplo n.º 11
0
    def __init__(self,
                 alias,
                 apikey=None,
                 apikey_secret=None,
                 access_token=None,
                 access_token_secret=None):
        ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_QQ], alias)

        self.apikey = apikey if apikey is not None else config.APIKEY_DICT[
            config.OPENID_QQ].get("key")
        self.apikey_secret = apikey_secret if apikey_secret is not None else config.APIKEY_DICT[
            config.OPENID_QQ].get("secret")

        ##TODO:这里的OAuth2Token也变相的存储了OAuth1的token和secret,需要后续改一改
        token = OAuth2Token.get(ua.id)
        self.access_token = access_token if access_token is not None else token.access_token
        self.access_token_secret = access_token_secret if access_token_secret is not None else token.refresh_token

        self.auth = QQOAuth1Login(self.apikey,
                                  self.apikey_secret,
                                  token=self.access_token,
                                  token_secret=self.access_token_secret)
Ejemplo n.º 12
0
    def __init__(self, alias=None, 
            apikey=None, apikey_secret=None, redirect_uri=None,
            token=None, token_secret=None):

        d = config.APIKEY_DICT[config.OPENID_TWITTER]

        self.consumer_key = apikey or d['key']
        self.consumer_secret = apikey_secret or d['secret']
        self.callback = redirect_uri or d['redirect_uri']

        self.token = token
        self.token_secret = token_secret

        self.alias = alias
        if alias:
            self.user_alias = UserAlias.get(
                    config.OPENID_TYPE_DICT[config.OPENID_TWITTER], alias)
        else:
            self.user_alias = None

        self.auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret, self.callback)
        if self.token and self.token_secret and self.auth:
            self.auth.set_access_token(self.token, self.token_secret)
Ejemplo n.º 13
0
def bind_wordpress():
    if not g.user:
        flash(u"请先使用豆瓣、微博、QQ、Twitter任意一个帐号登录后,再来做绑定blog的操作^^", "tip")
        return redirect("/home")
    user = g.user

    intros = [
        g.user.get_thirdparty_profile(x).get("intro")
        for x in config.OPENID_TYPE_DICT.values()
    ]
    intros = filter(None, intros)

    uas = g.user.get_alias()
    wordpress_alias_list = [
        x for x in uas
        if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS]
    ]

    step = "1"
    random_id = mc.get("wordpress_bind:%s" % g.user.id)
    c = random_id and Confirmation.get_by_random_id(random_id)
    if c:
        _, feed_uri = c.text.split(":", 1)
        step = "2"
    else:
        feed_uri = ""

    if request.method == "GET":
        return render_template("v2/bind_wordpress.html",
                               consts=consts,
                               **locals())

    elif request.method == "POST":
        ret = {}
        ret['ok'] = False
        if step == '1':
            feed_uri = request.form.get("feed_uri")
            if not feed_uri:
                ret['msg'] = 'feed地址不能为空'
            elif not (feed_uri.startswith("http://")
                      or feed_uri.startswith("https://")):
                ret['msg'] = 'feed地址貌似不对'
            else:
                ua = UserAlias.get(
                    config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri)
                if ua:
                    ret['msg'] = '该feed地址已被绑定'
                else:
                    ##设置一个激活码
                    code = randbytes(16)
                    val = "%s:%s" % (g.user.id, feed_uri)
                    r = Confirmation.add(code, val)
                    if r:
                        ret['ok'] = True
                        ret['msg'] = '为了验证blog的主人^^,请发一篇blog,内容为 %s,完成该步骤后,请点下一步完成绑定' \
                                % code
                        mc.set("wordpress_bind:%s" % g.user.id, code)
                    else:
                        ret['msg'] = '抱歉,出错了,请重试, 或者给管理员捎个话:[email protected]'
            return json_encode(ret)
        elif step == '2':
            if not (random_id and c):
                ret['msg'] = '出错了,激活码不对^^'
            else:
                text = c.text
                user_id, feed_uri = text.split(":", 1)
                ## 同步一下,看看验证码的文章是否正确
                client = Wordpress(feed_uri)
                rs = client.get_feeds(refresh=True)
                if not rs:
                    ret['msg'] = '没有发现含有验证码的文章,请检查后再提交验证'
                else:
                    latest_post = rs[0]
                    if not latest_post:
                        ret['msg'] = "你的feed地址可能无法访问,请检查下"
                    else:
                        content = latest_post.get_content(
                        ) or latest_post.get_summary()
                        if content and content.encode("utf8")[:100].find(
                                str(random_id)) != -1:
                            ua = UserAlias.bind_to_exists_user(
                                g.user, config.OPENID_TYPE_DICT[
                                    config.OPENID_WORDPRESS], feed_uri)
                            if not ua:
                                ret['msg'] = '出错了,麻烦你重试一下吧^^'
                            else:
                                ##添加同步任务
                                t = SyncTask.add(config.CATE_WORDPRESS_POST,
                                                 g.user.id)
                                t and TaskQueue.add(t.id, t.kind)
                                ##删除confiration记录
                                c.delete()
                                mc.delete("wordpress_bind:%s" % g.user.id)

                                ret['ok'] = True
                                ret['msg'] = '恭喜,绑定成功啦'
                        else:
                            ret['msg'] = "没有发现含有验证码的文章,请检查后再提交验证"
            return json_encode(ret)
    else:
        return "method not allowed"
Ejemplo n.º 14
0
def bind_wordpress():
    if not g.user:
        flash(u"请先使用豆瓣、微博、QQ、Twitter任意一个帐号登录后,再来做绑定blog的操作^^", "tip")
        return redirect("/home")

    intros = [g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()]
    intros = filter(None, intros)

    uas = g.user.get_alias()
    wordpress_alias_list = [x for x in uas if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS]]

    step = "1"
    random_id = mc.get("wordpress_bind:%s" % g.user.id)
    c = random_id and Confirmation.get_by_random_id(random_id)
    if c:
        _, feed_uri = c.text.split(":", 1)
        step = "2"
    else:
        feed_uri = ""
    

    if request.method == "GET":
        return render_template("bind_wordpress.html", consts=consts, **locals())
    
    elif request.method == "POST":
        ret = {}
        ret['ok'] = False
        if step == '1':
            feed_uri = request.form.get("feed_uri")
            if not feed_uri:
                ret['msg'] = 'feed地址不能为空'
            elif not (feed_uri.startswith("http://") or feed_uri.startswith("https://")):
                ret['msg'] = 'feed地址貌似不对'
            else:
                ua = UserAlias.get(config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri)
                if ua:
                    ret['msg'] = '该feed地址已被绑定'
                else:
                    ##设置一个激活码
                    code = randbytes(16)
                    val = "%s:%s" % (g.user.id, feed_uri)
                    r = Confirmation.add(code, val)
                    if r:
                        ret['ok'] = True
                        ret['msg'] = '为了验证blog的主人^^,请发一篇blog,内容为 %s,完成该步骤后,请点下一步完成绑定' \
                                % code
                        mc.set("wordpress_bind:%s" %g.user.id, code)
                    else:
                        ret['msg'] = '抱歉,出错了,请重试, 或者给管理员捎个话:[email protected]'
            return json_encode(ret)
        elif step == '2':
            if not (random_id and c):
                ret['msg'] = '出错了,激活码不对^^'
            else:
                text = c.text
                user_id, feed_uri = text.split(":", 1)
                ## 同步一下,看看验证码的文章是否正确
                client = Wordpress(feed_uri)
                rs = client.get_feeds(refresh=True)
                if not rs:
                    ret['msg'] = '没有发现含有验证码的文章,请检查后再提交验证'
                else:
                    latest_post = rs[0]
                    if not latest_post:
                        ret['msg'] = "你的feed地址可能无法访问,请检查下"
                    else:
                        content = latest_post.get_content() or latest_post.get_summary()
                        if content and content.encode("utf8")[:100].find(str(random_id)) != -1:
                            ua = UserAlias.bind_to_exists_user(g.user, 
                                    config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS], feed_uri)
                            if not ua:
                                ret['msg'] = '出错了,麻烦你重试一下吧^^'
                            else:
                                ##添加同步任务
                                t = SyncTask.add(config.CATE_WORDPRESS_POST, g.user.id)
                                t and TaskQueue.add(t.id, t.kind)
                                ##删除confiration记录
                                c.delete()
                                mc.delete("wordpress_bind:%s" %g.user.id)

                                ret['ok'] = True
                                ret['msg'] = '恭喜,绑定成功啦'
                        else:
                            ret['msg'] = "没有发现含有验证码的文章,请检查后再提交验证"
            return json_encode(ret)
    else:
        return "method not allowed"