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()
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")
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)
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")
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()
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()
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()
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)
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)
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)
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)
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)
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()
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()
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
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()
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)
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)
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()
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)
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()
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()
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)
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")
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()
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)
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})
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()
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)
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)
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)
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 })
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)
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)
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)
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)
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)
def GET(self, jrequest, jresponse): jresponse.setId() odenki_user = OdenkiUser.loadFromSession() jresponse.setResult(odenki_user)
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")
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")