def listen(request): username = '******' if request.user.is_authenticated(): username = request.user.username request = eval(request.path.split('n/')[-1]) for e in request: b = Keys(user=username, key_name=e['k'], release=e['r'], timestamp=e['t']) b.save() if request: pass #print >>file, request #file.close() return HttpResponse(request)
def get_keys_by_username_and_id(self, username, device_id): preKeys = list() my_key = Keys.objects.filter( username=username, device_id=device_id).order_by('key_id').first() if my_key: key_dict = model_to_dict(my_key) new_key = Keys(**key_dict) preKeys.append(new_key) if not my_key.last_resort: my_key.delete() return preKeys return None
def save_key(self, request): user = request.user assert isinstance(user, MyUser) device = user.get_authenticated_device() updateAccount = False serializer = PreKeyStateSerializer(data=request.data) if serializer.is_valid(): prekeystate = serializer.save() if isinstance(prekeystate, PreKeyState): if not (prekeystate.signedPreKey == device.signedPreKey): device.signedPreKey = prekeystate.signedPreKey updateAccount = True if not (prekeystate.identityKey == request.user.get_identity_key()): user = user.set_identity_key(prekeystate.identityKey) updateAccount = True if updateAccount: device_serializer = DeviceRecordSerializer(device) try: user.set_authenticated_device(device, device_serializer.data) except AttributeError, TypeError: return Response(status=400, data={'error': 'invalid request'}) user.save() old_key_record = Keys.objects.filter(username=user.get_username(), device_id=device.device_id) if old_key_record.count() > 0: for record in old_key_record: record.delete() for key in prekeystate.preKeys: try: new_key = Keys(username=user.get_username(), device_id=key.get('deviceId', 1), key_id=key.get('keyId'), public_key=key.get('publicKey'), last_resort=False) new_key.save() except: return Response(status=204, data={"error": key}) new_last_resort_key = Keys( username=user.get_username(), device_id=prekeystate.lastResortKey.get('deviceId', 1), key_id=prekeystate.lastResortKey.get('keyId'), public_key=prekeystate.lastResortKey.get('publicKey'), last_resort=True) new_last_resort_key.save() return Response(status=201)
def put(self, request, format=None): current_user = request.user assert isinstance(current_user, MyUser) device = current_user.get_authenticated_device() if device.device_id and Keys.get_available_keys_count( current_user, device.device_id) > 15: return Response(data={'message': 'you have enough prekeys'}, status=400) updateAccount = False serializer = PreKeyStateSerializerV2(data=request.data) if serializer.is_valid(): prekey_object = serializer.save() assert isinstance(prekey_object, PreKeyState) if not (prekey_object.signedPreKey == device.signedPreKey): device.signedPreKey = prekey_object.signedPreKey device_serializer = DeviceRecordSerializer(device) # user.authenticated_device = json.dumps(deviceSerializer.data) try: current_user.set_authenticated_device( device, device_serializer.data) # except AttributeError, TypeError: # return Response(status=400, data={'error': 'invalid request'}) except Exception as e: return Response(status=400, data={'error': 'invalid request'}) updateAccount = True if not (prekey_object.identityKey == request.user.get_identity_key()): current_user.set_identity_key(prekey_object.identityKey) updateAccount = True if updateAccount: current_user.save() Keys.store_keys(current_user.get_username(), device.device_id, prekey_object.preKeys, prekey_object.lastResortKey) return Response(data=serializer.data, status=status.HTTP_201_CREATED) return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_key_by_username(self, username): my_key_dict = Keys.objects.filter(username=username)\ .distinct('username', 'device_id').order_by('username', 'device_id', 'key_id') if my_key_dict.count() > 0: preKeys = list() for my_key in my_key_dict: key_dict = model_to_dict(my_key) new_key = Keys(**key_dict) preKeys.append(new_key) if not my_key.last_resort: my_key.delete() return preKeys return None
def put(self, request, format=None): current_user = request.user assert isinstance(current_user, MyUser) device = current_user.get_web_client() # updateAccount = False if Keys.get_available_keys_count(current_user, 2) > 15: return Response(data={'message': 'you have enough prekeys'}, status=status.HTTP_400_BAD_REQUEST) serializer = PreKeyStateSerializerV2(data=request.data) if serializer.is_valid(): prekey_object = serializer.save() assert isinstance(prekey_object, PreKeyState) # if not (prekey_object.signedPreKey == device.signedPreKey): # device.signedPreKey = prekey_object.signedPreKey # device_serializer = DeviceRecordSerializer(device) # # user.authenticated_device = json.dumps(deviceSerializer.data) # try: # device.set_as_web_authenticated_client(current_user) # except AttributeError, TypeError: # return Response(status=400, data={'error': 'invalid request'}) # updateAccount = True # old_identity_key_model = current_user.web_id_key_store.filter( # type=WebIdentityKeyStoreModel.IDENTITY_KEY_STORE_IDENTITY_KEYPAIR).first() # identity_store = WebIdentityKeyStore(current_user) # if not (prekey_object.identityKey == identity_store.getIdentityKeyPair().getPublicKey().getPublicKey()): # # current_user.set_identity_key(prekey_object.identityKey) # updateAccount = True # if updateAccount: # current_user.save() Keys.store_keys(current_user.get_username(), device.device_id, prekey_object.preKeys, prekey_object.lastResortKey) return Response(data=serializer.data, status=status.HTTP_201_CREATED) return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): prekey_count = Keys.get_available_keys_count(request.user, 2) return Response(data={'count': prekey_count}, status=status.HTTP_200_OK)