def consume(self, userid: str, token: str, registry=None): key_name = self.get_key(userid, token) conn = get_redis_conn(registry) cred_token = conn.get(key_name) if cred_token: cred_token = cred_token.decode() return Credentials.load(userid, cred_token, registry)
def create(self, credentials, expires=30, registry=None): assert isinstance(credentials, Credentials) # Test one_time_token = _generate_token() key_name = self.get_key(credentials.userid, one_time_token) conn = get_redis_conn(registry) conn.setex(key_name, expires, credentials.token) return one_time_token
def consume(self, token: str, registry=None): key_name = self.get_key(token) conn = get_redis_conn(registry) payload = conn.get(key_name) if payload: payload = payload.decode() return json.loads(payload)
def load(cls, userid, token, registry=None): if registry is None: registry = get_current_registry() key = "{}.{}.{}".format(cls.prefix, userid, token) conn = get_redis_conn(registry) payload = conn.get(key) if payload: payload = payload.decode() data = json.loads(payload) inst = cls(registry=registry, **data) inst.reset_expire() return inst
def create(self, payload: dict, expires: int = 1200, registry=None): token = _generate_token(length=70) conn = get_redis_conn(registry) key_name = self.get_key(token) conn.setex(key_name, expires, json.dumps(payload)) return token
def validate(self, userid: str, token: str, registry=None): key_name = self.get_key(userid, token) conn = get_redis_conn(registry) return bool(conn.exists(key_name))
def validate(self, token: str, registry=None): conn = get_redis_conn(registry) key_name = self.get_key(token) return bool(conn.exists(key_name))
def _conn(self): return get_redis_conn(self.registry)