def create_challenge(auth): demo_print("api", "requesting challenge from backend: Bearer " + auth, 2) response = requests.get("http://127.0.0.1:8080/approach1/users/challenge", headers={"Authorization": "Bearer " + auth}) token = response.json()["token"] demo_print("api", "challenge token generated: " + token, 2) return token
def register_two_users(): demo_print("user a", "registering and authorizing") user_a = authorize_user() demo_print("user b", "registering and authorizing") user_b = authorize_user() print("EXTRA", user_b) return [user_a, user_b]
def user_status(auth): demo_print("api", "retrieving user status with auth: Bearer " + auth, 2) response = requests.get("http://127.0.0.1:8080/users/status", headers={"Authorization": "Bearer " + auth}) data = response.json() demo_print("api", "status retrieved", 2) demo_print("api", "risk: " + str(data["risk"]), 2) demo_print("api", "message: " + str(data["message"]), 2) return response.json()
def simulate_beacon(beacon_key, timestamp): indent = 2 subject = "beacon" demo_print(subject, "beacon requesting response token at {}".format(timestamp), indent) demo_print(subject, "beacon key: " + beacon_key, indent) response = req.get("http://127.0.0.1:8080/beacon/{}/{}".format(beacon_key, timestamp)) if response.status_code == 200: beacon_token = response.json()['token'] demo_print(subject, "retrieved beacon response token: " + beacon_token, indent) return beacon_token else: demo_print(ERROR_SUB, "failed to create beacon transmission, error code: {}".format(response.status_code), indent)
def submit_response(auth, token): demo_print("api", "sending response to backend: Bearer " + auth, 2) demo_print("api", "response token: " + token, 2) requests.post("http://127.0.0.1:8080/approach1/users/response", headers={"Authorization": "Bearer " + auth}, json={"token": token}) demo_print("api", "response token submitted", 2)
def submit_challenge(auth, challenge): demo_print("api", "sending challenge to backend: Bearer " + auth, 2) demo_print("api", "challenge token: " + challenge, 2) response = requests.post("http://127.0.0.1:8080/approach1/users/challenge", headers={"Authorization": "Bearer " + auth}, json={"challenge": challenge}) token = response.json()["token"] demo_print("api", "response token generated: " + token, 2) return token
def register_beacon(auth, beacon_key): indent = 2 subject = "api" demo_print(subject, "registering beacon: Bearer " + auth, indent) demo_print(subject, "beacon key: " + beacon_key, indent) response = req.post("http://127.0.0.1:8080/approach2/register/{}/".format(beacon_key), headers={"Authorization": "Bearer " + auth}) if response.status_code == 200: demo_print(subject, "Successfully added beacon", 2) return
def add_interaction(auth, beacon_key, timestamp): indent = 2 subject = "api" demo_print(subject, "sending interaction to backend: Bearer " + auth, 2) response = req.post("http://127.0.0.1:8080/approach2/interact/{}/{}".format(beacon_key, timestamp), headers={"Authorization": "Bearer " + auth}) if response.status_code == 200: demo_print(subject, "successfully added interaction", indent) else: demo_print(ERROR_SUB, "API failed " + str(response.status_code), indent) return
def main(): subject = "Offline Approach" indent = 0 demo_print(subject, "starting demo", indent) [user1, user2] = register_two_users() demo_print(subject, "retrieving status before interaction") retrieve_and_print_user_status(user1, user2) demo_print(subject, "user b gets positively diagnosed") set__user_status_and_print("user b", user2) demo_print(subject, "creating beacon keys", indent) demo_print("beacon 1", "generating key") beacon1 = generate_beacon_key() demo_print("beacon 2", "generating key") beacon2 = generate_beacon_key() demo_print("user a", "registering beacon to user") register_beacon(user1, beacon1) demo_print("user b", "registering beacon to user") register_beacon(user2, beacon2) demo_print("beacon 1", "transmitting token") beacon1_token = simulate_beacon(beacon1, 1) send_over_air(beacon1_token) demo_print("user b", "beacon token received, sending to database") add_interaction(user2, beacon1_token, 1) demo_print(subject, "retrieving status after interaction") retrieve_and_print_user_status(user1, user2)
def generate_beacon_key(): demo_print("api", "retrieving factory unique generated identifier", 2) response = req.get("http://127.0.0.1:8080/beacon/generate") return response.json()["beacon_key"]
def main(): subject = "Online Approach" demo_print(subject, "starting demo") demo_print(subject, "registering users") [user_a, user_b] = register_two_users() demo_print(subject, "retrieving status before interaction") retrieve_and_print_user_status(user_a, user_b) demo_print(subject, "user b gets positively diagnosed") set__user_status_and_print("user b", user_b) demo_print(subject, "logging an interaction") demo_print("user a", "requesting challenge token") user_a_challenge = create_challenge(user_a) demo_print("user a", "sending challenge token to user b") user_b_received_challenge = send_over_air(user_a_challenge) demo_print("user b", "requesting response token with received challenge token") user_b_response = submit_challenge(user_b, user_b_received_challenge) demo_print("user b", "sending response token to user a") user_a_received_response = send_over_air(user_b_response) demo_print("user a", "logging interaction with received response token") submit_response(user_a, user_a_received_response) demo_print(subject, "retrieving status after interaction") retrieve_and_print_user_status(user_a, user_b)
def send_over_air(message): demo_print("air", "message travelling: " + message, 2) return message
def set__user_status_and_print(user_name, user_auth): demo_print("manual", "setting " + user_name + " status to positive") user_status_set(user_auth, "true") demo_print("user b", "confirming status") user_status(user_auth)
def retrieve_and_print_user_status(user_a, user_b): demo_print("user a", "requesting status") user_status(user_a) demo_print("user b", "requesting status") user_status(user_b)