Пример #1
0
def cleanup_and_get_new_connection_token( user_id ):
	# note: ensure user is logged in and has display name before calling this function
	if user_id:
		# delete any existing connect token for the user
		ndb.delete_multi_async( fetch_EnkiModelRestAPIConnectToken_by_user( user_id ))
		# create a new token and return it
		token = generate_connect_code()
		entity = EnkiModelRestAPIConnectToken( token = token, user_id = int( user_id ))
		entity.put()
		return token
	return None
Пример #2
0
def cleanup_and_get_new_connection_token(user_id):
    # note: ensure user is logged in and has display name before calling this function
    if user_id:
        # delete any existing connect token for the user
        ndb.delete_multi_async(
            fetch_EnkiModelRestAPIConnectToken_by_user(user_id))
        # create a new token and return it
        token = generate_connect_code()
        entity = EnkiModelRestAPIConnectToken(token=token,
                                              user_id=int(user_id))
        entity.put()
        return token
    return None
Пример #3
0
	def post( self ):
		jsonobject = json.loads( self.request.body )
		success = False
		error = 'Invalid request'
		answer = {}
		if jsonobject:
			code = jsonobject.get( 'code', '')
			displayname = jsonobject.get( 'displayname', '')
			app_id = jsonobject.get( 'app_id', '')
			app_secret = jsonobject.get( 'app_secret', '')
			if code and displayname and app_id and app_secret:
				if EnkiModelApp.exist_by_app_id_app_secret( app_id, app_secret ):   # check against registered apps
					user_id = EnkiModelDisplayName.get_user_id_from_display_name( displayname )
					if user_id:
						entity = EnkiModelRestAPIConnectToken.get_by_user_id_token_valid_age( user_id, code )
						if entity:
							auth_token = enki.libutil.generate_auth_token()
							entity.key.delete()     # single use token
							verification_token = EnkiModelRestAPITokenVerify( token = auth_token, user_id = user_id, app_id = app_id, app_secret = app_secret, type = 'apiconnect' )
							verification_token.put()    # persistent authentication token, a user may have several
							answer.update({ 'user_id' : str( user_id ), 'auth_token' : auth_token })
							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=(',',':') ))
Пример #4
0
	def post( self ):
		# generate a new token (and delete old one if they exist)
		if self.ensure_is_logged_in() and self.ensure_has_display_name( self.request.referrer ):
			self.check_CSRF()
			user_id = self.enki_user.key.id()
			token = EnkiModelRestAPIConnectToken.cleanup_and_get_new_connection_token( user_id )
			self.add_infomessage( MSG.SUCCESS(), MSG.GAME_CONNECTION_TOKEN( token, EnkiModelRestAPIConnectToken.MAX_AGE ))
			self.redirect_to_relevant_page()
Пример #5
0
def get_EnkiModelRestAPIConnectToken_by_token_user_id_valid_age(
        token, user_id):
    entity = EnkiModelRestAPIConnectToken.query(
        ndb.AND(
            EnkiModelRestAPIConnectToken.token == token,
            EnkiModelRestAPIConnectToken.user_id == user_id,
            EnkiModelRestAPIConnectToken.time_created >
            (datetime.datetime.now() -
             datetime.timedelta(minutes=MAX_AGE)))).get()
    return entity
Пример #6
0
 def cleanup_item(self):
     number = random.randint(1, 1000)
     likelihood = 10  # occurs with a probability of 1%
     if number < likelihood:
         ndb.delete_multi_async(self.fetch_keys_old_sessions(3))
         ndb.delete_multi_async(EnkiModelBackoffTimer.fetch_keys_old(3))
         ndb.delete_multi_async(EnkiModelTokenAuth.fetch_keys_expired())
         ndb.delete_multi_async(
             EnkiModelRestAPIConnectToken.fetch_expired())
         ndb.delete_multi_async(EnkiModelRestAPIDataStore.fetch_expired())
         ndb.delete_multi_async(
             EnkiModelTokenVerify.fetch_keys_old_tokens_by_types(
                 0.007, [
                     'loginaddconfirm_1', 'loginaddconfirm_2',
                     'loginaddconfirm_3'
                 ]))
         EnkiModelRestAPIDataStore.refresh_non_expiring()
         ndb.delete_multi_async(
             EnkiModelTokenVerify.fetch_keys_old_tokens_by_types(
                 1, ['emailsubscriptionconfirm']))
         ndb.delete_multi_async(EnkiModelBackoffTimer.fetch_keys_old(1))
Пример #7
0
def fetch_EnkiModelRestAPIConnectToken_expired():
    list = EnkiModelRestAPIConnectToken.query(
        EnkiModelRestAPIConnectToken.time_created <
        (datetime.datetime.now() - datetime.timedelta(minutes=MAX_AGE))).fetch(
            keys_only=True)
    return list
Пример #8
0
def fetch_EnkiModelRestAPIConnectToken_by_user(user_id):
    list = EnkiModelRestAPIConnectToken.query(
        EnkiModelRestAPIConnectToken.user_id == user_id).fetch(keys_only=True)
    return list
Пример #9
0
def fetch_EnkiModelRestAPIConnectToken_expired():
	list = EnkiModelRestAPIConnectToken.query( EnkiModelRestAPIConnectToken.time_created < ( datetime.datetime.now() - datetime.timedelta( minutes = MAX_AGE ))).fetch( keys_only = True )
	return list
Пример #10
0
def fetch_EnkiModelRestAPIConnectToken_by_user( user_id ):
	list = EnkiModelRestAPIConnectToken.query( EnkiModelRestAPIConnectToken.user_id == user_id ).fetch( keys_only = True )
	return list
Пример #11
0
def get_EnkiModelRestAPIConnectToken_by_token_user_id_valid_age( token, user_id ):
	entity = EnkiModelRestAPIConnectToken.query( ndb.AND( EnkiModelRestAPIConnectToken.token == token,
	                                                      EnkiModelRestAPIConnectToken.user_id == user_id,
	                                                      EnkiModelRestAPIConnectToken.time_created > ( datetime.datetime.now() - datetime.timedelta( minutes = MAX_AGE )))).get()
	return entity