def create_user(self, key, name, user_name, user_id, manager_id=None):
     batch_list, signature = create_user(
         txn_key=key,
         batch_key=self._key,
         name=name,
         user_name=user_name,
         user_id=user_id,
         metadata=uuid4().hex,
         manager_id=manager_id,
     )
     self._client.send_batches(batch_list)
     return self._client.get_statuses([signature], wait=10)
예제 #2
0
async def create_new_user(request):
    required_fields = ["name", "username", "password"]
    utils.validate_fields(required_fields, request.json)

    # Generate keys
    private_key = Secp256k1PrivateKey.new_random()
    txn_key = Key(private_key.as_hex())
    encrypted_private_key = encrypt_private_key(
        request.app.config.AES_KEY, txn_key.public_key, private_key.as_bytes()
    )

    # Build create user transaction
    batch_list = create_user(
        txn_key,
        request.app.config.BATCHER_KEY_PAIR,
        request.json.get("name"),
        request.json.get("username"),
        txn_key.public_key,
        request.json.get("metadata"),
        request.json.get("manager"),
    )

    # Submit transaction and wait for complete
    await utils.send(
        request.app.config.VAL_CONN, batch_list[0], request.app.config.TIMEOUT
    )

    # Save new user in auth table
    hashed_password = hashlib.sha256(
        request.json.get("password").encode("utf-8")
    ).hexdigest()

    auth_entry = {
        "user_id": txn_key.public_key,
        "hashed_password": hashed_password,
        "encrypted_private_key": encrypted_private_key,
        "user_name": request.json.get("username"),
        "email": request.json.get("email"),
    }
    await auth_query.create_auth_entry(request.app.config.DB_CONN, auth_entry)

    # Send back success response
    return create_user_response(request, txn_key.public_key)
예제 #3
0
    def create_user_transaction(self, name, username, metadata):
        txn_private_key = Secp256k1PrivateKey.new_random()
        batch_private_key = Secp256k1PrivateKey.new_random()

        txn_key = Key(txn_private_key.as_hex())
        batch_key = Key(batch_private_key.as_hex())

        batchlist, sig = create_user(
            txn_key, batch_key, name, username, txn_key.public_key, metadata
        )

        self.assertEqual(type(sig), str)
        self.assertEqual(type(batchlist), batch_pb2.BatchList)

        self.assertEqual(len(sig), SIGNATURE_LENGTH)
        self.assertTrue(SIGNATURE_PATTERN.match(sig))

        batch_count = 0
        for batch in batchlist.batches:
            batch_count += 1
            self.assertEqual(type(batch), batch_pb2.Batch)
            self.assertEqual(type(batch.header_signature), str)
            self.assertEqual(len(batch.header_signature), SIGNATURE_LENGTH)
            self.assertTrue(SIGNATURE_PATTERN.match(batch.header_signature))

            trans_count = 0
            for transaction in batch.transactions:
                trans_count += 1
                self.assertEqual(type(transaction.header), bytes)
                self.assertEqual(type(transaction.header_signature), str)
                self.assertEqual(type(transaction.payload), bytes)

                self.assertEqual(len(transaction.header_signature), SIGNATURE_LENGTH)
                self.assertTrue(SIGNATURE_PATTERN.match(transaction.header_signature))

                header = transaction_pb2.TransactionHeader()
                header.ParseFromString(transaction.header)

                self.assertEqual(type(header), transaction_pb2.TransactionHeader)
                self.assertEqual(header.family_name, addresser.FAMILY_NAME)
                self.assertEqual(header.family_version, addresser.FAMILY_VERSION)
                self.assertEqual(header.batcher_public_key, batch_key.public_key)
                self.assertEqual(header.signer_public_key, txn_key.public_key)

                self.assertEqual(len(header.payload_sha512), SIGNATURE_LENGTH)
                self.assertTrue(SIGNATURE_PATTERN.match(header.payload_sha512))

                input_count = 0
                for address in header.inputs:
                    input_count += 1
                    self.assertEqual(type(address), str)
                    self.assertEqual(len(address), addresser.ADDRESS_LENGTH)
                    self.assertEqual(
                        addresser.address_is(address), addresser.AddressSpace.USER
                    )

                self.assertEqual(input_count, 1)

                output_count = 0
                for address in header.outputs:
                    output_count += 1
                    self.assertEqual(type(address), str)
                    self.assertEqual(len(address), addresser.ADDRESS_LENGTH)
                    self.assertEqual(
                        addresser.address_is(address), addresser.AddressSpace.USER
                    )

                self.assertEqual(output_count, 1)

                payload = rbac_payload_pb2.RBACPayload()
                payload.ParseFromString(transaction.payload)

                self.assertEqual(type(payload), rbac_payload_pb2.RBACPayload)
                self.assertEqual(
                    payload.message_type, rbac_payload_pb2.RBACPayload.CREATE_USER
                )
                self.assertEqual(type(payload.content), bytes)

                user = user_transaction_pb2.CreateUser()
                user.ParseFromString(payload.content)

                self.assertEqual(type(user), user_transaction_pb2.CreateUser)
                self.assertEqual(user.name, name)
                self.assertEqual(user.user_name, username)
                self.assertEqual(type(user.user_id), str)
                self.assertEqual(user.user_id, txn_key.public_key)

            self.assertEqual(trans_count, 1)

        self.assertEqual(batch_count, 1)