Exemple #1
0
 def client(self):
     """
     :rtype: keycloak.client.KeycloakClient
     """
     if self._client is None:
         self._client = KeycloakClient(server_url=self._server_url,
                                       headers=self._headers)
     return self._client
Exemple #2
0
class KeycloakRealm(object):

    _server_url = None
    _realm_name = None

    _headers = None
    _client = None

    def __init__(self, server_url, realm_name, headers=None):
        """
        :param str server_url: The base URL where the Keycloak server can be
            found
        :param str realm_name: REALM name
        :param dict headers: Optional extra headers to send with requests to
            the server
        """
        self._server_url = server_url
        self._realm_name = realm_name
        self._headers = headers

    @property
    def client(self):
        """
        :rtype: keycloak.client.KeycloakClient
        """
        if self._client is None:
            self._client = KeycloakClient(server_url=self._server_url,
                                          headers=self._headers)
        return self._client

    @property
    def realm_name(self):
        return self._realm_name

    @property
    def server_url(self):
        return self._server_url

    @property
    def admin(self):
        return KeycloakAdmin(realm=self)

    def open_id_connect(self, client_id, client_secret):
        """
        Get OpenID Connect client

        :param str client_id:
        :param str client_secret:
        :rtype: keycloak.openid_connect.KeycloakOpenidConnect
        """
        return KeycloakOpenidConnect(realm=self,
                                     client_id=client_id,
                                     client_secret=client_secret)

    def authz(self, client_id):
        """
        Get Authz client

        :param str client_id:
        :rtype: keycloak.authz.KeycloakAuthz
        """
        return KeycloakAuthz(realm=self, client_id=client_id)

    def uma(self):
        """
        Get UMA client

        This method is here for backwards compatibility

        :return: keycloak.uma.KeycloakUMA
        """
        return self.uma2

    @property
    def uma2(self):
        """
        Starting from Keycloak 4 UMA2 is supported
        :rtype: keycloak.uma.KeycloakUMA
        """
        return KeycloakUMA(realm=self)

    @property
    def uma1(self):
        """
        :rtype: keycloak.uma1.KeycloakUMA1
        """
        return KeycloakUMA1(realm=self)

    def close(self):
        if self._client is not None:
            self._client.close()
            self._client = None

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()
class KeycloakClientTestCase(TestCase):

    def setUp(self):
        self.headers = {'initial': 'header'}
        self.server_url = 'https://example.com'

        self.client = KeycloakClient(server_url=self.server_url,
                                     headers=self.headers)

    def test_default_client_logger_name(self):
        """
        Case: Session get requested
        Expected: Session object get returned and the same one if called for
                  the second time
        """

        self.assertEqual(self.client.logger.name, 'KeycloakClient')

    def test_session(self):
        """
        Case: Session get requested
        Expected: Session object get returned and the same one if called for
                  the second time
        """
        session = self.client.session

        self.assertIsInstance(session, Session)

        self.assertEqual(session, self.client.session)

    def test_close_session(self):
        """
        Case: Session get requested
        Expected: Session object get returned and the same one if called for
                  the second time
        """
        self.client.close()
        self.assertIsNone(self.client._session)

    def test_get_full_url(self):
        """
        Case: retrieve a valid url
        Expected: The path get added to the base url or to the given url
        """

        self.assertEqual(self.client.get_full_url('/some/path'),
                         'https://example.com/some/path')

        self.assertEqual(self.client.get_full_url('/some/path',
                                                  'https://another_url.com'),
                         'https://another_url.com/some/path')

    @mock.patch('keycloak.client.requests', autospec=True)
    def test_post(self, request_mock):
        """
        Case: A POST request get executed
        Expected: The correct parameters get given to the request library
        """
        request_mock.Session.return_value.headers = mock.MagicMock()

        self.client._handle_response = mock.MagicMock()
        response = self.client.post(url='https://example.com/test',
                                    data={'some': 'data'},
                                    headers={'some': 'header'},
                                    extra='param')

        request_mock.Session.return_value.post.assert_called_once_with(
            'https://example.com/test',
            data={'some': 'data'},
            headers={'some': 'header'},
            params={'extra': 'param'}
        )
        self.client._handle_response.assert_called_once_with(
            request_mock.Session.return_value.post.return_value
        )
        self.assertEqual(response, self.client._handle_response.return_value)

    @mock.patch('keycloak.client.requests', autospec=True)
    def test_get(self, request_mock):
        """
        Case: A GET request get executed
        Expected: The correct parameters get given to the request library
        """
        request_mock.Session.return_value.headers = mock.MagicMock()

        self.client._handle_response = mock.MagicMock()
        response = self.client.get(url='https://example.com/test',
                                   headers={'some': 'header'},
                                   extra='param')

        request_mock.Session.return_value.get.assert_called_once_with(
            'https://example.com/test',
            headers={'some': 'header'},
            params={'extra': 'param'}
        )

        self.client._handle_response.assert_called_once_with(
            request_mock.Session.return_value.get.return_value
        )
        self.assertEqual(response, self.client._handle_response.return_value)

    @mock.patch('keycloak.client.requests', autospec=True)
    def test_put(self, request_mock):
        """
        Case: A PUT request get executed
        Expected: The correct parameters get given to the request library
        """
        request_mock.Session.return_value.headers = mock.MagicMock()

        self.client._handle_response = mock.MagicMock()
        response = self.client.put(url='https://example.com/test',
                                   data={'some': 'data'},
                                   headers={'some': 'header'},
                                   extra='param')

        request_mock.Session.return_value.put.assert_called_once_with(
            'https://example.com/test',
            data={'some': 'data'},
            headers={'some': 'header'},
            params={'extra': 'param'}
        )

        self.client._handle_response.assert_called_once_with(
            request_mock.Session.return_value.put.return_value
        )
        self.assertEqual(response,
                         self.client._handle_response.return_value)

    @mock.patch('keycloak.client.requests', autospec=True)
    def test_delete(self, request_mock):
        """
        Case: A DELETE request get executed
        Expected: The correct parameters get given to the request library
        """
        request_mock.Session.return_value.headers = mock.MagicMock()

        self.client._handle_response = mock.MagicMock()
        response = self.client.delete(url='https://example.com/test',
                                      headers={'some': 'header'},
                                      extra='param')

        request_mock.Session.return_value.delete.assert_called_once_with(
            'https://example.com/test',
            headers={'some': 'header'},
            extra='param'
        )
        self.assertEqual(response,
                         request_mock.Session.return_value.delete.return_value)

    def test_handle_response(self):
        """
        Case: Response get processed
        Expected: Decoded json get returned else raw_response
        """
        response = mock.MagicMock()

        processed_response = self.client._handle_response(response=response)

        response.raise_for_status.assert_called_once_with()
        response.json.assert_called_once_with()

        self.assertEqual(processed_response, response.json.return_value)

        response.json.side_effect = ValueError
        processed_response = self.client._handle_response(response=response)

        self.assertEqual(processed_response, response.content)
    def setUp(self):
        self.headers = {'initial': 'header'}
        self.server_url = 'https://example.com'

        self.client = KeycloakClient(server_url=self.server_url,
                                     headers=self.headers)