def main():
    with open('data/hospitals.txt') as f:
        hospitals = [line.strip() for line in f.readlines()
                     if line.strip()][:2]

    valid_token = jwt.encode(
        {
            'name': PATIENT_NAME,
            'id': PATIENT_ID,
            'exp': int(time.time()) + 60,
        },
        PRIVATE_KEY,
        algorithm='RS256').decode('utf-8')

    uid = PATIENT_NAME + PATIENT_ID

    print(f'[*] Unregistering uid={uid} at {hospitals[0]}...')
    res = succeed('http://localhost:8100/patient_unreg', {
        'uid': uid,
        'auth_token': valid_token,
    })
    print(f'    Result obtained: {res}')
    return True
Example #2
0
def main():
    with open('data/hospitals.txt') as f:
        hospitals = [line.strip() for line in f.readlines() if line.strip()]
        slugs = [re.sub('[^a-z]+', '-', name.lower()) for name in hospitals]
        num_hospitals = len(hospitals)

    if num_hospitals < 2:
        print('There needs to be at least 2 hospitals running.')
        return False

    # Create JWT token; one valid and one invalid
    valid_token = jwt.encode({
        'name': PATIENT_NAME,
        'id': PATIENT_ID,
        'exp': int(time.time()) + 60,
    }, PRIVATE_KEY, algorithm='RS256').decode('utf-8')

    invalid_token = jwt.encode({
        'name': PATIENT_NAME,
        'id': PATIENT_ID,
        'exp': int(time.time()) - 60,
    }, PRIVATE_KEY, algorithm='RS256').decode('utf-8')

    # Register patient at hospital A
    print(f'[*] Registering name={PATIENT_NAME} id={PATIENT_ID} at {hospitals[0]}...')
    res = succeed('http://localhost:8100/patient_reg', {
        'id': PATIENT_ID,
        'name': PATIENT_NAME,
        'pub_key': PUBLIC_KEY,
    })

    print(f'    Result obtained: {res}')
    patient_uid = res['uid']

    # Attempt to transfer from hospital B, there should be an error
    print(f'[*] Attempting to transfer uid={patient_uid} from {hospitals[1]} to {hospitals[0]}...')
    res = fail('http://localhost:8101/patient_transfer', {
        'uid': patient_uid,
        'auth_token': valid_token,
        'dest_hospital': slugs[0],
    })
    print(f'    Exception obtained: {res}')

    # Attempt to transfer from hospital A with an invalid token
    print(f'[*] Attempting to transfer uid={patient_uid} from {hospitals[0]} to {hospitals[1]} with invalid token...')
    res = fail('http://localhost:8100/patient_transfer', {
        'uid': patient_uid,
        'auth_token': invalid_token,
        'dest_hospital': slugs[1],
    })
    print(f'    Exception obtained: {res}')

    # Attempt to transfer from hospital A to hospital A
    print(f'[*] Attempting to transfer uid={patient_uid} from {hospitals[0]} to {hospitals[0]}...')
    res = fail('http://localhost:8100/patient_transfer', {
        'uid': patient_uid,
        'auth_token': valid_token,
        'dest_hospital': slugs[0],
    })
    print(f'    Exception obtained: {res}')

    # Now transfer from hospital A to hospital B for real
    print(f'[*] Transferring uid={patient_uid} from {hospitals[0]} to {hospitals[1]} for real this time...')
    res = succeed('http://localhost:8100/patient_transfer', {
        'uid': patient_uid,
        'auth_token': valid_token,
        'dest_hospital': slugs[1],
    })
    print(f'    Result obtained: {res}')

    # Try transferring again?
    print(f'[*] Transferring uid={patient_uid} from {hospitals[0]} to {hospitals[1]} again!')
    res = fail('http://localhost:8100/patient_transfer', {
        'uid': patient_uid,
        'auth_token': valid_token,
        'dest_hospital': slugs[1],
    })
    print(f'    Exception obtained: {res}')

    # Attempt to unregister at hospital A
    print(f'[*] Attempting to unregister uid={patient_uid} at {hospitals[0]}...')
    res = fail('http://localhost:8100/patient_unreg', {
        'uid': patient_uid,
        'auth_token': valid_token,
    })
    print(f'    Exception obtained: {res}')

    # Transfer from hospital B back to hospital A
    print(f'[*] Transferring uid={patient_uid} from {hospitals[1]} to {hospitals[0]}...')
    res = succeed('http://localhost:8101/patient_transfer', {
        'uid': patient_uid,
        'auth_token': valid_token,
        'dest_hospital': slugs[0],
    })
    print(f'    Result obtained: {res}')

    # Now we can unregister at hospital A
    print(f'[*] Unregistering uid={patient_uid} at {hospitals[0]}...')
    res = succeed('http://localhost:8100/patient_unreg', {
        'uid': patient_uid,
        'auth_token': valid_token,
    })
    print(f'    Result obtained: {res}')

    print()

    print('Test successful!')
    return True
Example #3
0
def main():
    with open('data/hospitals.txt') as f:
        hospitals = [line.strip() for line in f.readlines() if line.strip()]
        num_hospitals = len(hospitals)

    if num_hospitals < 2:
        print('There needs to be at least 2 hospitals running.')
        return False

    # Register patient at hospital A
    print(
        f'[*] Registering name={PATIENT_NAME} id={PATIENT_ID} at {hospitals[0]}...'
    )
    res = succeed('http://localhost:8100/patient_reg', {
        'id': PATIENT_ID,
        'name': PATIENT_NAME,
        'pub_key': PUBLIC_KEY,
    })
    print(f'    Result obtained: {res}')

    patient_uid = res['uid']

    # Attempt to register patient at hospital B
    print(
        f'[*] Registering name={PATIENT_NAME} id={PATIENT_ID} at {hospitals[1]}...'
    )
    res = fail('http://localhost:8101/patient_reg', {
        'id': PATIENT_ID,
        'name': PATIENT_NAME,
        'pub_key': PUBLIC_KEY,
    })
    print(f'    Exception obtained: {res}')

    # Read patient card from hospital A
    print(
        f'[*] Fetching patient data for name={PATIENT_NAME} id={PATIENT_ID} from {hospitals[0]}...'
    )
    res = succeed('http://localhost:8100/patient_read', {
        'uid': patient_uid,
    })
    data_len = len(res['data'])
    print(f'    Result obtained: data=[{data_len} items]')

    # Decrypt patient data
    print(
        f'[*] Decrypting patient data for name={PATIENT_NAME} id={PATIENT_ID} from {hospitals[0]}...'
    )
    priv_key = rsa.PrivateKey.load_pkcs1(PRIVATE_KEY)
    for data in res['data']:
        data = base64.b64decode(data.encode('utf-8'))
        decrypted = rsa.decrypt(data, priv_key).decode('utf-8')
        print(f'    Result obtained: {decrypted}')

    # Clean up: Unregister patient
    token = jwt.encode(
        {
            'name': PATIENT_NAME,
            'id': PATIENT_ID,
            'exp': int(time.time()) + 60,
        },
        PRIVATE_KEY,
        algorithm='RS256').decode('utf-8')

    print(f'[*] Unregistering uid={patient_uid} at {hospitals[0]}...')
    res = succeed('http://localhost:8100/patient_unreg', {
        'uid': patient_uid,
        'auth_token': token,
    })
    print(f'    Result obtained: {res}')

    print()

    print('Test successful!')
    return True