예제 #1
0
def create(petition: PetitionIn, expand: bool = False, token: str = Security(security)):
    if not allowed_to_control_petition(token):
        raise HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="Not authorized to control this petition",
        )
    try:
        petition_object, ci_uid = _generate_petition_object(petition)
    except FileNotFoundError:
        raise HTTPException(
            status_code=HTTP_404_NOT_FOUND,
            detail="Credential issuer is not validated, missing info/keys/credentials",
        )
    except ConnectionError:
        raise HTTPException(
            status_code=HTTP_424_FAILED_DEPENDENCY,
            detail="Credential issuer server is not available",
        )

    p = Petition(
        petition=petition_object,
        petition_id=petition.petition_id,
        credential_issuer_uid=ci_uid,
        credential_issuer_url=petition.credential_issuer_url,
    )

    try:
        DBSession.add(p)
        DBSession.commit()
    except IntegrityError:
        DBSession.rollback()
        raise HTTPException(
            status_code=HTTP_409_CONFLICT, detail="Duplicate Petition Id"
        )
    return p.publish(expand)
예제 #2
0
async def count(petition_id: str):
    p = Petition.by_pid(petition_id)
    if not p:
        raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Petition not Found")

    if p.status != STATUS.CLOSED:
        raise HTTPException(
            status_code=HTTP_424_FAILED_DEPENDENCY, detail="Petition still open"
        )

    p.count = zencode(CONTRACTS.COUNT_PETITION, keys=p.tally, data=p.petition)
    DBSession.commit()
    return json.loads(p.count)
예제 #3
0
async def sign(petition_id: str, signature: PetitionSignature, expand: bool = False):
    p = Petition.by_pid(petition_id)
    if not p:
        raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Petition not Found")

    try:
        petition = zencode(
            CONTRACTS.ADD_SIGNATURE, keys=p.petition, data=signature.json()
        )
    except Error as e:
        debug(e)
        raise HTTPException(
            status_code=HTTP_424_FAILED_DEPENDENCY,
            detail="Petition signature is duplicate or not valid",
        )
    p.petition = petition

    DBSession.commit()
    return p.publish(expand)
예제 #4
0
def test_sign(client):
    petition = Bunch(
        petition_id="petition",
        credential_issuer_url="https://credentials.decodeproject.eu",
        authorizable_attribute_id="aa_test",
        credential_issuer_petition_value=[
            {
                "name": "zip_code",
                "value": "08001"
            },
            {
                "name": "email",
                "value": "*****@*****.**"
            },
        ],
    )

    vk = _retrieve_verification_key(petition)
    credential = _retrieve_credential(petition)

    petition_signature = zencode(
        CONTRACTS.SIGN_PETITION,
        keys=credential,
        data=json.dumps(vk),
        placeholders={"petition": petition.petition_id},
    )

    print(petition_signature)

    r = client.post(
        f"/petitions/{petition.petition_id}/sign",
        headers={"Authorization": "Bearer %s" % auth()},
        json=json.loads(petition_signature),
    )

    assert r.status_code == 200
    p = Petition.by_pid("petition")
    petition = json.loads(p.petition)
    assert petition["petition"]["scores"]["pos"]["right"] != "Infinity"
    assert petition["petition"]["scores"]["pos"]["left"] != "Infinity"
    assert petition["petition"]["scores"]["neg"]["right"] != "Infinity"
    assert petition["petition"]["scores"]["neg"]["left"] != "Infinity"
예제 #5
0
async def tally(
    petition_id: str,
    authorizable_attribute: TallyBody = Body(...),
    expand: bool = False,
    token: str = Security(security),
):
    if not allowed_to_control_petition(token):
        raise HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="Not authorized to control this petition",
        )
    p = Petition.by_pid(petition_id)
    if not p:
        raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Petition not Found")
    petition = Bunch(
        petition_id=petition_id,
        credential_issuer_url=p.credential_issuer_url,
        authorizable_attribute_id=authorizable_attribute.authorizable_attribute_id,
    )
    credential = _retrieve_credential(petition)
    p.tally = zencode(CONTRACTS.TALLY_PETITION, keys=credential, data=p.petition)
    DBSession.commit()
    return p.publish(expand)
예제 #6
0
async def get_one(petition_id: str, expand: bool = False):
    p = Petition.by_pid(petition_id)
    if not p:
        raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Petition not Found")

    return p.publish(expand)