def post(self): jsonobject = json.loads(self.request.body) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int(jsonobject.get('user_id', '')) auth_token = jsonobject.get('auth_token', '') app_secret = jsonobject.get('app_secret', '') data_type = jsonobject.get('data_type', '') data_id = jsonobject.get('data_id', '') if user_id and auth_token and app_secret and data_type and data_id: if enki.librestapi.check_secret(user_id, auth_token, app_secret): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token) if token_valid: # user is valid data_stores = enki.librestapi.fetch_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_data_id( user_id, token_valid.app_id, data_type, data_id) ndb.delete_multi(data_stores) success = True error = '' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({'success': success, 'error': error}) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(answer, separators=(',', ':')))
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '')) auth_token = jsonobject.get( 'auth_token', '') app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '') data_id = jsonobject.get( 'data_id', '') if user_id and auth_token and app_secret and data_type and data_id: if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid data_store_item = EnkiModelRestAPIDataStore.get_by_user_id_app_id_data_type_data_id_not_expired( user_id, token_valid.app_id, data_type, data_id ) if data_store_item: answer.update({ 'data_payload' : data_store_item.data_payload, 'time_expires' : enki.libutil.seconds_from_epoch( data_store_item.time_expires ) , 'read_access' : data_store_item.read_access, 'server_time' : int( time.time())}) success = True error = '' else: error = 'Not found' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))
def check_secret( user_id, auth_token, app_secret ): if EnkiModelRestAPITokenVerify.exist_by_user_id_token_app_secret( user_id, auth_token, app_secret ): # the user verify token contains the same secret as was sent in the request return True else: # retrieve the app_id from the verify token and check if the corresponding registered app has a new secret token_verify = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_verify: app = EnkiModelApp.get_by_id( int( token_verify.app_id )) if app and app.secret == app_secret: # update the user's verify token app_secret token_verify.app_secret = app.secret token_verify.put() return True return False
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '')) auth_token = jsonobject.get( 'auth_token', '') app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '') data_id = jsonobject.get( 'data_id', '') data_payload = jsonobject.get( 'data_payload' ) # set the expiry time time_to_expiry = int(jsonobject.get( 'time_expires', EnkiModelRestAPIDataStore.DATASTORE_EXPIRY_DEFAULT )) # default lifetime if unspecified if time_to_expiry == 0: # non-expiring lifetime time_to_expiry = EnkiModelRestAPIDataStore.DATASTORE_NON_EXPIRING time_expires = datetime.datetime.now() + datetime.timedelta( seconds = time_to_expiry ) read_access = jsonobject.get( 'read_access', '' ) if user_id and auth_token and app_secret and data_type and data_id and data_payload and time_expires: if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid # add optional calculated properties to the data payload if 'calc_ip_addr' in data_payload: # IP address of the request remote_address = self.request.remote_addr data_payload.update({ 'calc_ip_addr' : remote_address }) data_store = EnkiModelRestAPIDataStore.get_by_user_id_app_id_data_type_data_id( user_id, token_valid.app_id, data_type, data_id ) if data_store: # update data_store.data_payload = data_payload data_store.time_expires = time_expires # update the expiry time if read_access: # if read_access is not specified, don't update it data_store.read_access = read_access else: # create new if not read_access: # if read_access is not specified, use the default value defined in the model data_store = EnkiModelRestAPIDataStore( user_id = user_id, app_id = token_valid.app_id, data_type = data_type, data_id = data_id, data_payload = data_payload, time_expires = time_expires ) else: data_store = EnkiModelRestAPIDataStore( user_id = user_id, app_id = token_valid.app_id, data_type = data_type, data_id = data_id, data_payload = data_payload, time_expires = time_expires, read_access = read_access ) data_store.put() success = True error = '' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))
def check_secret(user_id, auth_token, app_secret): if EnkiModelRestAPITokenVerify.exist_by_user_id_token_app_secret( user_id, auth_token, app_secret): # the user verify token contains the same secret as was sent in the request return True else: # retrieve the app_id from the verify token and check if the corresponding registered app has a new secret token_verify = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token) if token_verify: app = EnkiModelApp.get_by_id(int(token_verify.app_id)) if app and app.secret == app_secret: # update the user's verify token app_secret token_verify.app_secret = app.secret token_verify.put() return True return False
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '' )) auth_token = jsonobject.get( 'auth_token', '' ) app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '' ) read_access = jsonobject.get( 'read_access', '' ) if user_id and auth_token and app_secret and data_type and ( read_access == 'public' or read_access == 'private' or read_access == 'friends' ): if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid error = 'Not found' data_store_list = [] if read_access == 'public': # returns all data with read-access "public" data_store_list = EnkiModelRestAPIDataStore.fetch_by_app_id_data_type_read_access_not_expired( token_valid.app_id, data_type, read_access ) else: people_list = [] if read_access == 'private': # returns all user's data with read-access "private" people_list = [ user_id ] elif read_access == 'friends': # returns list of user's friends' data with friends' read_access "friends" people_list = EnkiModelFriends.get_friends_user_id( user_id ) # get the user's friends' ids if people_list: for person_id in people_list: # get each persons' data data_store_list = EnkiModelRestAPIDataStore.fetch_by_user_id_app_id_data_type_read_access_not_expired( person_id, token_valid.app_id, data_type, read_access ) if data_store_list: data_payloads = [] for data_store_item in data_store_list: data_payloads.append({ 'user_id' : str( data_store_item.user_id ), 'data_id' : data_store_item.data_id, 'data_payload' : data_store_item.data_payload, 'time_expires' : enki.libutil.seconds_from_epoch( data_store_item.time_expires )}) if data_payloads: answer.update({ 'data_payloads' : data_payloads, 'server_time' : int( time.time())}) success = True error = '' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))