Пример #1
0
 def POST(self, jrequest, jresponse):
     jresponse.setId()
     odenki_id = int(jrequest.getValue("odenkiId")[0])
     try:
         odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
     except EntityNotFound, e:
         odenki_user = OdenkiUser.createNew()
Пример #2
0
 def POST(self, jrequest, jresponse):
     jresponse.setId()
     odenki_id = int(jrequest.getValue("odenkiId")[0])
     try:
         odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
     except EntityNotFound, e:
         odenki_user = OdenkiUser.createNew()
Пример #3
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/error.html")
        #jresponse.setRedirectTarget("/html/auth/index.html")
        current_user = users.get_current_user()
        if current_user is None:
            #            jresponse.setRedirectTarget("/html/auth/index.html")
            return
        #debug("type of current_user.user_id() is %s " % type(current_user.user_id()))

        # prepare GmailUser
        try:
            gmail_user = GmailUser.getByGmailId(current_user.user_id())
            gmail_user.nickname = current_user.nickname()
            gmail_user.gmail = current_user.email()
            gmail_user.put_async()
        except EntityNotFound:
            gmail_user = GmailUser()
            gmail_user.gmailId = current_user.user_id()
            gmail_user.gmail = current_user.email()
            gmail_user.nickname = current_user.nickname()
            gmail_user.put()
        assert isinstance(gmail_user, GmailUser)

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

        if odenki_user is None:
            if gmail_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                odenki_user.saveToSession()
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(gmail_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if gmail_user.odenkiId is None:
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
                odenki_user.saveToSession()
            else:
                if gmail_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({
                        gmail_user.__class__.__name__:
                        gmail_user,
                        odenki_user.__class__.__name__:
                        odenki_user
                    })
                odenki_user.saveToSession()

        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("GmailUser", gmail_user)
        jresponse.setRedirectTarget("/html/settings.html")
Пример #4
0
    def GET(self, jrequest, jresponse):
        """load EmailUser instance by nonce.
        Nonce will be invalidated and email will be updated."""
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        #EmailUser.deleteFromSession()
        nonce = unicode(jrequest.getPathInfo(4))
        email_user = EmailUser.getByNonce(nonce)
        assert isinstance(email_user, EmailUser)
        assert email_user.nonceEmail is not None
        email_user.email = email_user.nonceEmail
        email_user.nonceEmail = None
        email_user.hashedPassword = email_user.nonceHashedPassword
        email_user.nonceHashedPassword = None
        email_user.nonce = None
        email_user.put_async()
        #email_user.saveToSession()

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

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

        jresponse.setResultValue(odenki_user.__class__.__name__, odenki_user)
        jresponse.setResultValue(email_user.__class__.__name__, email_user)
        #jresponse.setResultValue("nonce", nonce)
        jresponse.setRedirectTarget("http://%s/html/settings.html" %
                                    jrequest.request.host)
Пример #5
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/error.html")
        #jresponse.setRedirectTarget("/html/auth/index.html")
        current_user = users.get_current_user()
        if current_user is None:
#            jresponse.setRedirectTarget("/html/auth/index.html")
            return
        #debug("type of current_user.user_id() is %s " % type(current_user.user_id()))

        # prepare GmailUser
        try:
            gmail_user = GmailUser.getByGmailId(current_user.user_id())
            gmail_user.nickname = current_user.nickname()
            gmail_user.gmail = current_user.email()
            gmail_user.put_async()
        except EntityNotFound:
            gmail_user = GmailUser()
            gmail_user.gmailId = current_user.user_id()
            gmail_user.gmail = current_user.email()
            gmail_user.nickname = current_user.nickname()
            gmail_user.put()
        assert isinstance(gmail_user, GmailUser)
        
        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None
        
        if odenki_user is None:
            if gmail_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                odenki_user.saveToSession()
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(gmail_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if gmail_user.odenkiId is None:
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
                odenki_user.saveToSession()
            else:
                if gmail_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({gmail_user.__class__.__name__: gmail_user,
                                                     odenki_user.__class__.__name__:odenki_user})
                odenki_user.saveToSession()
        
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("GmailUser", gmail_user)
        jresponse.setRedirectTarget("/html/settings.html")
Пример #6
0
def fillOdenkiUserByEmailUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        email_user = EmailUser.loadFromSession()
        assert isinstance(email_user, EmailUser)
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
Пример #7
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()
Пример #8
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()
Пример #9
0
def fillOdenkiUserByEmailUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        email_user = EmailUser.loadFromSession()
        assert isinstance(email_user, EmailUser)
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
Пример #10
0
 def GET(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     try:
         odenki_user = OdenkiUser.loadFromSession()
     except EntityNotFound:
         jresponse.addResult("not logged in")
         return
     assert isinstance(odenki_user, OdenkiUser)
     jresponse.addResult("logged out")
     jresponse.addResult(odenki_user)
     OdenkiUser.deleteFromSession()
Пример #11
0
 def GET(self, jrequest, jresponse):
     """load EmailUser instance by nonce.
     Nonce will be invalidated and email will be updated."""
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     #EmailUser.deleteFromSession()
     nonce = unicode(jrequest.getPathInfo(4))
     email_user = EmailUser.getByNonce(nonce)
     assert isinstance(email_user, EmailUser)
     assert email_user.nonceEmail is not None
     email_user.email = email_user.nonceEmail
     email_user.nonceEmail = None
     email_user.hashedPassword = email_user.nonceHashedPassword
     email_user.nonceHashedPassword = None
     email_user.nonce = None
     email_user.put_async()
     #email_user.saveToSession()
     
     # prepare OdenkiUser
     try:
         odenki_user = OdenkiUser.loadFromSession()
     except EntityNotFound:
         odenki_user = None
         
     # reconcile EmailUser and OdenkiUser
     if odenki_user is None:
         if email_user.odenkiId is None:
             odenki_user = OdenkiUser.createNew()
             assert isinstance(odenki_user, OdenkiUser)
             odenki_user.saveToSession()
             email_user.setOdenkiId(odenki_user.odenkiId)
             email_user.put_async()
         else:
             odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
             odenki_user.saveToSession()
     else:
         if email_user.odenkiId is None:
             odenki_user.saveToSession()
             email_user.setOdenkiId(odenki_user.odenkiId)
             email_user.put_async()
         else:
             if email_user.odenkiId != odenki_user.odenkiId:
                 raise InconsistentAuthentiation({email_user.__class__.__name__: email_user,
                                                  odenki_user.__class__.__name__:odenki_user})
             odenki_user.saveToSession()
     
     jresponse.setResultValue(odenki_user.__class__.__name__, odenki_user)
     jresponse.setResultValue(email_user.__class__.__name__, email_user)
     #jresponse.setResultValue("nonce", nonce)
     jresponse.setRedirectTarget("http://%s/html/settings.html" % jrequest.request.host)
Пример #12
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)
Пример #13
0
    def POST(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        try:
            email = jrequest.getValue("email")[0]
            raw_password = jrequest.getValue("password")[0]
        except Exception:
            raise InvalidParams(
                "email and password are required for method=login.")
        try:
            email_user = EmailUser.getByEmail(email)
        except Exception:
            raise EntityNotFound("EmailUser entity is not found",
                                 {"email": email})
        assert isinstance(email_user, EmailUser)
        email_user.matchPassword(
            raw_password)  # raises PasswordMismatch if password not matches
        #        email_user.saveToSession()

        assert email_user.odenkiId is not None
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        odenki_user.saveToSession()
        EmailUser.deleteFromSession()

        jresponse.setResultValue("EmailUser", email_user)
        jresponse.setResultValue("OdenkiUser", odenki_user)
Пример #14
0
 def setPassword(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     #        try:
     #            session = gaesessions.get_current_session()
     #            nonce = session[EMAIL_REGISTRATION_NONCE]
     #        except Exception, e:
     #            session.pop(EMAIL_REGISTRATION_NONCE);
     #            raise UnexpectedState("Nonce is not stored in session data.")
     #        assert isinstance(nonce, unicode)
     #        email_user = EmailUser.getByNonce(nonce)
     try:
         odenki_user = OdenkiUser.loadFromSession()
     except EntityNotFound:
         odenki_user = None
     email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
     assert isinstance(email_user, EmailUser)
     try:
         raw_password = jrequest.getValue("password")[0].decode()
         raw_password2 = jrequest.getValue("password2")[0].decode()
         assert len(raw_password) < 8
     except Exception, e:
         raise InvalidParams(
             "setPassword method requires password and password2. %s" % e)
Пример #15
0
 def GET(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     jresponse.setResultValue("odenkiId", odenki_user.odenkiId)
     jresponse.setResultValue("odenkiName", odenki_user.odenkiName)
Пример #16
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()
Пример #17
0
 def deleteEmailUser(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
     email_user.key.delete_async()
Пример #18
0
 def deleteEmailUser(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
     email_user.key.delete_async()
Пример #19
0
 def GET(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     jresponse.setResultValue("odenkiId", odenki_user.odenkiId)
     jresponse.setResultValue("odenkiName", odenki_user.odenkiName)
Пример #20
0
 def logout(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     try:
         odenki_user = OdenkiUser.loadFromSession()
     except EntityNotFound, e:
         jresponse.addResult("not logged in")
         return
Пример #21
0
 def invalidate(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
     assert isinstance(email_user, EmailUser)
     email_user.invalidate()
Пример #22
0
 def deleteGmailUser(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
     gmail_user.key.delete_async()
     jresponse.setResultValue("OdenkiUser", odenki_user)
     jresponse.setResultValue("GmailUser", gmail_user)
Пример #23
0
 def invalidate(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
     assert isinstance(email_user, EmailUser)
     email_user.invalidate()
Пример #24
0
 def deleteGmailUser(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
     gmail_user.key.delete_async()
     jresponse.setResultValue("OdenkiUser", odenki_user)
     jresponse.setResultValue("GmailUser", gmail_user)
Пример #25
0
 def logout(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     try:
         odenki_user = OdenkiUser.loadFromSession()
     except EntityNotFound, e:
         jresponse.addResult("not logged in")
         return
Пример #26
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)
Пример #27
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)
Пример #28
0
 def setOdenkiName(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     try:
         new_odenki_name = jrequest.getValue("newOdenkiName")[0]
         assert isinstance(new_odenki_name, unicode)
     except:
         raise InvalidParams("setOdenkiName requires newOdenkiName")
     odenki_user.setOdenkiName(new_odenki_name)
     odenki_user.saveToSession()
Пример #29
0
 def setOdenkiName(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     try:
         new_odenki_name = jrequest.getValue("newOdenkiName")[0]
         assert isinstance(new_odenki_name, unicode)
     except:
         raise InvalidParams("setOdenkiName requires newOdenkiName")
     odenki_user.setOdenkiName(new_odenki_name)
     odenki_user.saveToSession()
Пример #30
0
 def register(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     request = jrequest.request
     assert isinstance(request, Request)
     if len(request.params) < 3:
         raise InvalidParams(
             "method=register&x=y&z=w&moduleId=mid&sensorId=sid&sensorName=mysensor where x is product field, y is product string, z is serial field, is serial string"
         )
     assert request.params.items()[0][0] == "method"
     assert request.params.items()[0][1] == "register"
     (product_name_field, product_name_string) = request.params.items()[1]
     (serial_number_field, serial_number_string) = request.params.items()[2]
     assert isinstance(product_name_field, str)
     product_name_field = unicode(product_name_field)
     assert isinstance(product_name_field, unicode)
     assert isinstance(serial_number_field, str)
     serial_number_field = unicode(serial_number_field)
     assert isinstance(serial_number_string, unicode)
     try:
         product_name_data = Data.prepare(product_name_field,
                                          product_name_string)
         serial_number_data = Data.prepare(serial_number_field,
                                           serial_number_string)
         module_id_string = request.get("moduleId")
         assert len(module_id_string) > 0
         sensor_id_string = request.get("sensorId")
         assert len(sensor_id_string) > 0
         module_id_data = Data.prepare("moduleId", request.get("moduleId"))
         sensor_id_data = Data.prepare("sensorId", request.get("sensorId"))
         existing_sensor = Sensor.getByProductNameSerialNumberModuleIdSensorId(
             product_name_data, serial_number_data, module_id_data,
             sensor_id_data)
         raise EntityExists(
             "Equipment", {
                 product_name_field: product_name_string,
                 serial_number_field: serial_number_string,
                 "moduleId": module_id_string,
                 "sensorId": sensor_id_string
             })
     except EntityNotFound:
         pass
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     sensor_name = request.get("sensorName")
     if len(sensor_name) == 0: sensor_name = unicode(sensor_name)
     sensor_name_data = Data.prepare("sensorName", sensor_name)
     sensor = Sensor.create(product_name_data, serial_number_data,
                            module_id_data, sensor_id_data,
                            sensor_name_data, odenki_user)
     jresponse.addResult(sensor)
Пример #31
0
def fillGmailUser():
    odenki_user = OdenkiUser.loadFromSession()
    assert isinstance(odenki_user, OdenkiUser)
    try:
        gmail_user = GmailUser.loadFromSession()
    except EntityNotFound: 
        gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
    assert isinstance(gmail_user, GmailUser)

    if gmail_user.odenkiId is None:
        gmail_user.odenkiId = odenki_user.odenkiId
        gmail_user.put() # put GmailUser if odenkiId is updated
    gmail_user.saveToSession()
Пример #32
0
def fillGmailUser():
    odenki_user = OdenkiUser.loadFromSession()
    assert isinstance(odenki_user, OdenkiUser)
    try:
        gmail_user = GmailUser.loadFromSession()
    except EntityNotFound:
        gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
    assert isinstance(gmail_user, GmailUser)

    if gmail_user.odenkiId is None:
        gmail_user.odenkiId = odenki_user.odenkiId
        gmail_user.put()  # put GmailUser if odenkiId is updated
    gmail_user.saveToSession()
Пример #33
0
def fillEmailUser():
    odenki_user = OdenkiUser.loadFromSession()
    assert isinstance(odenki_user, OdenkiUser)
    try:
        email_user = EmailUser.loadFromSession()
    except EntityNotFound: 
        email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
    assert isinstance(email_user, EmailUser)

    if email_user.odenkiId is None:
        email_user.odenkiId = odenki_user.odenkiId
        email_user.put() # put EmailUser if odenkiId is updated
    email_user.saveToSession()
Пример #34
0
def fillEmailUser():
    odenki_user = OdenkiUser.loadFromSession()
    assert isinstance(odenki_user, OdenkiUser)
    try:
        email_user = EmailUser.loadFromSession()
    except EntityNotFound:
        email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
    assert isinstance(email_user, EmailUser)

    if email_user.odenkiId is None:
        email_user.odenkiId = odenki_user.odenkiId
        email_user.put()  # put EmailUser if odenkiId is updated
    email_user.saveToSession()
Пример #35
0
 def login(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     import gaesessions
     session = gaesessions.get_current_session()
     assert isinstance(session, gaesessions.Session)
     odenki_id = jrequest.request.get("odenkiId")
     odenki_id = int(odenki_id)
     odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
     assert isinstance(odenki_user, OdenkiUser)
     odenki_user.saveToSession()
     jresponse.addResult(odenki_user)
Пример #36
0
    def login(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        import gaesessions

        session = gaesessions.get_current_session()
        assert isinstance(session, gaesessions.Session)
        odenki_id = jrequest.request.get("odenkiId")
        odenki_id = int(odenki_id)
        odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
        jresponse.addResult(odenki_user)
Пример #37
0
class _Touch(JsonRpcDispatcher):

    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        
        outlet_id = None
        relay_id = None
        card_type = None
        card_id = None
        try:
            outlet_id = unicode(jrequest.getValue("outletId")[0])
            assert isinstance(outlet_id, unicode)
            relay_id = unicode(jrequest.getValue("relayId")[0])
            assert isinstance(relay_id, unicode)
            card_id = unicode(jrequest.getValue("cardId")[0])
            assert isinstance(card_id, unicode)
            card_type = unicode(jrequest.getValue("cardType")[0])
            assert isinstance(card_type, unicode)
        except Exception, e:
            raise InvalidParams({"outletId":None, "relayId":None, "cardType":None, "cardId":None, "exception": e.__class__.__name__})
        
        try:
            outlet = Outlet.getByOutletId(outlet_id)
        except EntityNotFound: 
            outlet = Outlet.create(outlet_id)
        assert isinstance(outlet, Outlet)
        outlet.cardId = card_id
        outlet.cardType = card_type
        outlet.put_async()
        
        try:
            outlet_name = jrequest.getValue("outletName")[0]
            assert isinstance(outlet_name, unicode)
            outlet.outletName = outlet_name
            outlet.put_async()
        except: pass
        
        try:
            odenki_user = OdenkiUser.loadFromSession()
            outlet.odenkiId = odenki_user.odenkiId
            outlet.put_async()
        except: pass
        
        outlet.saveToSession()
        
        jresponse.setResultObject(outlet)
        jresponse.setRedirectTarget("/html/outlet.html")
Пример #38
0
    def create(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)

        odenki_name = jrequest.request.get("odenkiName")
        odenki_name = odenki_name.decode()
        if odenki_name is None or len(odenki_name) == 0:
            raise InvalidParams("odenkiName is mandatory.")

        odenki_user = OdenkiUser.createNew()
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.odenkiName = odenki_name
        odenki_user.put()
        jresponse.addResult(odenki_user)
        jresponse.setId()
Пример #39
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()

        #query = ndb.Query(kind="OdenkiUser")
        #query = query.order(-OdenkiUser.odenkiId)
        #keys = query.fetch(limit=100, keys_only=True)
        #for key in keys:
        #    jresponse.addResult(key.get())

        odenki_user = OdenkiUser.loadFromSession()
        if odenki_user is not None:
            assert isinstance(odenki_user, OdenkiUser)
            jresponse.setResultObject(odenki_user)
Пример #40
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})
Пример #41
0
    def create(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)

        odenki_name = jrequest.request.get("odenkiName")
        odenki_name = odenki_name.decode()
        if odenki_name is None or len(odenki_name) == 0:
            raise InvalidParams("odenkiName is mandatory.")

        odenki_user = OdenkiUser.createNew()
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.odenkiName = odenki_name
        odenki_user.put()
        jresponse.addResult(odenki_user)
        jresponse.setId()
Пример #42
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()

        #query = ndb.Query(kind="OdenkiUser")
        #query = query.order(-OdenkiUser.odenkiId)
        #keys = query.fetch(limit=100, keys_only=True)
        #for key in keys:
        #    jresponse.addResult(key.get())

        odenki_user = OdenkiUser.loadFromSession()
        if odenki_user is not None:
            assert isinstance(odenki_user, OdenkiUser)
            jresponse.setResultObject(odenki_user)
Пример #43
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()
Пример #44
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:
         outlet = Outlet.loadFromSession()
         assert isinstance(outlet, Outlet)
     except EntityNotFound: 
         outlet = None
     jresponse.setResultObject(odenki_user)
     jresponse.setResultObject(outlet)
Пример #45
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()
Пример #46
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        
        odenki_user, email_user = None, None
        try:
            odenki_user = OdenkiUser.loadFromSession()
            assert isinstance(odenki_user, OdenkiUser)
            email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
            assert isinstance(email_user, EmailUser)
        except EntityNotFound: pass
        except AttributeError: pass

        jresponse.setResultValue("EmailUser", email_user)
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("host", jrequest.request.host)
Пример #47
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:
            outlet = Outlet.loadFromSession()
            assert isinstance(outlet, Outlet)
        except EntityNotFound:
            outlet = None
        jresponse.setResultObject(odenki_user)
        jresponse.setResultObject(outlet)
Пример #48
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()

        odenki_user, email_user = None, None
        try:
            odenki_user = OdenkiUser.loadFromSession()
            assert isinstance(odenki_user, OdenkiUser)
            email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
            assert isinstance(email_user, EmailUser)
        except EntityNotFound:
            pass
        except AttributeError:
            pass

        jresponse.setResultValue("EmailUser", email_user)
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("host", jrequest.request.host)
Пример #49
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None

        try:
            gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
        except EntityNotFound: 
            gmail_user = None
        except AttributeError:
            gmail_user = None
        login_url = users.create_login_url("/api/auth/Gmail/RedirectedFromGoogle")
        assert isinstance(login_url, str)
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("GmailUser", gmail_user)
        jresponse.setResultValue("login_url", login_url)
Пример #50
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
        })
Пример #51
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None

        try:
            gmail_user = GmailUser.getByOdenkiId(odenki_user.odenkiId)
        except EntityNotFound:
            gmail_user = None
        except AttributeError:
            gmail_user = None
        login_url = users.create_login_url(
            "/api/auth/Gmail/RedirectedFromGoogle")
        assert isinstance(login_url, str)
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("GmailUser", gmail_user)
        jresponse.setResultValue("login_url", login_url)
Пример #52
0
 def register(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     request = jrequest.request
     assert isinstance(request, Request)
     if len(request.params) < 3:
         raise InvalidParams("method=register&x=y&z=w&moduleId=mid&sensorId=sid&sensorName=mysensor where x is product field, y is product string, z is serial field, is serial string")
     assert request.params.items()[0][0] == "method"
     assert request.params.items()[0][1] == "register"
     (product_name_field, product_name_string) = request.params.items()[1]
     (serial_number_field, serial_number_string) = request.params.items()[2]
     assert isinstance(product_name_field, str)
     product_name_field = unicode(product_name_field) 
     assert isinstance(product_name_field, unicode)
     assert isinstance(serial_number_field, str)
     serial_number_field = unicode(serial_number_field)
     assert isinstance(serial_number_string, unicode)
     try:
         product_name_data = Data.prepare(product_name_field, product_name_string)
         serial_number_data = Data.prepare(serial_number_field, serial_number_string)
         module_id_string = request.get("moduleId")
         assert len(module_id_string) > 0
         sensor_id_string = request.get("sensorId")
         assert len(sensor_id_string) > 0
         module_id_data = Data.prepare("moduleId", request.get("moduleId"))
         sensor_id_data = Data.prepare("sensorId", request.get("sensorId"))
         existing_sensor = Sensor.getByProductNameSerialNumberModuleIdSensorId(product_name_data, serial_number_data, module_id_data, sensor_id_data)
         raise EntityExists("Equipment", {product_name_field:product_name_string, serial_number_field: serial_number_string, "moduleId":module_id_string, "sensorId":sensor_id_string})
     except EntityNotFound: pass
     odenki_user = OdenkiUser.loadFromSession()
     assert isinstance(odenki_user, OdenkiUser)
     sensor_name = request.get("sensorName")
     if len(sensor_name) == 0: sensor_name = unicode(sensor_name)
     sensor_name_data = Data.prepare("sensorName", sensor_name)
     sensor = Sensor.create(product_name_data, serial_number_data, module_id_data, sensor_id_data, sensor_name_data, odenki_user)
     jresponse.addResult(sensor)
Пример #53
0
    def POST(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        try:
            email = jrequest.getValue("email")[0]
            raw_password = jrequest.getValue("password")[0]
        except Exception:
            raise InvalidParams("email and password are required for method=login.")
        try:    
            email_user = EmailUser.getByEmail(email)
        except Exception:
            raise EntityNotFound("EmailUser entity is not found", {"email": email})
        assert isinstance(email_user, EmailUser)
        email_user.matchPassword(raw_password) # raises PasswordMismatch if password not matches
#        email_user.saveToSession()
        
        assert email_user.odenkiId is not None
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        odenki_user.saveToSession()
        EmailUser.deleteFromSession()
        
        jresponse.setResultValue("EmailUser", email_user)
        jresponse.setResultValue("OdenkiUser", odenki_user)
Пример #54
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)
Пример #55
0
    def setPassword(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
#        try:
#            session = gaesessions.get_current_session()
#            nonce = session[EMAIL_REGISTRATION_NONCE]
#        except Exception, e:
#            session.pop(EMAIL_REGISTRATION_NONCE);
#            raise UnexpectedState("Nonce is not stored in session data.")
#        assert isinstance(nonce, unicode)
#        email_user = EmailUser.getByNonce(nonce)
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None
        email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId)
        assert isinstance(email_user, EmailUser)
        try:
            raw_password = jrequest.getValue("password")[0].decode()
            raw_password2 = jrequest.getValue("password2")[0].decode()
            assert len(raw_password) < 8
        except Exception, e:
            raise InvalidParams("setPassword method requires password and password2. %s" % e)
Пример #56
0
 def GET(self, jrequest, jresponse):
     jresponse.setId()
     odenki_user = OdenkiUser.loadFromSession()
     jresponse.setResult(odenki_user)
Пример #57
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")
Пример #58
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")