コード例 #1
0
 def GET(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     
     try:
         odenki_user = OdenkiUser.loadFromSession()
         assert isinstance(odenki_user, OdenkiUser)
     except: odenki_user = None
     
     try:
         email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
         assert isinstance(email_user, EmailUser)
     except: email_user = None
     
     try:
         twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
         assert isinstance(twitter_user, TwitterUser)
     except: twitter_user = None
     
     try:
         gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
         assert isinstance(gmail_user, GmailUser)
     except: gmail_user = None
     
     jresponse.setResultValue(odenki_user.__class__.__name__, odenki_user)
     jresponse.setResultObject(email_user)
     jresponse.setResultObject(twitter_user)
     jresponse.setResultObject(gmail_user)
     login_url = users.create_login_url("/api/auth/Gmail/RedirectedFromGoogle")
     jresponse.setResultValue("login_url", login_url)
コード例 #2
0
ファイル: Session.py プロジェクト: TakashiSasaki/odenkiapi
def fillTwitterUser():
    """fillTwitterUser() loads TwitterUser into the session if OdenkiUser is already in the session
    and fill TwitterUser.odenkiId if it does not exist.
    It also loads OdenkiUser into the session if TwitterUser is already in it and TwitterUser.odenkiId exists.  
    """
    odenki_user = OdenkiUser.loadFromSession()
    assert isinstance(odenki_user, OdenkiUser)
    try:
        twitter_user = TwitterUser.loadFromSession()
    except EntityNotFound:
        twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
    assert isinstance(twitter_user, TwitterUser)

    if twitter_user.odenkiId is None:
        twitter_user.odenkiId = odenki_user.odenkiId
        twitter_user.put()  # put TwitterUser if odenkiId is updated
    twitter_user.saveToSession()
コード例 #3
0
def fillTwitterUser():
    """fillTwitterUser() loads TwitterUser into the session if OdenkiUser is already in the session
    and fill TwitterUser.odenkiId if it does not exist.
    It also loads OdenkiUser into the session if TwitterUser is already in it and TwitterUser.odenkiId exists.  
    """
    odenki_user = OdenkiUser.loadFromSession()
    assert isinstance(odenki_user, OdenkiUser)
    try:
        twitter_user = TwitterUser.loadFromSession()
    except EntityNotFound:
        twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
    assert isinstance(twitter_user, TwitterUser)
    
    if twitter_user.odenkiId is None:
        twitter_user.odenkiId = odenki_user.odenkiId
        twitter_user.put() # put TwitterUser if odenkiId is updated
    twitter_user.saveToSession()
コード例 #4
0
 def deleteAllTwitterUsers(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     assert jrequest.fromAdminHost
     jresponse.setId()
     query = TwitterUser.query()
     for twitter_user_key in query.fetch(keys_only=True):
         assert isinstance(twitter_user_key, ndb.Key)
         twitter_user_key.delete_async()
コード例 #5
0
 def deleteAllTwitterUsers(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     assert jrequest.fromAdminHost
     jresponse.setId()
     query = TwitterUser.query()
     for twitter_user_key in query.fetch(keys_only=True):
         assert isinstance(twitter_user_key, ndb.Key)
         twitter_user_key.delete_async()
コード例 #6
0
 def deleteTwitterUser(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
     assert isinstance(twitter_user, TwitterUser)
     twitter_user.key.delete()
     jresponse.setResultValue("OdenkiUser", odenki_user)
コード例 #7
0
 def showAllTwitterUsers(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     assert jrequest.fromAdminHost
     jresponse.setId()
     query = TwitterUser.query()
     twitter_users=[]
     for twitter_user in query:
         twitter_users.append(twitter_user)
     jresponse.setResult(twitter_users)
コード例 #8
0
 def showAllTwitterUsers(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     assert jrequest.fromAdminHost
     jresponse.setId()
     query = TwitterUser.query()
     twitter_users = []
     for twitter_user in query:
         twitter_users.append(twitter_user)
     jresponse.setResult(twitter_users)
コード例 #9
0
 def deleteTwitterUser(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
     assert isinstance(twitter_user, TwitterUser)
     twitter_user.key.delete()
     jresponse.setResultValue("OdenkiUser", odenki_user)
コード例 #10
0
def fillOdenkiUserByTwitterUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        twitter_user = TwitterUser.loadFromSession()
        assert isinstance(twitter_user, TwitterUser)
        odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
コード例 #11
0
ファイル: Session.py プロジェクト: TakashiSasaki/odenkiapi
def fillOdenkiUserByTwitterUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        twitter_user = TwitterUser.loadFromSession()
        assert isinstance(twitter_user, TwitterUser)
        odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
コード例 #12
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        twitter_user = None
        odenki_user = None
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound: pass

        if odenki_user:
            try:
                twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
            except EntityNotFound: pass
        jresponse.setResult({"OdenkiUser": odenki_user, "TwitterUser":twitter_user})
コード例 #13
0
 def GET(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     try:
         twitter_user = TwitterUser.loadFromSession()
         assert isinstance(twitter_user, TwitterUser)
         jresponse.setResultValue("twitterId", twitter_user.twitterId)
         jresponse.setResultValue("screenName", twitter_user.screenName)
         jresponse.setResultValue("odenkiId", twitter_user.odenkiId)
     except EntityNotFound: pass
     
     implicit_flow = ImplicitFlow()
     callback_url = self._makeCallbackUrl(jrequest.request.url)
     jresponse.setResultValue("callback_url", callback_url)
     auth_url = implicit_flow.getAuthUrl(TWITTER_CONSUMER_KEY, callback_url)
     jresponse.setResultValue("auth_url", auth_url)
コード例 #14
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        twitter_user = None
        odenki_user = None
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            pass

        if odenki_user:
            try:
                twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
            except EntityNotFound:
                pass
        jresponse.setResult({
            "OdenkiUser": odenki_user,
            "TwitterUser": twitter_user
        })
コード例 #15
0
ファイル: settings.py プロジェクト: TakashiSasaki/odenkiapi
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()

        try:
            odenki_user = OdenkiUser.loadFromSession()
            assert isinstance(odenki_user, OdenkiUser)
        except:
            odenki_user = None

        try:
            email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
            assert isinstance(email_user, EmailUser)
        except:
            email_user = None

        try:
            twitter_user = TwitterUser.getByOdenkiId(odenki_user.odenkiId)
            assert isinstance(twitter_user, TwitterUser)
        except:
            twitter_user = None

        try:
            gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
            assert isinstance(gmail_user, GmailUser)
        except:
            gmail_user = None

        jresponse.setResultValue(odenki_user.__class__.__name__, odenki_user)
        jresponse.setResultObject(email_user)
        jresponse.setResultObject(twitter_user)
        jresponse.setResultObject(gmail_user)
        login_url = users.create_login_url(
            "/api/auth/Gmail/RedirectedFromGoogle")
        jresponse.setResultValue("login_url", login_url)
コード例 #16
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/auth/error.html")
        try:
            oauth_token = jrequest.getValue("oauth_token")[0]
            oauth_verifier = jrequest.getValue("oauth_verifier")[0]
        except:
            raise OAuthError("OAuthCallback was called with neither oauth_token nor oauth_verifier.")
        
        session = gaesessions.get_current_session()
        try:
            request_token = session[REQUEST_TOKEN_SESSION_KEY]
            request_token_secret = session[REQUEST_TOKEN_SECRET_SESSION_KEY]
        except KeyError:
            raise OAuthError("Request token have not been obtained.")
        
        if oauth_token != request_token:
            raise OAuthError("OAuthCallback gets token which is not identical to retaining request token.")

        token = oauth2.Token(request_token, request_token_secret)
        token.set_verifier(oauth_verifier)
        consumer = oauth2.Consumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
        client = oauth2.Client(consumer, token)
        resp, content = client.request(ACCESS_TOKEN_URL, "POST")
        access_token_dict = dict(parse_qsl(content))
        try:
            access_token = access_token_dict["oauth_token"]
            access_token_secret = access_token_dict["oauth_token_secret"]
            user_id = access_token_dict["user_id"]
            screen_name = access_token_dict["screen_name"]
        except KeyError:
            raise OAuthError({"request_token": request_token,
                              "ACCESS_TOKEN_URL": ACCESS_TOKEN_URL
                              },
                             message="OAuthCallback failed to exchange verified request token to access token.")
    
        # prepare TwittrUser
        try: 
            twitter_user = TwitterUser.getByTwitterId(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put_async()
        except EntityNotFound:
            twitter_user = TwitterUser.create(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put()
        assert isinstance(twitter_user, TwitterUser)
        
        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound: 
            odenki_user = None
        
        # reconcile TwitterUser and OdenkiUser
        if odenki_user is None:
            if twitter_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                assert isinstance(odenki_user, OdenkiUser)
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if twitter_user.odenkiId is None:
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                if twitter_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({twitter_user.__class__.__name__: twitter_user,
                                                     odenki_user.__class__.__name__:odenki_user})
                odenki_user.saveToSession()
        
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("TwitterUser", twitter_user)
        jresponse.setRedirectTarget("/html/settings.html")
コード例 #17
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/api/Twitter")
        debug(jrequest.request)
        implicit_flow = ImplicitFlow()
        implicit_flow.setAccessToken(jrequest.request)
        implicit_flow.saveToSession()
        try:
            twitter_user = TwitterUser.loadFromSession(self)
            if twitter_user.twitterId != implicit_flow.twitterId:
                raise MixedAuthentication({"twitter_user.twitterId": twitter_user.twitterId, "implicit_flow.twitterId": implicit_flow.twitterId})
        except EntityNotFound:
            try:
                twitter_user = TwitterUser.getByTwitterId(implicit_flow.twitterId)
            except EntityNotFound:
                twitter_user = TwitterUser()
                twitter_user.twitterId = implicit_flow.twitterId

        assert isinstance(twitter_user, TwitterUser)
        assert twitter_user.twitterId == implicit_flow.twitterId
        twitter_user.screenName = implicit_flow.screenName
        twitter_user.accessToken = implicit_flow.accessToken
        
        if twitter_user.odenkiId is None:
            try:
                odenki_user = OdenkiUser.loadFromSession()
                assert isinstance(odenki_user, OdenkiUser)
                twitter_user.odenkiId = odenki_user.odenkiId
            except EntityNotFound:
                odenki_user = OdenkiUser.getNew()
                assert isinstance(odenki_user, OdenkiUser)
                twitter_user.odenkiId = odenki_user.odenkiId
        else:
            try:
                odenki_user = OdenkiUser.loadFromSession()
                if twitter_user.odenkiId != odenki_user.odenkiId:
                    raise MixedAuthentication({"twitter_user.odenkiId": twitter_user.odenkiId, "odenki_user.odenkiId": odenki_user.odenkiId}) 
            except EntityNotFound:
                odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
                odenki_user.saveToSession()
                
        assert twitter_user.odenkiId == odenki_user.odenkiId
        twitter_user.put()
        twitter_user.saveToSession()
コード例 #18
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/auth/error.html")
        try:
            oauth_token = jrequest.getValue("oauth_token")[0]
            oauth_verifier = jrequest.getValue("oauth_verifier")[0]
        except:
            raise OAuthError(
                "OAuthCallback was called with neither oauth_token nor oauth_verifier."
            )

        session = gaesessions.get_current_session()
        try:
            request_token = session[REQUEST_TOKEN_SESSION_KEY]
            request_token_secret = session[REQUEST_TOKEN_SECRET_SESSION_KEY]
        except KeyError:
            raise OAuthError("Request token have not been obtained.")

        if oauth_token != request_token:
            raise OAuthError(
                "OAuthCallback gets token which is not identical to retaining request token."
            )

        token = oauth2.Token(request_token, request_token_secret)
        token.set_verifier(oauth_verifier)
        consumer = oauth2.Consumer(TWITTER_CONSUMER_KEY,
                                   TWITTER_CONSUMER_SECRET)
        client = oauth2.Client(consumer, token)
        resp, content = client.request(ACCESS_TOKEN_URL, "POST")
        access_token_dict = dict(parse_qsl(content))
        try:
            access_token = access_token_dict["oauth_token"]
            access_token_secret = access_token_dict["oauth_token_secret"]
            user_id = access_token_dict["user_id"]
            screen_name = access_token_dict["screen_name"]
        except KeyError:
            raise OAuthError(
                {
                    "request_token": request_token,
                    "ACCESS_TOKEN_URL": ACCESS_TOKEN_URL
                },
                message=
                "OAuthCallback failed to exchange verified request token to access token."
            )

        # prepare TwittrUser
        try:
            twitter_user = TwitterUser.getByTwitterId(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put_async()
        except EntityNotFound:
            twitter_user = TwitterUser.create(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put()
        assert isinstance(twitter_user, TwitterUser)

        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None

        # reconcile TwitterUser and OdenkiUser
        if odenki_user is None:
            if twitter_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                assert isinstance(odenki_user, OdenkiUser)
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if twitter_user.odenkiId is None:
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                if twitter_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({
                        twitter_user.__class__.__name__:
                        twitter_user,
                        odenki_user.__class__.__name__:
                        odenki_user
                    })
                odenki_user.saveToSession()

        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("TwitterUser", twitter_user)
        jresponse.setRedirectTarget("/html/settings.html")
コード例 #19
0
 def __init__(self):
     TwitterUser.__init__(self)