Package: redis-entities
RedisEntitiesHow to install it?You can install RedisEntites from this Github repository withpython3 setup.py install,
or just install it directly from pypi withpip3 install redis-entities.What is it?Redis Entities is a small library that allows you to map represent certainentitiesin Redis.
AnEntitycould be for example a Hashmap type and stores information about one User that currently requested a
password reset. Each Entity has a predefined Prefix used to differentiate the different types of Entities you create.
There are 3 different RedisEntities you can use:RedisListEntityRedisSetEntityRedisHashmapEntityIn addition there are a couple of mixins, which can be used to provide additional functionality to your entities.
There are 3 different Mixins you can use:HashedIdentifierMixin: All identifiers are hashed before stored in RedisAuthenticatedEncryptionMixin: All values are encrypted and authenticated before stored in RedisDeterministicAuthenticatedEncryptionMixin: All values are encrypted and authenticated deterministically (the same value is encrypted to the same ciphertext) before stored in RedisYou can also combine multiple Mixins, exceptAuthenticatedEncryptionMixinandDeterministicAuthenticatedEncryptionMixin.How does it work?To create an own Entity, just subclass from one of the Provided RedisEntity Base classes.Note, that every Entity has to set theRedisClientClass Attribute to an instances ofRedisfromredis-py, or
a class that supports all methods thatRedisfromredis-pydoes.Under the hood all keys are stored as strings, and all values are stored as bytes in Redis.RedisListEntity:importredisfromredis_entitiesimportRedisListEntityfromredis_entities.mixinsimportAuthenticatedEncryptionMixinclassJobQueueEntity(RedisListEntity,AuthenticatedEncryptionMixin):RedisClient=redis.Redis(...)Prefix="JobQueue"AesKey=b"deaddeaddeaddeaddeaddeaddeaddead"# AES-128 in this caseJobQueueEntity.lpush("Worker1","command1")assertJobQueueEntity.length("Worker1")==1Supported Methods are:lpushbrpoplindexlengthclearRedisSetEntity:importredisfromredis_entitiesimportRedisSetEntityfromredis_entities.mixinsimportDeterministicAuthenticatedEncryptionMixinclassAccessTokensEntity(RedisSetEntity,DeterministicAuthenticatedEncryptionMixin):RedisClient=redis.Redis(...)Prefix="AccessTokens"Expire=180AesKey=b"deaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddead"# need to be twice the size of the key required by the underlying cipher (e.g. 64 bytes for AES-256)AccessTokensEntity.add("User1","Token1")AccessTokensEntity.add("User2","Token1")assertAccessTokensEntity.exists("User1","Token1")isTrueSupported Methods are:adddeleteclearexistslist_alllengthNOTE: It is strongly recommended to use theDeterministicAuthenticatedEncryptionMixininstead of theAuthenticatedEncryptionMixinwhen you are working withRedisSetEntity, as most methods need to know the exact value that is stored in Redis. With theDeterministicAuthenticatedEncryptionMixin, the same plaintext results in the same ciphertext and thus makes this possible.RedisHashmapEntityimportredisfromredis_entitiesimportRedisHashmapEntity,fromredis_entities.mixinsimportHashedIdentifierMixin,AuthenticatedEncryptionMixinclassVerifyEmailTokens(RedisHashmapEntity,HashedIdentifierMixin,AuthenticatedEncryptionMixin):RedisClient=redis.Redis(...)Prefix="VerifyEmailTokens"Contents=("MandatoryKey1","MandatoryKey2")Expire=180HashName="sha512_256"Salt=b"VerifyEmailTokens"AesKey=b"deaddeaddeaddeaddeaddeaddeaddead"VerifyEmailTokens.store("test@example.com",MandatoryKey1="Value1",MandatoryKey2="Value2")loaded_entity=VerifyEmailTokens.load("test@example.com")assertloaded_entity.MandatoryKey1==b"Value1"assertloaded_entity.MandatoryKey1==b"Value2"assertVerifyEmailTokens.exists("test@example.com")isTrueSupported Methods are:storeloadexistsdeletelength