async def save_subscription_to_cache(self) -> None: """Save a subscription to the cache.""" r = await utils.get_aredis_for_cache() await r.setex( "subscription-cache-owner-%s" % self.owner_id, 3600, crypto.encrypt(json.dumps(self.to_dict()).encode()), )
async def save_subscription_to_cache(self) -> None: """Save a subscription to the cache.""" await self.redis.setex( self._cache_key(self.owner_id), self.RETENTION_SECONDS, crypto.encrypt(json.dumps(self.to_dict()).encode()), ) self.ttl = self.RETENTION_SECONDS
def test_key_rotation( cleanup_secrets: None, monkeypatch: pytest.MonkeyPatch, ) -> None: x = "this is an amazing string, right? 🙄".encode() monkeypatch.setattr(config, "CACHE_TOKEN_SECRET", "old password") importlib.reload(crypto) # regen digest with new secret encryped_old = crypto.encrypt(x) monkeypatch.setattr(config, "CACHE_TOKEN_SECRET", "new password") monkeypatch.setattr(config, "CACHE_TOKEN_SECRET_OLD", "old password") importlib.reload(crypto) # regen digest with new secrets encryped_new = crypto.encrypt(x) assert encryped_new != encryped_old assert x == crypto.decrypt(encryped_new) assert x == crypto.decrypt(encryped_old)
async def save_to_cache(self) -> None: """Save a tokens to the cache.""" await self.redis.setex( self._cache_key(self.owner_id), self.RETENTION_SECONDS, crypto.encrypt(json.dumps({ "tokens": self.tokens }).encode()), ) self.ttl = self.RETENTION_SECONDS
async def save_to_cache(self) -> None: """Save an application to the cache.""" await self.redis.setex( self._cache_key( self.api_access_key, None if self.account_scope is None else self.account_scope["login"], ), self.RETENTION_SECONDS, crypto.encrypt( json.dumps( CachedApplication({ "id": self.id, "name": self.name, "api_access_key": self.api_access_key, "api_secret_key": self.api_secret_key, "account_scope": self.account_scope, })).encode()), ) self.ttl = self.RETENTION_SECONDS
def test_encrypt() -> None: x = "this is an amazing string, right? 🙄".encode() assert x == crypto.decrypt(crypto.encrypt(x)) assert x == crypto.decrypt(crypto.encrypt(x)) assert x == crypto.decrypt( crypto.encrypt(crypto.decrypt(crypto.encrypt(x))))