Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions simvaIntegration/authSIMVA.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'client_id': 'uadventure', 'response_type': 'code', 'scope': 'openid', 'nonce': '9EO2VPmUhmlwsmhr', 'redirect_uri': 'https://api.simva.e-ucm.es/activities/5f6214c0e195220016c87124/result', 'state': '1wltn5wWJ97Tvwb4', 'claims': {'id_token': {'username': True}, 'userinfo': {'name': None}}}\n",
"https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/auth?client_id=uadventure&response_type=code&scope=openid&nonce=9EO2VPmUhmlwsmhr&redirect_uri=https%3A%2F%2Fapi.simva.e-ucm.es%2Factivities%2F5f6214c0e195220016c87124%2Fresult&state=1wltn5wWJ97Tvwb4&claims=%7B%22id_token%22%3A+%7B%22username%22%3A+true%7D%2C+%22userinfo%22%3A+%7B%22name%22%3A+null%7D%7D&token\n"
]
},
{
"ename": "AttributeError",
"evalue": "'NoneType' object has no attribute 'find_element_by_xpath'",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mAttributeError\u001B[0m Traceback (most recent call last)",
"\u001B[1;32m<ipython-input-2-b654327a8673>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m()\u001B[0m\n\u001B[0;32m 49\u001B[0m \u001B[1;31m#fill form\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 50\u001B[0m \u001B[0mget_ipython\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mrun_line_magic\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'run'\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'browserFormFiler.ipynb'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 51\u001B[1;33m \u001B[0mlogin_url_form_fill\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mfill_form\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mlogin_url\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'pg4d'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 52\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 53\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mlogin_url_form_fill\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
"\u001B[1;32m<ipython-input-2-6c2488d7ff53>\u001B[0m in \u001B[0;36mfill_form\u001B[1;34m(url, username)\u001B[0m\n\u001B[0;32m 23\u001B[0m \u001B[0mtime\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0msleep\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;36m2\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 24\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 25\u001B[1;33m \u001B[0muser\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mweb\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mfind_element_by_xpath\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'//*[@id=\"username\"]'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 26\u001B[0m \u001B[0muser\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0msend_keys\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0musername\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 27\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
"\u001B[1;31mAttributeError\u001B[0m: 'NoneType' object has no attribute 'find_element_by_xpath'"
]
}
],
"source": [
"from oic.oic import Client\n",
"from oic.utils.authn.client import CLIENT_AUTHN_METHOD\n",
"from oic.oic.message import ProviderConfigurationResponse, RegistrationResponse, AuthorizationResponse, ClaimsRequest, Claims\n",
"from oic import rndstr\n",
"\n",
"client = Client(client_authn_method=CLIENT_AUTHN_METHOD)\n",
"\n",
"client_id = \"uadventure\"\n",
"client_secret = \"\"\n",
"info = {\"client_id\": client_id, \"client_secret\": client_secret}\n",
"\n",
"client_reg = RegistrationResponse(**info)\n",
"client.store_registration_info(client_reg)\n",
"issuer= 'https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/'\n",
"authorization_base_url = 'https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/auth'\n",
"token_url = 'https://sso.simva.e-ucm.es/auth/realms/simva/protocol/openid-connect/token'\n",
"op_info = ProviderConfigurationResponse(\n",
" version=\"1.0\", issuer=issuer,\n",
" authorization_endpoint=authorization_base_url,\n",
" token_endpoint=token_url)\n",
"\n",
"client.handle_provider_config(op_info, op_info['issuer'])\n",
"\n",
"redirect_uri='https://api.simva.e-ucm.es/activities/5f6214c0e195220016c87124/result'\n",
"\n",
"session={\"state\": rndstr(), \"nonce\": rndstr()}\n",
"\n",
"\n",
"claims_request=ClaimsRequest(\n",
" id_token=Claims(username=True),\n",
" userinfo=Claims(name=None))\n",
"\n",
"args_request = {\n",
" \"client_id\": client.client_id,\n",
" \"response_type\": \"code\",\n",
" \"scope\": [\"openid\"],\n",
" \"nonce\": session[\"nonce\"],\n",
" \"redirect_uri\": redirect_uri,\n",
" \"state\": session[\"state\"],\n",
" \"claims\":claims_request\n",
"}\n",
"\n",
"auth_req = client.construct_AuthorizationRequest(request_args=args_request)\n",
"login_url = auth_req.request(client.authorization_endpoint)+\"&token\"\n",
"\n",
"print(auth_req)\n",
"print(login_url)\n",
"\n",
"#fill form\n",
"%run browserFormFiler.ipynb\n",
"login_url_form_fill=fill_form(login_url, 'pg4d')\n",
"\n",
"print(login_url_form_fill)\n",
"print(client)\n",
"\n",
"#code given\n",
"aresp = client.parse_response(AuthorizationResponse, info=login_url_form_fill, sformat=\"urlencoded\")\n",
"code = aresp[\"code\"]\n",
"assert aresp[\"state\"] == session[\"state\"]\n",
"print(code)\n",
"\n",
"args = {\n",
" \"code\": code,\n",
" \"client_id\":client.client_id,\n",
" \"redirect_uri\":redirect_uri\n",
"}\n",
"resp = client.do_access_token_request(state=aresp[\"state\"],\n",
" request_args=args,\n",
" authn_method=\"client_secret_basic\")\n",
"print(resp)\n",
"\n",
"userinfo = client.do_user_info_request(state=aresp[\"state\"])\n",
"print(userinfo)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import requests\n",
"\n",
"urlpost=\"https://api.simva.e-ucm.es/activities/5f6214c0e195220016c87124/result?state=BzjCQg96TbLaYUEs&session_state=90894e2e-eff9-4bd5-8c4e-585494822fa3&code=5a4a5b0a-1d7c-4662-9eeb-4ed60b498df0.90894e2e-eff9-4bd5-8c4e-585494822fa3.edfc1ac7-f2cf-4789-80b3-4ad4c1e75290\"\n",
"def post_activity_xapi_data(dataxapilist):\n",
" for dataxapi in dataxapilist:\n",
" response=requests.post(urlpost, data=dataxapi)#redirect.message\n",
" print(response.status_code)\n",
" print(response.content)\n",
"\n",
"data=[\n",
" {\n",
" \"actor\": {\n",
" \"id\": \"63ba693f-23f5-41ef-8660-bdc00adc1e5f\",\n",
" \"name\": \"63ba693f-23f5-41ef-8660-bdc00adc1e5f\"\n",
" },\n",
" \"verb\": {\n",
" \"id\": \"https://w3id.org/xapi/seriousgames/verbs/interacted\"\n",
" },\n",
" \"object\": {\n",
" \"id\": \"13651f64-0fb7-4e46-aacd-4069c94c0dcd\",\n",
" \"definition\": {\n",
" \"type\": \"https://w3id.org/xapi/seriousgames/activity-types/enemy\"\n",
" }\n",
" },\n",
" \"timestamp\": \"2020-09-16T16:42:23.095536Z\"\n",
" },\n",
" {\n",
" \"actor\": {\n",
" \"id\": \"488de066-18b0-4e10-bd29-a38b2a82ea33\",\n",
" \"name\": \"488de066-18b0-4e10-bd29-a38b2a82ea33\"\n",
" },\n",
" \"verb\": {\n",
" \"id\": \"https://w3id.org/xapi/seriousgames/verbs/selected\"\n",
" },\n",
" \"object\": {\n",
" \"id\": \"47290d8e-5f82-4e55-be9d-db0ce51734b9\",\n",
" \"definition\": {\n",
" \"type\": \"https://w3id.org/xapi/seriousgames/activity-types/question\"\n",
" }\n",
" },\n",
" \"timestamp\": \"2020-09-16T16:42:42.844953Z\"\n",
" }\n",
"]\n",
"post_activity_xapi_data(data)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
84 changes: 84 additions & 0 deletions simvaIntegration/browserFormFiler.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"outputs": [],
"source": [
"import os\n",
"from pyvirtualdisplay import Display\n",
"from selenium import webdriver\n",
"import time\n",
"\n",
"#https://pypi.org/project/selenium/\n",
"def get_browser(url):\n",
" try:\n",
" #https://sites.google.com/a/chromium.org/chromedriver/downloads\n",
" web = webdriver.Chrome(os.path.join(os.getcwd(),\"chromedriver_win32\\mschromedriver.exe\"))\n",
" web.get(url)\n",
" return web\n",
" except (ValueError,Exception):\n",
" try:\n",
" #https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/\n",
" web=webdriver.Edge(os.path.join(os.getcwd(),\"edgedriver_win64\\msedgedriver.exe\"))\n",
" web.get(url)\n",
" return web\n",
" except (ValueError,Exception):\n",
" try:\n",
" #https://github.com/mozilla/geckodriver/releases/tag/v0.27.0\n",
" web=webdriver.Firefox(os.path.join(os.getcwd(),\"firefoxdriver_win64\\geckodriver.exe\"))\n",
" web.get(url)\n",
" return web\n",
" except (ValueError,Exception):\n",
" #https://webkit.org/blog/6900/webdriver-support-in-safari-10/\n",
" web=webdriver.Safari()\n",
" web.get(url)\n",
" return web\n",
"\n",
"def fill_form(url, username):\n",
" #display = Display(visible=0, size=(800, 600))\n",
" #display.start()\n",
" web=get_browser(url)\n",
" time.sleep(2)\n",
"\n",
" user = web.find_element_by_xpath('//*[@id=\"username\"]')\n",
" user.send_keys(username)\n",
"\n",
" submit = web.find_element_by_xpath('//*[@id=\"kc-login\"]')\n",
" submit.click()\n",
" url=web.current_url\n",
" print(url)\n",
" web.quit()\n",
" #display.close()\n",
" return url"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"name": "pycharm-831db6a3",
"language": "python",
"display_name": "PyCharm (xapi-sg-data-generator)"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Binary file not shown.
Loading