class SignatureTestCase(SimpleTestCase): def setUp(self): self.auth = SignatureAuthentication() def test_no_headers_in_signature_is_date(self): signature = build_signature([]) headers = self.auth.get_headers_from_signature(signature) self.assertEqual(1, len(headers)) self.assertEqual("date", headers[0]) def test_date_in_signature(self): signature = build_signature(['date']) headers = self.auth.get_headers_from_signature(signature) self.assertTrue('date' in headers) def test_many_in_signature(self): signature = build_signature(['date', 'accept', '(request-target)']) headers = self.auth.get_headers_from_signature(signature) self.assertTrue('date' in headers) self.assertTrue('accept' in headers) self.assertTrue('(request-target)' in headers) def test_get_signature(self): signature_string = build_signature(['(request-target)', 'date']) signature = self.auth.get_signature_from_signature_string( signature_string) self.assertEqual(SIGNATURE, signature) def test_get_signature_without_headers(self): signature_string = build_signature([]) signature = self.auth.get_signature_from_signature_string( signature_string) self.assertEqual(SIGNATURE, signature)
class SignatureTestCase(SimpleTestCase): def setUp(self): self.auth = SignatureAuthentication() def test_no_headers_in_signature_is_date(self): signature = build_signature([]) headers = self.auth.get_headers_from_signature(signature) self.assertEqual(1, len(headers)) self.assertEqual("date", headers[0]) def test_date_in_signature(self): signature = build_signature(['date']) headers = self.auth.get_headers_from_signature(signature) self.assertTrue('date' in headers) def test_many_in_signature(self): signature = build_signature(['date', 'accept', '(request-target)']) headers = self.auth.get_headers_from_signature(signature) self.assertTrue('date' in headers) self.assertTrue('accept' in headers) self.assertTrue('(request-target)' in headers) def test_get_signature(self): signature_string = build_signature(['(request-target)', 'date']) signature = self.auth.get_signature_from_signature_string( signature_string) self.assertEqual(SIGNATURE, signature) def test_get_signature_without_headers(self): signature_string = build_signature([]) signature = self.auth.get_signature_from_signature_string( signature_string) self.assertEqual(SIGNATURE, signature)
class SignatureTestCase(SimpleTestCase): def setUp(self): self.auth = SignatureAuthentication() def test_no_headers_in_signature(self): signature = build_signature([]) self.assertRaises(AuthenticationFailed, self.auth.get_headers_from_signature, signature) def test_date_in_signature(self): signature = build_signature(['date']) headers = self.auth.get_headers_from_signature(signature) self.assertTrue('date' in headers) def test_many_in_signature(self): signature = build_signature(['date', 'accept', 'request-line']) headers = self.auth.get_headers_from_signature(signature) self.assertTrue('date' in headers) self.assertTrue('accept' in headers) self.assertTrue('request-line' in headers) def test_get_signature(self): signature_string = build_signature(['request-line', 'date']) signature = self.auth.get_signature_from_signature_string( signature_string) self.assertEqual(SIGNATURE, signature) def test_get_signature_without_headers(self): signature_string = build_signature([]) signature = self.auth.get_signature_from_signature_string( signature_string) self.assertEqual(SIGNATURE, signature)
class BuildSignatureTestCase(SimpleTestCase): request = RequestFactory() KEYID = 'su-key' def setUp(self): self.auth = SignatureAuthentication() def test_build_signature(self): # TO SIGN: # # GET /packages/measures/ HTTP/1.1 # host: localhost:8000 # accept: application/json # date: Mon, 17 Feb 2014 06:11:05 GMT headers = ['(request-target)', 'host', 'accept', 'date'] expected_signature = '+dV3yojX7N5I5J+rx0N+7kL5zES2L9Goo4ApJIn33IM=' expected_signature_string = build_signature( headers, key_id=self.KEYID, signature=expected_signature) req = RequestFactory().get( '/packages/measures/', {}, HTTP_HOST='localhost:8000', HTTP_ACCEPT='application/json', HTTP_DATE='Mon, 17 Feb 2014 06:11:05 GMT', HTTP_AUTHORIZATION=expected_signature_string) signature_string = self.auth.build_signature( self.KEYID, SECRET, req) signature = re.match( '.*signature="(.+)",?.*', signature_string).group(1) self.assertEqual(expected_signature, signature)
def authenticate(self, request): api_key_header = self.header_canonical(self.API_KEY_HEADER) api_key = request.META.get(api_key_header) if not api_key: return None nonce = request.META.get(self.header_canonical("nonce")) if not nonce: raise exceptions.AuthenticationFailed('No nonce provided') ts = request.META.get(self.header_canonical("timestamp")) if not ts: raise exceptions.AuthenticationFailed('No timestamp provided') ts_diff = int(time.time()) - int(ts) if abs(ts_diff) > self.TIME_BACK: raise exceptions.AuthenticationFailed( 'Timestamp is more than %s minutes different from the server.' % TIME_BACK) try: nonce = Nonce(nonce=nonce, timestamp=ts) nonce.save(force_insert=True) except: raise exceptions.AuthenticationFailed('Nonce check failed') return SignatureAuthentication.authenticate(self, request)
def authenticate(self, request): api_key_header = self.header_canonical(self.API_KEY_HEADER) api_key = request.META.get(api_key_header) if not api_key: return None nonce = request.META.get(self.header_canonical("nonce")) if not nonce: raise exceptions.AuthenticationFailed('No nonce provided') ts = request.META.get(self.header_canonical("timestamp")) if not ts: raise exceptions.AuthenticationFailed('No timestamp provided') ts_diff = int(time.time()) - int(ts) if abs(ts_diff) > self.TIME_BACK: raise exceptions.AuthenticationFailed( 'Timestamp is more than %s minutes different from the server.' % TIME_BACK) try: nonce = Nonce(nonce=nonce, timestamp=ts) nonce.save(force_insert=True) except: raise exceptions.AuthenticationFailed('Nonce check failed') return SignatureAuthentication.authenticate(self, request)
class BuildSignatureTestCase(SimpleTestCase): request = RequestFactory() KEYID = 'su-key' def setUp(self): self.auth = SignatureAuthentication() def test_build_signature(self): # TO SIGN: # # GET /packages/measures/ HTTP/1.1 # host: localhost:8000 # accept: application/json # date: Mon, 17 Feb 2014 06:11:05 GMT headers = ['(request-target)', 'host', 'accept', 'date'] expected_signature = '+dV3yojX7N5I5J+rx0N+7kL5zES2L9Goo4ApJIn33IM=' expected_signature_string = build_signature( headers, key_id=self.KEYID, signature=expected_signature) req = RequestFactory().get( '/packages/measures/', {}, HTTP_HOST='localhost:8000', HTTP_ACCEPT='application/json', HTTP_DATE='Mon, 17 Feb 2014 06:11:05 GMT', HTTP_AUTHORIZATION=expected_signature_string) signature_string = self.auth.build_signature(self.KEYID, SECRET, req) signature = re.match('.*signature="(.+?)"', signature_string).group(1) self.assertEqual(expected_signature, signature)
class BuildSignatureTestCase(SimpleTestCase): request = RequestFactory() KEYID = 'su-key' def setUp(self): self.auth = SignatureAuthentication() def test_build_signature(self): # TO SIGN: # # GET /packages/measures/ HTTP/1.1 # accept: application/json # date: Mon, 17 Feb 2014 06:11:05 GMT # host: localhost:8000 headers = ['request-line', 'accept', 'date', 'host'] expected_signature = 'DvQs08T31vR83r5tUqonb6EcpHb+BtDPEbCZ1/WVH58=' expected_signature_string = build_signature( headers, key_id=self.KEYID, signature=expected_signature) req = RequestFactory().get( '/packages/measures/', {}, HTTP_HOST='localhost:8000', HTTP_DATE='Mon, 17 Feb 2014 06:11:05 GMT', HTTP_ACCEPT='application/json', HTTP_AUTHORIZATION=expected_signature_string) signature_string = self.auth.build_signature( self.KEYID, SECRET, req) signature = re.match( '.*signature="(.+)",?.*', signature_string).group(1) self.assertEqual(expected_signature, signature)
class HeadersUnitTestCase(SimpleTestCase): request = RequestFactory() def setUp(self): self.auth = SignatureAuthentication() def test_special_header_names(self): for special in ['Content-Type', 'CONTENT-TYPE', 'content-type']: canon = self.auth.header_canonical(special) self.assertEqual('CONTENT-TYPE', canon) for special in ['Content-Length', 'CONTENT-LENGTH', 'content-length']: canon = self.auth.header_canonical(special) self.assertEqual('CONTENT-LENGTH', canon) def test_header_names(self): headers = ['X-Api-Key', 'Authentication', 'date', 'X-Something-Else'] for header in headers: canon = self.auth.header_canonical(header) expected = 'HTTP_%s' % header.upper().replace('-', '_') self.assertEqual(expected, canon) def test_build_signature_for_date(self): req = self.request.get(ENDPOINT, {}, HTTP_X_DATE="some date") dict_to_sign = self.auth.build_dict_to_sign(req, ['date']) self.assertTrue('date' in dict_to_sign.keys()) def test_build_signature_for_date_and_other(self): req = self.request.get(ENDPOINT, {}, HTTP_X_ACCEPT="*/*", HTTP_X_DATE="some date") dict_to_sign = self.auth.build_dict_to_sign(req, ['accept', 'date']) self.assertTrue('date' in dict_to_sign.keys()) self.assertTrue('accept' in dict_to_sign.keys()) def test_build_signature_for_request_line(self): req = self.request.get(ENDPOINT, {}, HTTP_X_DATE="some date") dict_to_sign = self.auth.build_dict_to_sign( req, ['(request-target)', 'date']) self.assertTrue('date' in dict_to_sign.keys()) self.assertTrue('(request-target)' not in dict_to_sign.keys())
class HeadersUnitTestCase(SimpleTestCase): request = RequestFactory() def setUp(self): self.auth = SignatureAuthentication() def test_special_header_names(self): for special in ['Content-Type', 'CONTENT-TYPE', 'content-type']: canon = self.auth.header_canonical(special) self.assertEqual('CONTENT-TYPE', canon) for special in ['Content-Length', 'CONTENT-LENGTH', 'content-length']: canon = self.auth.header_canonical(special) self.assertEqual('CONTENT-LENGTH', canon) def test_header_names(self): headers = ['X-Api-Key', 'Authentication', 'date', 'X-Something-Else'] for header in headers: canon = self.auth.header_canonical(header) expected = 'HTTP_%s' % header.upper().replace('-', '_') self.assertEqual(expected, canon) def test_build_signature_for_date(self): req = self.request.get(ENDPOINT, {}, HTTP_X_DATE="some date") dict_to_sign = self.auth.build_dict_to_sign(req, ['date']) self.assertTrue('date' in dict_to_sign.keys()) def test_build_signature_for_date_and_other(self): req = self.request.get(ENDPOINT, {}, HTTP_X_ACCEPT="*/*", HTTP_X_DATE="some date") dict_to_sign = self.auth.build_dict_to_sign(req, ['accept', 'date']) self.assertTrue('date' in dict_to_sign.keys()) self.assertTrue('accept' in dict_to_sign.keys()) def test_build_signature_for_request_line(self): req = self.request.get(ENDPOINT, {}, HTTP_X_DATE="some date") dict_to_sign = self.auth.build_dict_to_sign( req, ['(request-target)', 'date']) self.assertTrue('date' in dict_to_sign.keys()) self.assertTrue('(request-target)' not in dict_to_sign.keys())
def setUp(self): self.auth = SignatureAuthentication()
def setUp(self): self.auth = SignatureAuthentication()