From ca7f2ec73cb559f0875e580b81a1e0f18b06c21a Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 28 Dec 2020 15:08:03 +0200 Subject: [PATCH] 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. --- etebase_fastapi/authentication.py | 9 +++++---- etebase_fastapi/collection.py | 6 +++--- etebase_fastapi/invitation.py | 8 ++++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/etebase_fastapi/authentication.py b/etebase_fastapi/authentication.py index 04aec31..2f5a2f1 100644 --- a/etebase_fastapi/authentication.py +++ b/etebase_fastapi/authentication.py @@ -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) diff --git a/etebase_fastapi/collection.py b/etebase_fastapi/collection.py index e6c10c3..74730ff 100644 --- a/etebase_fastapi/collection.py +++ b/etebase_fastapi/collection.py @@ -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), diff --git a/etebase_fastapi/invitation.py b/etebase_fastapi/invitation.py index 39460a9..9e731bc 100644 --- a/etebase_fastapi/invitation.py +++ b/etebase_fastapi/invitation.py @@ -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), )