def _get_redis_cred_storage(self): if hasattr(self, 'unique_id'): key = 'da:' + self.appname + ':uniqueid:' + str(self.unique_id) lock = 'da:' + self.appname + ':lock:uniqueid:' + str(self.unique_id) else: key = 'da:' + self.appname + ':user:'******'da:' + self.appname + ':lock:user:' + user_info().email return RedisCredStorage(key, lock, self.expires)
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
def definable(the_variable): """Test if the given variable (as a string) can be defined in the context of the current interview.""" interview = get_interview(user_info().filename) status = InterviewStatus() try: variables = all_variables(simplify=False) result = interview.askfor(the_variable, variables, variables, status) except Exception as err: result = err return isinstance(result, dict)
def get_user_id() -> str: """ Returns a unique ID for the current user If the user is logged in, uses their user id, otherwise uses session id. """ info = get_user_info() if info: return str(info["id"]) else: return str(user_info().session)
def short_url(): info = user_info() url = None for key, val in get_config('dispatch').iteritems(): interview_name = re.sub(r'\:([^\/]+)$', r':data/questions/\1', val) if interview_name == info.filename: url = '%sstart/%s?session=%s' % (url_of( 'root', _external=True), key, info.session) break if url is None: url = interview_url() return url
def analyze(): conn = variables_snapshot_connection() cur = conn.cursor() cur.execute( "select data->>'favorite_fruit' from jsonstorage where filename='" + user_info().filename + "'") counts = dict() for record in cur.fetchall(): fruit = record[0].lower() if fruit not in counts: counts[fruit] = 0 counts[fruit] += 1 conn.close() return counts
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()
def __init__(self, app): self.r = DARedis() self.key = 'da:' + app + ':user:'******'da:' + app + ':lock:user:' + user_info().email
def _get_redis_key(self): if hasattr(self, 'unique_id'): return 'da:' + self.appname + ':status:uniqueid:' + str(self.unique_id) return 'da:' + self.appname + ':status:user:' + user_info().email
def save(self): data = dict(self.data) data['session'] = user_info().session data['filename'] = user_info().filename data['start_time'] = start_time().astimezone() store_variables_snapshot(data, key=self.key)