Ejemplo n.º 1
0
 def __init__(self):
     self.authentication = Authentication(database)
     self.request        = request
     self.render         = render_template
     self.abort          = abort
     self.request        = request
     self.redirect       = redirect 
     self.url_for        = url_for 
Ejemplo n.º 2
0
class Controller(object):
    
    def __init__(self):
        self.authentication = Authentication(database)
        self.request        = request
        self.render         = render_template
        self.abort          = abort
        self.request        = request
        self.redirect       = redirect 
        self.url_for        = url_for 

    @staticmethod
    def authentication_required(view_function):
        def decorator(self, *args, **kwargs):
            if not self.user_is_authenticated():
                return self.prompt_for_password()
            return view_function(self, *args, **kwargs)
        return decorator

    def user_is_authenticated(self):
        request_header = request.headers.get('Authorization')
        if not self.authentication.is_valid_authentication_format(request_header):
            return False
        
        encoded_credentials = self.authentication.extract_encoded_credentials(request_header)
        decoded_credentials = self.authentication.decode_credentials(encoded_credentials)
        credentials = self.authentication.split_credentials(decoded_credentials) 
    
        return self.authentication.verify_credentials(credentials['username'], credentials['password'])

    def prompt_for_password(self):
        response = make_response(self.render('admin/unauthenticated.html'), 401)
        response_header = self.authentication.authenticate_response_header()
        response.headers['WWW-Authenticate'] = response_header
        return response
Ejemplo n.º 3
0
 def test_authentication_controller_raises_exception_if_user_does_not_exist(self, mock_user_repository):
     with self.assertRaises(AuthenticationException):
         authentication_controller = Authentication()
         mock_user_repository.return_value = None
         authentication_controller.look_up_user('*****@*****.**')
Ejemplo n.º 4
0
 def test_authentication_controller_delegates_to_user_repository(self, mock_user_repository):
     authentication_controller = Authentication()
     authentication_controller.look_up_user('*****@*****.**')
     mock_user_repository.assert_called_with('*****@*****.**')
Ejemplo n.º 5
0
 def test_authentication_controller_can_return_a_header_to_the_client(self):
     authentication_controller = Authentication()
     self.assertEqual(authentication_controller.authenticate_response_header(), 'Basic realm="Authentication Required"')
Ejemplo n.º 6
0
 def test_authentication_controller_can_extract_username_and_password(self):
     authentication_controller = Authentication()
     self.assertEqual(authentication_controller.split_credentials('bob:secret123'), { 'username': '******', 'password': '******' })
Ejemplo n.º 7
0
 def test_authentication_controller_returns_true_if_authorization_value_has_one_colon(self):
     authentication_controller = Authentication()
     header_value = 'Basic dmFsaWQ6Y3JlZGVudGlhbA==' # valid:credential
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), True)
Ejemplo n.º 8
0
 def test_authentication_controller_can_decode_base64_string(self):
     authentication_controller = Authentication()
     encoded = 'dXNlcm5hbWU6cGFzc3dvcmQ='
     decoded = authentication_controller.decode_credentials(encoded)
     self.assertEqual(decoded, "username:password")
Ejemplo n.º 9
0
 def test_authentication_controller_returns_false_if_authorization_value_has_multiple_colons(self):
     authentication_controller = Authentication()
     header_value = 'Basic dG9vLW1hbnk6OmNvbG9ucw==' # too-many::colons
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), False)
Ejemplo n.º 10
0
 def test_authentication_controller_returns_false_if_authorization_value_is_missing_colon(self):
     authentication_controller = Authentication()
     header_value = 'Basic bWlzc2luZztjb2xvbg==' # missing;colon
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), False)
Ejemplo n.º 11
0
 def test_authentication_controller_returns_false_if_authorization_value_is_none(self):
     authentication_controller = Authentication()
     header_value = None
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), False)
Ejemplo n.º 12
0
 def test_authentication_controller_returns_false_if_authorization_value_contains_spaces_in_encoded_credentials(self):
     authentication_controller = Authentication()
     header_value = 'Basic dmFsaWQ6Y3 JlZGVudGlhbA==' # valid:credential (with space)
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), False)
Ejemplo n.º 13
0
 def test_authentication_controller_returns_false_if_authorization_value_is_not_prefixed_with_basic(self):
     authentication_controller = Authentication()
     header_value = 'Advanced dmFsaWQ6Y3JlZGVudGlhbA==' # valid:credential 
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), False)
Ejemplo n.º 14
0
 def test_authentication_controller_can_decode_scandinavian_encoded_base64_string(self):
     authentication_controller = Authentication()
     encoded = 'w6XDpcOlw6XDpTrDpsOmw7jDpcOlw6U='
     decoded = authentication_controller.decode_credentials(encoded)
     self.assertEqual(decoded, "ååååå:ææøååå")
Ejemplo n.º 15
0
 def test_authentication_controller_can_extract_encoded_credentials(self):
     authentication_controller = Authentication()
     header_value = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='
     encoded_credentials = authentication_controller.extract_encoded_credentials(header_value)
     self.assertEqual(encoded_credentials, "dXNlcm5hbWU6cGFzc3dvcmQ=")
Ejemplo n.º 16
0
 def test_authentication_controller_returns_user_from_lookup(self, mock_user_repository):
     authentication_controller = Authentication()
     mock_user = MagicMock()
     mock_user_repository.return_value = mock_user 
     self.assertEqual(authentication_controller.look_up_user('*****@*****.**'), mock_user)
Ejemplo n.º 17
0
 def test_authentication_controller_returns_false_if_authorization_value_is_missing_username(self):
     authentication_controller = Authentication()
     header_value = 'Basic OnBhc3N3b3Jk' # :password
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), False)
Ejemplo n.º 18
0
 def test_authentication_controller_returns_false_if_authorization_value_is_missing_password(self):
     authentication_controller = Authentication()
     header_value = 'Basic dXNlcm5hbWU6' # username:
     self.assertEqual(authentication_controller.is_valid_authentication_format(header_value), False)
Ejemplo n.º 19
0
 def test_authentication_controller_can_verify_credentials_of_user(self, mock_look_up_user):
     authentication_controller = Authentication()
     mock_user = MagicMock()
     mock_user.checkCredentials.return_value = True
     mock_look_up_user.return_value = mock_user 
     self.assertEqual(authentication_controller.verify_credentials('*****@*****.**', 'password123'), True)