<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from binascii import hexlify <EOL> import hashlib <EOL> import os <EOL> from time import time <EOL> try : <EOL> from urllib . parse import parse_qs , urlencode , urlparse <EOL> except ImportError : <EOL> from urllib import urlencode <EOL> from urlparse import parse_qs , urlparse <EOL> import requests <EOL> from aspen import Response <EOL> from liberapay . elsewhere . _base import Platform <EOL> from liberapay . elsewhere . _extractors import key , not_available <EOL> class Bountysource ( Platform ) : <EOL> name = '<STR_LIT>' <EOL> display_name = '<STR_LIT>' <EOL> account_url = '<STR_LIT>' <EOL> optional_user_name = True <EOL> api_format = '<STR_LIT>' <EOL> api_user_info_path = '<STR_LIT>' <EOL> api_user_self_info_path = '<STR_LIT>' <EOL> x_user_id = key ( '<STR_LIT:id>' ) <EOL> x_user_name = not_available <EOL> x_display_name = key ( '<STR_LIT>' ) <EOL> x_email = key ( '<STR_LIT:email>' ) <EOL> x_avatar_url = key ( '<STR_LIT>' ) <EOL> def get_auth_session ( self , token = None ) : <EOL> sess = requests . Session ( ) <EOL> sess . auth = BountysourceAuth ( token ) <EOL> return sess <EOL> def get_auth_url ( self , user ) : <EOL> query_id = hexlify ( os . urandom ( <NUM_LIT:10> ) ) <EOL> time_now = int ( time ( ) ) <EOL> raw = '<STR_LIT>' % ( user . id , time_now , self . api_secret ) <EOL> h = hashlib . md5 ( raw ) . hexdigest ( ) <EOL> token = '<STR_LIT>' % ( user . id , time_now , h ) <EOL> params = dict ( <EOL> redirect_url = self . callback_url + '<STR_LIT>' + query_id , <EOL> external_access_token = token <EOL> ) <EOL> url = self . auth_url + '<STR_LIT>' + urlencode ( params ) <EOL> return url , query_id , '<STR_LIT>' <EOL> def get_query_id ( self , querystring ) : <EOL> token = querystring [ '<STR_LIT>' ] <EOL> i = token . rfind ( '<STR_LIT:.>' ) <EOL> data , data_hash = token [ : i ] , token [ i + <NUM_LIT:1> : ] <EOL> if data_hash != hashlib . md5 ( data + '<STR_LIT:.>' + self . api_secret ) . hexdigest ( ) : <EOL> raise Response ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return querystring [ '<STR_LIT>' ] <EOL> def get_user_self_info ( self , sess ) : <EOL> querystring = urlparse ( sess . _callback_url ) . query <EOL> info = { k : v [ <NUM_LIT:0> ] if len ( v ) == <NUM_LIT:1> else v <EOL> for k , v in parse_qs ( querystring ) . items ( ) } <EOL> info . pop ( '<STR_LIT>' ) <EOL> info . pop ( '<STR_LIT>' ) <EOL> return self . extract_user_info ( info ) <EOL> def handle_auth_callback ( self , url , query_id , unused_arg ) : <EOL> sess = self . get_auth_session ( token = query_id ) <EOL> sess . _callback_url = url <EOL> return sess <EOL> class BountysourceAuth ( object ) : <EOL> def __init__ ( self , token = None ) : <EOL> self . token = token <EOL> def __call__ ( self , req ) : <EOL> if self . token : <EOL> req . params [ '<STR_LIT>' ] = self . token </s>
