def setUp(self): self.token = 'TESTINGOAUTHSTUFF' self.header = {'Authorization': 'Bearer ' + self.token} r_client.hset(self.token, 'timestamp', '12/12/12 12:12:00') r_client.hset(self.token, 'grant_type', 'client') r_client.expire(self.token, 20) super(OauthTestingBase, self).setUp()
def setUp(self): self.client_token = 'SOMEAUTHTESTINGTOKENHERE2122' r_client.hset(self.client_token, 'timestamp', '12/12/12 12:12:00') r_client.hset(self.client_token, 'client_id', 'test123123123') r_client.hset(self.client_token, 'grant_type', 'client') r_client.expire(self.client_token, 5) self.headers = {'Authorization': 'Bearer ' + self.client_token} super(RESTHandlerTestCase, self).setUp()
def set_token(self, client_id, grant_type, user=None, timeout=3600): """Create access token for the client on redis and send json response Parameters ---------- client_id : str Client that requested the token grant_type : str Type of key being requested user : str, optional If password grant type requested, the user requesting the key. timeout : int, optional The timeout, in seconds, for the token. Default 3600 Returns ------- Writes token information JSON in the form expected by RFC6750: {'access_token': token, 'token_type': 'Bearer', 'expires_in': timeout} access_token: the actual token to use token_type: 'Bearer', which is the expected token type for Oauth2 expires_in: time to token expiration, in seconds. """ token = self.generate_access_token() r_client.hset(token, 'timestamp', datetime.datetime.now()) r_client.hset(token, 'client_id', client_id) r_client.hset(token, 'grant_type', grant_type) r_client.expire(token, timeout) if user: r_client.hset(token, 'user', user) if grant_type == 'password': # Check if client has access limit key, and if not, create it limit_key = '%s_%s_daily_limit' % (client_id, user) limiter = r_client.get(limit_key) if limiter is None: # Set limit to 5,000 requests per day r_client.setex(limit_key, 5000, 86400) self.write({ 'access_token': token, 'token_type': 'Bearer', 'expires_in': timeout }) self.finish()
def set_token(self, client_id, grant_type, user=None, timeout=3600): """Create access token for the client on redis and send json response Parameters ---------- client_id : str Client that requested the token grant_type : str Type of key being requested user : str, optional If password grant type requested, the user requesting the key. timeout : int, optional The timeout, in seconds, for the token. Default 3600 Returns ------- Writes token information JSON in the form expected by RFC6750: {'access_token': token, 'token_type': 'Bearer', 'expires_in': timeout} access_token: the actual token to use token_type: 'Bearer', which is the expected token type for Oauth2 expires_in: time to token expiration, in seconds. """ token = self.generate_access_token() r_client.hset(token, 'timestamp', datetime.datetime.now()) r_client.hset(token, 'client_id', client_id) r_client.hset(token, 'grant_type', grant_type) r_client.expire(token, timeout) if user: r_client.hset(token, 'user', user) if grant_type == 'password': # Check if client has access limit key, and if not, create it limit_key = '%s_%s_daily_limit' % (client_id, user) limiter = r_client.get(limit_key) if limiter is None: # Set limit to 5,000 requests per day r_client.setex(limit_key, 5000, 86400) self.write({'access_token': token, 'token_type': 'Bearer', 'expires_in': timeout}) self.finish()
def setUp(self): # Create client test authentication token self.client_token = 'SOMEAUTHTESTINGTOKENHERE2122' r_client.hset(self.client_token, 'timestamp', '12/12/12 12:12:00') r_client.hset(self.client_token, 'client_id', 'test123123123') r_client.hset(self.client_token, 'grant_type', 'client') r_client.expire(self.client_token, 5) # Create username test authentication token self.user_token = 'SOMEAUTHTESTINGTOKENHEREUSERNAME' r_client.hset(self.user_token, 'timestamp', '12/12/12 12:12:00') r_client.hset(self.user_token, 'client_id', 'testuser') r_client.hset(self.user_token, 'grant_type', 'password') r_client.hset(self.user_token, 'user', '*****@*****.**') r_client.expire(self.user_token, 5) # Create test access limit token self.user_rate_key = '[email protected]_daily_limit' r_client.setex(self.user_rate_key, 2, 5) super(OAuth2BaseHandlerTests, self).setUp()
def _get_stats(self, callback): # check if the key exists in redis redis_lats_key = '%s:stats:sample_lats' % qiita_config.portal redis_longs_key = '%s:stats:sample_longs' % qiita_config.portal lats = r_client.lrange(redis_lats_key, 0, -1) longs = r_client.lrange(redis_longs_key, 0, -1) if not (lats and longs): # if we don't have them, then fetch from disk and add to the # redis server with a 24-hour expiration lat_longs = get_lat_longs() lats = [float(x[0]) for x in lat_longs] longs = [float(x[1]) for x in lat_longs] with r_client.pipeline() as pipe: for latitude, longitude in lat_longs: # storing as a simple data structure, hopefully this # doesn't burn us later pipe.rpush(redis_lats_key, latitude) pipe.rpush(redis_longs_key, longitude) # set the key to expire in 24 hours, so that we limit the # number of times we have to go to the database to a reasonable # amount r_client.expire(redis_lats_key, 86400) r_client.expire(redis_longs_key, 86400) pipe.execute() else: # If we do have them, put the redis results into the same structure # that would come back from the database longs = [float(x) for x in longs] lats = [float(x) for x in lats] lat_longs = zip(lats, longs) # Get the number of studies num_studies = get_count('qiita.study') # Get the number of samples num_samples = len(lats) # Get the number of users num_users = get_count('qiita.qiita_user') callback([num_studies, num_samples, num_users, lat_longs])