When converting from ORM convert binary fields to bytes.

The problem is that some ORMs return memoryview which are more efficient but
are not supported by pydantic at the moment.
This commit is contained in:
Tom Hacohen 2020-12-28 15:08:03 +02:00
parent 50f89c48e2
commit ca7f2ec73c
3 changed files with 14 additions and 9 deletions

View File

@ -25,7 +25,7 @@ from django_etebase.token_auth.models import get_default_expiry
from django_etebase.utils import create_user, get_user_queryset, CallbackContext
from .exceptions import AuthenticationFailed, transform_validation_error, HttpError
from .msgpack import MsgpackRoute
from .utils import BaseModel, permission_responses, msgpack_encode, msgpack_decode
from .utils import BaseModel, permission_responses, msgpack_encode, msgpack_decode
User = get_user_model()
token_scheme = APIKeyHeader(name="Authorization")
@ -63,7 +63,7 @@ class UserOut(BaseModel):
@classmethod
def from_orm(cls: t.Type["UserOut"], obj: User) -> "UserOut":
return cls(pubkey=obj.userinfo.pubkey, encryptedContent=obj.userinfo.encryptedContent)
return cls(pubkey=bytes(obj.userinfo.pubkey), encryptedContent=bytes(obj.userinfo.encryptedContent))
class LoginOut(BaseModel):
@ -228,14 +228,15 @@ async def is_etebase():
@authentication_router.post("/login_challenge/", response_model=LoginChallengeOut)
async def login_challenge(user: User = Depends(get_login_user)):
enc_key = get_encryption_key(user.userinfo.salt)
salt = bytes(user.userinfo.salt)
enc_key = get_encryption_key(salt)
box = nacl.secret.SecretBox(enc_key)
challenge_data = {
"timestamp": int(datetime.now().timestamp()),
"userId": user.id,
}
challenge = bytes(box.encrypt(msgpack_encode(challenge_data), encoder=nacl.encoding.RawEncoder))
return LoginChallengeOut(salt=user.userinfo.salt, challenge=challenge, version=user.userinfo.version)
return LoginChallengeOut(salt=salt, challenge=challenge, version=user.userinfo.version)
@authentication_router.post("/login/", response_model=LoginOut)

View File

@ -58,7 +58,7 @@ class CollectionItemRevisionInOut(BaseModel):
chunks.append((chunk_obj.uid, f.read()))
else:
chunks.append((chunk_obj.uid,))
return cls(uid=obj.uid, meta=obj.meta, deleted=obj.deleted, chunks=chunks)
return cls(uid=obj.uid, meta=bytes(obj.meta), deleted=obj.deleted, chunks=chunks)
class CollectionItemCommon(BaseModel):
@ -103,8 +103,8 @@ class CollectionOut(CollectionCommon):
member: models.CollectionMember = obj.members.get(user=context.user)
collection_type = member.collectionType
ret = cls(
collectionType=collection_type and collection_type.uid,
collectionKey=member.encryptionKey,
collectionType=collection_type and bytes(collection_type.uid),
collectionKey=bytes(member.encryptionKey),
accessLevel=member.accessLevel,
stoken=obj.stoken,
item=CollectionItemOut.from_orm_context(obj.main_item, context),

View File

@ -32,6 +32,10 @@ class UserInfoOut(BaseModel):
class Config:
orm_mode = True
@classmethod
def from_orm(cls: t.Type["UserInfoOut"], obj: models.UserInfo) -> "UserInfoOut":
return cls(pubkey=bytes(obj.pubkey))
class CollectionInvitationAcceptIn(BaseModel):
collectionType: bytes
@ -69,8 +73,8 @@ class CollectionInvitationOut(CollectionInvitationCommon):
username=obj.user.username,
collection=obj.collection.uid,
fromUsername=obj.fromMember.user.username,
fromPubkey=obj.fromMember.user.userinfo.pubkey,
signedEncryptionKey=obj.signedEncryptionKey,
fromPubkey=bytes(obj.fromMember.user.userinfo.pubkey),
signedEncryptionKey=bytes(obj.signedEncryptionKey),
)