-
-
Save shreddd/45f76640c556fc1dbb22 to your computer and use it in GitHub Desktop.
NEWT Auth iPython Notebook
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
{ | |
"metadata": { | |
"name": "Untitled0" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "# NEWT AUTH" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "from requests import Session\ns = Session()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "r = s.post(\"https://newt.nersc.gov/newt/auth\", {\"username\": \"myuser\", \"password\": \"XXXXXXXX\"})", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "The `newt_sessionid` object is actually returned with the auth request so you don't need to dig around the cookies.\n" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "r.json()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": "{u'auth': True,\n u'newt_sessionid': u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',\n u'session_lifetime': 43199,\n u'username': u'myuser'}" | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "Now you can just extract this newt_session_id and pass it on with a future request" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "newt_sessionid = r.json()['newt_sessionid']\n", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "A new session can now be initiated as long as it has the newt_sessionid string. This ``newt_sessionid`` is a short-lived token and can be passed to other clients or servers that need access. " | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "s2 = Session()\nr = s2.get(\"https://newt.nersc.gov/newt/auth\", cookies={'newt_sessionid': newt_sessionid})", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "r.json()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": "{u'auth': True,\n u'newt_sessionid': u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',\n u'session_lifetime': 39698,\n u'username': u'myuser'}" | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment