def get_credentials(self): r = DARedis() r_key = 'da:' + self.appname + ':status:user:'******'code' in self.url_args and 'state' in self.url_args: r.delete(r_key) if self.url_args['state'] != stored_state.decode(): raise Exception("State did not match") flow = self._get_flow() credentials = flow.step2_exchange(self.url_args['code']) storage = RedisCredStorage(self.appname) storage.put(credentials) del self.url_args['code'] del self.url_args['state'] else: message("Please wait.", "You are in the process of authenticating.") storage = RedisCredStorage(self.appname) credentials = storage.get() if not credentials or credentials.invalid: state_string = random_string(16) pipe = r.pipeline() pipe.set(r_key, state_string) pipe.expire(r_key, 60) pipe.execute() flow = self._get_flow() uri = flow.step1_get_authorize_url(state=state_string) if 'state' in self.url_args: del self.url_args['state'] if 'code' in self.url_args: del self.url_args['code'] response(url=uri) return credentials
class RedisCredStorage(oauth2client.client.Storage): def __init__(self, app): self.r = DARedis() self.key = 'da:' + app + ':user:'******'da:' + app + ':lock:user:' + user_info().email def acquire_lock(self): pipe = self.r.pipeline() pipe.set(self.lockkey, 1) pipe.expire(self.lockkey, 5) pipe.execute() def release_lock(self): self.r.delete(self.lockkey) def locked_get(self): json_creds = self.r.get(self.key) creds = None if json_creds is not None: json_creds = json_creds.decode() try: creds = oauth2client.client.Credentials.new_from_json( json_creds) except: log("RedisCredStorage: could not read credentials from " + str(json_creds)) return creds def locked_put(self, credentials): self.r.set(self.key, credentials.to_json()) def locked_delete(self): self.r.delete(self.key)
class RedisCredStorage(oauth2client.client.Storage): def __init__(self, key, lock, expires): self.r = DARedis() self.key = key self.lockkey = lock self.expires = expires def acquire_lock(self): pipe = self.r.pipeline() pipe.set(self.lockkey, 1) pipe.expire(self.lockkey, 5) pipe.execute() def release_lock(self): self.r.delete(self.lockkey) def locked_get(self): json_creds = self.r.get(self.key) creds = None if json_creds is not None: self.r.expire(self.key, self.expires) json_creds = json_creds.decode() try: creds = oauth2client.client.Credentials.new_from_json(json_creds) except: log("RedisCredStorage: could not read credentials from " + str(json_creds)) return creds def locked_put(self, credentials): if self.expires: pipe = self.r.pipeline() pipe.set(self.key, credentials.to_json()) pipe.expire(self.key, self.expires) pipe.execute() else: self.r.set(self.key, credentials.to_json()) def locked_delete(self): self.r.delete(self.key)
def delete_credentials(self): """Deletes the stored credentials.""" self._setup() r = DARedis() r.delete(self._get_redis_key()) storage = self._get_redis_cred_storage() storage.locked_delete()
def fetch_philadox_info(self): r = DARedis() while r.get('using_philadox') is not None: time.sleep(5) pipe = r.pipeline() pipe.set('using_philadox', 1) pipe.expire('using_philadox', 120) pipe.execute() tdir = tempfile.mkdtemp() info = urllib.quote(json.dumps([self.address['number'], self.address['direction'], self.address['street'], tdir, get_config('philadox username'), get_config('philadox password')])) step = ['casperjs', DAStaticFile(filename='eagleweb.js').path(), info] result = subprocess.call(step) r.delete('using_philadox') if result != 0: raise Exception("Failed to fetch Philadox information") outfiles = [] for pdf_file in sorted([f for f in os.listdir(tdir) if f.endswith('.pdf')]): new_file = DAFile() new_file.set_random_instance_name() new_file.initialize(filename=pdf_file) new_file.copy_into(os.path.join(tdir, pdf_file)) new_file.retrieve() new_file.commit() outfiles.append(new_file) self.philadox_files = outfiles
def get_credentials(self): self._setup() r = DARedis() r_key = self._get_redis_key() stored_state = r.get(r_key) if stored_state is not None and stored_state.decode() == 'None': stored_state = None if stored_state is not None: if 'code' in self.url_args and 'state' in self.url_args: r.delete(r_key) if self.url_args['state'] != stored_state.decode(): raise Exception("State did not match. " + repr(self.url_args['state']) + " vs " + repr(stored_state.decode()) + " where r_key is " + repr(r_key)) flow = self._get_flow() credentials = flow.step2_exchange(self.url_args['code']) storage = self._get_redis_cred_storage() storage.put(credentials) del self.url_args['code'] del self.url_args['state'] else: message("Please wait.", "You are in the process of authenticating.", dead_end=True) storage = self._get_redis_cred_storage() credentials = storage.get() if not credentials or credentials.invalid: state_string = safeid(user_info().filename + '^' + random_string(8)) pipe = r.pipeline() pipe.set(r_key, state_string) pipe.expire(r_key, 300) pipe.execute() flow = self._get_flow() uri = flow.step1_get_authorize_url(state=state_string) if 'state' in self.url_args: del self.url_args['state'] if 'code' in self.url_args: del self.url_args['code'] response(url=uri) return credentials
def delete_credentials(self): """Deletes the stored credentials.""" r = DARedis() r.delete('da:' + self.appname + ':status:user:' + user_info().email) storage = RedisCredStorage(self.appname) storage.locked_delete()