def new_for_user(cls, user_id): str_user_id = str(user_id) shard_id = MySQL.get_shard_id_for_string(str_user_id) id = MySQL.next_id(shard_id) secret = RandomToken.build(16) return cls(id, str_user_id, secret, user_id, True)
def new_session(self, auth, user_agent_string, log_out_ts=None, session_flags=None): user = auth.user new_session_id = MySQL.next_id(Id(user.id).get_shard_id()) try: user_agent = self._get_user_agent_by_string(user_agent_string) except UserAgentNotFoundException: shard_id = MySQL.get_shard_id_for_string(user_agent_string) id = MySQL.next_id(shard_id) user_agent = UserAgent(id, user_agent_string) self._save_user_agent(user_agent) return Session(new_session_id, user, user_agent, auth, log_out_ts=log_out_ts, flags=session_flags)
def _get_user_agent_by_string(self, user_agent_string): shard_id = MySQL.get_shard_id_for_string(user_agent_string) hash_ = UserAgent.generate_hash(user_agent_string) rows = MySQL.get_by_shard_id(shard_id).query("SELECT * FROM user_agents WHERE user_agent_hash = %s", (hash_,)) if not len(rows): raise UserAgentNotFoundException() row = rows[0] if row['id'] not in self._user_agents: self._user_agents[row['id']] = UserAgent(row['id'], row['user_agent_string'].decode("utf-8")) return self._user_agents[row['id']]
def _get_user_agent_by_string(self, user_agent_string): shard_id = MySQL.get_shard_id_for_string(user_agent_string) hash_ = UserAgent.generate_hash(user_agent_string) rows = MySQL.get_by_shard_id(shard_id).query( "SELECT * FROM user_agents WHERE user_agent_hash = %s", (hash_, )) if not len(rows): raise UserAgentNotFoundException() row = rows[0] if row['id'] not in self._user_agents: self._user_agents[row['id']] = UserAgent( row['id'], row['user_agent_string'].decode("utf-8")) return self._user_agents[row['id']]
def get_auth_by_provider_id(self, auth_class, provider_id): type_id = self._class_to_type_id(auth_class) shard_id = MySQL.get_shard_id_for_string(provider_id) rows = MySQL.get_by_shard_id(shard_id).query("SELECT * FROM auth WHERE provider_id=%s AND provider_type=%s", (provider_id, type_id)) if not len(rows): raise NoAuthFoundException(provider_id) row = rows[0] try: auth = self._row_to_model(row) except RowDeletedException: raise NoAuthFoundException(provider_id) auth.update_stored_state() return auth
def get_auth_by_provider_id(self, auth_class, provider_id): type_id = self._class_to_type_id(auth_class) shard_id = MySQL.get_shard_id_for_string(provider_id) rows = MySQL.get_by_shard_id(shard_id).query( "SELECT * FROM auth WHERE provider_id=%s AND provider_type=%s", (provider_id, type_id)) if not len(rows): raise NoAuthFoundException(provider_id) row = rows[0] try: auth = self._row_to_model(row) except RowDeletedException: raise NoAuthFoundException(provider_id) auth.update_stored_state() return auth
def _generate_id_from_provider_id(self, provider_id): shard_id = MySQL.get_shard_id_for_string(provider_id) return MySQL.next_id(shard_id)