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.
master
Tom Hacohen 4 years ago
parent 50f89c48e2
commit ca7f2ec73c

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

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

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

Loading…
Cancel
Save