def test_valid_recovery(self): self.assertTrue( ecrecover( '{"custom":{"about":"about ","location":"location "},"timestamp":1483968938,"username":"******"}', data_decoder( '0xbd5c9009cc87c6d4ebb3ef8223fc036726bc311678890890619c787aa914d3b636aee82d885c6fb668233b5cc70ab09eea7051648f989e758ee09234f5340d9100' ), '0x5249dc212cd9c16f107c50b6c893952d617c011e'))
def test_valid_recovery_unicode(self): self.assertTrue( ecrecover( '{"custom":{"about":"æ","location":""},"timestamp":1483964545,"username":"******"}', data_decoder( '0xb3c61812e1e73f1a75cc9a2f5e748099378b7af2dd8bc3c1b4f0c067e6e9a4012d0c411b77bab63708b350742d41de574add6b06a3d06a5ae10fc9c63c18405301' ), '0x5249dc212cd9c16f107c50b6c893952d617c011e'))
def verify_request(self): """Verifies that the signature and the payload match the expected address raising a JSONHTTPError (400) if something is wrong with the request""" if TOSHI_ID_ADDRESS_HEADER in self.request.headers: expected_address = self.request.headers[ TOSHI_ID_ADDRESS_HEADER] elif self.get_argument(TOSHI_ID_ADDRESS_QUERY_ARG, None): expected_address = self.get_argument( TOSHI_ID_ADDRESS_QUERY_ARG) elif TOKEN_ID_ADDRESS_HEADER in self.request.headers: expected_address = self.request.headers[ TOKEN_ID_ADDRESS_HEADER] elif self.get_argument(TOKEN_ID_ADDRESS_QUERY_ARG, None): expected_address = self.get_argument( TOKEN_ID_ADDRESS_QUERY_ARG) else: raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'bad_arguments', 'message': 'Missing Toshi-ID-Address' }] }) if TOSHI_SIGNATURE_HEADER in self.request.headers: signature = self.request.headers[TOSHI_SIGNATURE_HEADER] elif self.get_argument(TOSHI_SIGNATURE_QUERY_ARG, None): signature = self.get_argument(TOSHI_SIGNATURE_QUERY_ARG) elif TOKEN_SIGNATURE_HEADER in self.request.headers: signature = self.request.headers[TOKEN_SIGNATURE_HEADER] elif self.get_argument(TOKEN_SIGNATURE_QUERY_ARG, None): signature = self.get_argument(TOKEN_SIGNATURE_QUERY_ARG) else: raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'bad_arguments', 'message': 'Missing Toshi-Signature' }] }) if TOSHI_TIMESTAMP_HEADER in self.request.headers: timestamp = self.request.headers[TOSHI_TIMESTAMP_HEADER] elif self.get_argument(TOSHI_TIMESTAMP_QUERY_ARG, None): timestamp = self.get_argument(TOSHI_TIMESTAMP_QUERY_ARG) elif TOKEN_TIMESTAMP_HEADER in self.request.headers: timestamp = self.request.headers[TOKEN_TIMESTAMP_HEADER] elif self.get_argument(TOKEN_TIMESTAMP_QUERY_ARG, None): timestamp = self.get_argument(TOKEN_TIMESTAMP_QUERY_ARG) else: raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'bad_arguments', 'message': 'Missing Toshi-Timestamp' }] }) timestamp = parse_int(timestamp) if timestamp is None: raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'invalid_timestamp', 'message': 'Given Toshi-Timestamp is invalid' }] }) if not validate_address(expected_address): raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'invalid_id_address', 'message': 'Invalid Toshi-ID-Address' }] }) if not validate_signature(signature): raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'invalid_signature', 'message': 'Invalid Toshi-Signature' }] }) try: signature = data_decoder(signature) except Exception: raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'invalid_signature', 'message': 'Invalid Toshi-Signature' }] }) verb = self.request.method uri = self.request.path if self.request.body: datahash = self.request.body else: datahash = "" data_string = generate_request_signature_data_string( verb, uri, timestamp, datahash) if not ecrecover(data_string, signature, expected_address): raise JSONHTTPError(400, body={ 'errors': [{ 'id': 'invalid_signature', 'message': 'Invalid Toshi-Signature' }] }) if abs(int(time.time()) - timestamp) > TIMESTAMP_EXPIRY: raise JSONHTTPError( 400, body={ 'errors': [{ 'id': 'invalid_timestamp', 'message': 'The difference between the timestamp and the current time is too large' }] }) return expected_address
def test_too_short_signature_comparison(self): self.assertFalse(ecrecover( '{"custom":{"about":"æ","location":""},"timestamp":1483964545,"username":"******"}', data_decoder('0x5301'), '0x5249dc212cd9c16f107c50b6c893952d617c011e' ))
def test_too_short_signature(self): self.assertEqual(ecrecover( '{"custom":{"about":"æ","location":""},"timestamp":1483964545,"username":"******"}', data_decoder('0x5301') ), None)