<s> """<STR_LIT>""" <EOL> try : <EOL> from urllib . request import ( build_opener , HTTPCookieProcessor ) <EOL> from urllib . parse import ( urlencode , parse_qs ) <EOL> from http import server <EOL> except ImportError : <EOL> from urllib import urlencode <EOL> from urlparse import parse_qs <EOL> from urllib2 import ( build_opener , HTTPCookieProcessor ) <EOL> import BaseHTTPServer as server <EOL> import webbrowser <EOL> import json <EOL> class Authentication ( object ) : <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . token = self . root_collection [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> cookie_handler = HTTPCookieProcessor ( ) <EOL> self . cookies = cookie_handler . cookiejar <EOL> self . opener = build_opener ( cookie_handler ) <EOL> def login ( self , username , password ) : <EOL> """<STR_LIT>""" <EOL> self . logged_in = False <EOL> self . cookies . clear ( ) <EOL> url = self . token <EOL> credentials = urlencode ( { '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT>' : self . key , <EOL> '<STR_LIT>' : '<STR_LIT:password>' <EOL> } ) <EOL> credentials = credentials . encode ( "<STR_LIT:utf-8>" ) <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:application/json>" } <EOL> response = self . post ( url , credentials , headers ) <EOL> self . access_token = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . logged_in = True <EOL> self . fix_discovery ( ) <EOL> def oauth_desktop_login ( self , ruri = None ) : <EOL> """<STR_LIT>""" <EOL> if ruri is None : <EOL> ruri = "<STR_LIT>" <EOL> self . logged_in = False <EOL> self . cookies . clear ( ) <EOL> url = self . root_collection [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> url = self . _add_query_params ( url , { '<STR_LIT>' : '<STR_LIT:code>' , <EOL> '<STR_LIT>' : self . key , <EOL> '<STR_LIT>' : ruri <EOL> } ) <EOL> webbrowser . open ( url ) <EOL> server . HTTPServer ( ( '<STR_LIT>' , <NUM_LIT> ) , Getter ) . handle_request ( ) <EOL> self . oauth_code_login ( qs ) <EOL> def oauth_code_login ( self , code ) : <EOL> """<STR_LIT>""" <EOL> url = self . token <EOL> credentials = urlencode ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : code , <EOL> '<STR_LIT>' : self . key <EOL> } ) <EOL> credentials = credentials . encode ( "<STR_LIT:utf-8>" ) <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:application/json>" } <EOL> response = self . post ( url , credentials , headers , nojson = True ) <EOL> response = json . loads ( ) <EOL> self . access_token = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . logged_in = True <EOL> self . fix_discovery ( ) <EOL> def unauthenticated_login ( self , ip_address ) : <EOL> """<STR_LIT>""" <EOL> self . logged_in = False <EOL> self . cookies . clear ( ) <EOL> url = self . token <EOL> credentials = urlencode ( { '<STR_LIT>' : ip_address , <EOL> '<STR_LIT>' : self . key , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:application/json>" } <EOL> credentials = credentials . encode ( "<STR_LIT:utf-8>" ) <EOL> response = self . post ( url , credentials , headers ) <EOL> self . access_token = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . logged_in = True <EOL> self . fix_discovery ( ) <EOL> def logout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logged_in = False <EOL> url = self . token + "<STR_LIT>" + self . access_token <EOL> self . delete ( url ) <EOL> self . access_token = None <EOL> self . cookies . clear ( ) <EOL> self . fix_discovery ( ) <EOL> class Getter ( server . BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def do_GET ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send_response ( code = <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . end_headers ( ) <EOL> path = self . path <EOL> global qs <EOL> qs = parse_qs ( path ) <EOL> qs = list ( qs . values ( ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> sendme = "<STR_LIT>" <EOL> sendme += "<STR_LIT>" <EOL> sendme += "<STR_LIT>" <EOL> sendme = sendme . encode ( "<STR_LIT>" ) <EOL> self . wfile . write ( sendme ) </s>
