44import string
55from base64 import b64decode
66from binascii import hexlify
7- import requests
87from xml .etree import ElementTree as etree
8+ import requests
9+
910from . import blob
1011from .exceptions import (
1112 NetworkError ,
@@ -59,7 +60,7 @@ def request_iteration_count(username, web_client=http):
5960
6061 try :
6162 count = int (response .content )
62- except :
63+ except Exception :
6364 raise InvalidResponseError ('Key iteration count is invalid' )
6465
6566 if count > 0 :
@@ -123,7 +124,6 @@ def oob_login(web_client, parsed_response, body, key_iteration_count, trust_id):
123124 'error' )
124125 if 'outofbandname' not in error .attrib or 'capabilities' not in error .attrib :
125126 return (None , parsed_response )
126- oob_name = error .attrib ['outofbandname' ]
127127 oob_capabilities = error .attrib ['capabilities' ].split (',' )
128128 can_do_passcode = 'passcode' in oob_capabilities
129129 if can_do_passcode and 'outofband' not in oob_capabilities :
@@ -173,11 +173,12 @@ def create_session(parsed_response, key_iteration_count, trust_id):
173173 token = ok_response .attrib .get ('token' )
174174 if isinstance (session_id , str ):
175175 return Session (session_id , key_iteration_count , token , trust_id )
176+ return None
176177
177178
178179def login_error (parsed_response ):
179180 error = None if parsed_response .tag != 'response' else parsed_response .find ('error' )
180- if error is None or len ( error .attrib ) == 0 :
181+ if error is None or not error .attrib :
181182 raise UnknownResponseSchemaError ()
182183
183184 exceptions = {
@@ -198,27 +199,25 @@ def login_error(parsed_response):
198199 return InvalidResponseError (message )
199200
200201
201- def decode_blob (blob ):
202- return b64decode (blob )
202+ def decode_blob (blob_ ):
203+ return b64decode (blob_ )
203204
204205
205206def make_key (username , password , key_iteration_count ):
206207 # type: (str, str, int) -> bytes
207208 if key_iteration_count == 1 :
208209 return hashlib .sha256 (username .encode ('utf-8' ) + password .encode ('utf-8' )).digest ()
209- else :
210- return hashlib .pbkdf2_hmac ('sha256' , password .encode ('utf-8' ), username .encode ('utf-8' ), key_iteration_count , 32 )
210+ return hashlib .pbkdf2_hmac ('sha256' , password .encode ('utf-8' ), username .encode ('utf-8' ), key_iteration_count , 32 )
211211
212212
213213def make_hash (username , password , key_iteration_count ):
214214 # type: (str, str, int) -> bytes
215215 if key_iteration_count == 1 :
216216 return bytearray (hashlib .sha256 (hexlify (make_key (username , password , 1 )) + password .encode ('utf-8' )).hexdigest (), 'ascii' )
217- else :
218- return hexlify (hashlib .pbkdf2_hmac (
219- 'sha256' ,
220- make_key (username , password , key_iteration_count ),
221- password .encode ('utf-8' ),
222- 1 ,
223- 32
224- ))
217+ return hexlify (hashlib .pbkdf2_hmac (
218+ 'sha256' ,
219+ make_key (username , password , key_iteration_count ),
220+ password .encode ('utf-8' ),
221+ 1 ,
222+ 32
223+ ))
0 commit comments