Created
March 28, 2024 22:40
-
-
Save monoxgas/3dc29a873384a7420e4b5ec7f134139a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Test Challenge on Crucible" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Objective\n", | |
"\n", | |
"The following code blocks build ipywidgets, which allow for light weight UI functionality, so you can copy and paste your Crucible API key into the input field and then submit a request to the API endpoint to test for a valid key." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Import dependencies block\n", | |
"import ipywidgets as widgets\n", | |
"from IPython.display import display\n", | |
"import requests" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Creates an ipywidget input field for API key\n", | |
"api_key_widget = widgets.Password(\n", | |
" value='',\n", | |
" placeholder='Enter API Key',\n", | |
" description='API Key:',\n", | |
" disabled=False\n", | |
")\n", | |
"\n", | |
"# Creates an ipywidget button that will trigger the fetch_data function\n", | |
"fetch_button = widgets.Button(\n", | |
" description='Fetch Data',\n", | |
" disabled=False,\n", | |
" button_style='',\n", | |
" tooltip='Click to fetch data using the API key'\n", | |
")\n", | |
"\n", | |
"# Function to fetch data from the API with a POST request\n", | |
"def fetch_data(b):\n", | |
" # API endpoint\n", | |
" url = \"https://test.dev-crucible.dreadnode.io/score\"\n", | |
" \n", | |
" # Headers with API key\n", | |
" headers = {\n", | |
" \"Authorization\": api_key_widget.value\n", | |
" }\n", | |
"\n", | |
" # Data to be sent with the POST request\n", | |
" data = {\"data\":\"flag please?\"}\n", | |
"\n", | |
" # Perform the POST request\n", | |
" response = requests.post(url, headers=headers, json=data)\n", | |
" \n", | |
" if response.status_code == 200:\n", | |
" # Process the response if request is successful\n", | |
" print(\"Request successful!\")\n", | |
" print(response.json()) # Assuming the response is JSON-formatted\n", | |
" else:\n", | |
" # Handle errors\n", | |
" print(\"Failed to fetch data:\", response.status_code)\n", | |
"\n", | |
"# Display the widgets\n", | |
"display(api_key_widget, fetch_button)\n", | |
"\n", | |
"# Call fetch_data function when the button is clicked\n", | |
"fetch_button.on_click(fetch_data)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The following is a simplified code block, from our example code in the challenge, that does not require user interaction. Be sure to insert __YOUR__ API key in the API_KEY_HERE string." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Example of how to query the API, from the Crucible Test challenge documentation\n", | |
"\n", | |
"def query(input_data):\n", | |
" response = requests.post(\n", | |
" \"https://test.dev-crucible.dreadnode.io/score\",\n", | |
" headers={\"Authorization\": \"API_KEY_HERE\"},\n", | |
" json={\"data\": input_data}\n", | |
" )\n", | |
" return response.json()\n", | |
"\n", | |
"query(\"flag please?\")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".venv", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment