예제 #1
0
 def expel_user_from_group(
         user_url: str,
         token: Union[str, Token],
         user: Union[UserRepresentation, str],
         group: Union[GroupRepresentation, str],
         response_debug_processor: DebugProcessorType = None
 ) -> ApiResponse:
     user_id = get_id(user)
     group_id = get_id(group)
     url = f'{user_url.rstrip("/")}/{user_id}/groups/{group_id}'
     headers = {'Authorization': str(token)}
     response = requests.delete(url, headers=headers)
     return api_response(response,
                         response_debug_processor=response_debug_processor)
예제 #2
0
def get_users(*,
              base_url: str,
              realm: str,
              token: Token,
              user_or_id: Optional[Union[str, UserRepresentation]] = None,
              response_debug_processor: DebugProcessorType = None,
              with_groups: bool = True,
              **kwargs) -> ApiResponse:
    url = base_url.format(realm=realm)
    user_id = get_id(user_or_id, raise_on_error=False)
    if user_id:
        url = f'{url.rstrip("/")}/{user_id}'
        if with_groups:
            user_data = KeyCloakAPI.get(
                url=url,
                token=token,
                realm=realm,
                response_debug_processor=response_debug_processor,
                response_data_type=UserRepresentation,
                **kwargs)
            if user_data.success:
                groups_data = KeyCloakAPI.get_user_groups(user_url=url,
                                                          token=token)
                if isinstance(user_data.data, dict):
                    user_data.data['groups'] = groups_data.data
                else:
                    user_data.data.groups = groups_data.data
            return user_data
    return KeyCloakAPI.get(url=url,
                           token=token,
                           realm=realm,
                           response_debug_processor=response_debug_processor,
                           response_data_type=UserRepresentation,
                           **kwargs)
예제 #3
0
def put_entity(
    *,
    base_url: str,
    realm: str,
    token: Token,
    entity: Union[UserRepresentation, GroupRepresentation],
    response_debug_processor: DebugProcessorType = None,
) -> ApiResponse:
    entity_id = get_id(entity)
    url = base_url.format(realm=realm)
    url = f'{url.rstrip("/")}/{entity_id}'
    return KeyCloakAPI.put(url=url,
                           token=token,
                           body=entity,
                           response_debug_processor=response_debug_processor)
예제 #4
0
def add_subgroup(*,
                 group_url: str,
                 realm: str,
                 token: Token,
                 parent: Union[GroupRepresentation, str],
                 child: Union[GroupRepresentation, str],
                 response_debug_processor: DebugProcessorType = None):
    parent_id = get_id(parent)
    url = f'{group_url.rstrip("/")}/{parent_id}/children'
    if isinstance(child, str):
        child = GroupRepresentation(name=child)
    return post_entity(base_url=url,
                       realm=realm,
                       token=token,
                       entity=child,
                       response_debug_processor=response_debug_processor)
예제 #5
0
def delete_entity(
        *,
        base_url: str,
        realm: str,
        token: Token,
        entity_or_id: Union[UserRepresentation, GroupRepresentation, str],
        response_debug_processor: DebugProcessorType = None) -> ApiResponse:
    entity_id = get_id(entity_or_id)

    url = base_url.format(realm=realm)
    url = f'{url.rstrip("/")}/{entity_id}'

    return KeyCloakAPI.delete(
        url=url,
        token=token,
        response_debug_processor=response_debug_processor)
예제 #6
0
 def add_subgroup_to_group(
         group_url: str,
         token: Union[str, Token],
         parent: Union[GroupRepresentation, str],
         child: GroupRepresentation,
         response_debug_processor: DebugProcessorType = None
 ) -> ApiResponse:
     parent_id = get_id(parent)
     url = f'{group_url.rstrip("/")}/{parent_id}/children'
     headers = {
         'Authorization': str(token),
         'Content-Type': 'application/json'
     }
     response = requests.post(url,
                              headers=headers,
                              json=child.dict(exclude_unset=True))
     return api_response(response,
                         response_debug_processor=response_debug_processor)
예제 #7
0
def get_groups(*,
               base_url: str,
               realm: str,
               token: Token,
               group_or_id: Optional[Union[str, GroupRepresentation]] = None,
               search: Optional[str] = None,
               response_debug_processor: DebugProcessorType = None,
               with_members: bool = True) -> ApiResponse:
    search_kwarg = dict()
    url = base_url.format(realm=realm)
    group_id = get_id(group_or_id, raise_on_error=False)
    if group_id:
        url = f'{url.rstrip("/")}/{group_id}'
        if with_members:
            group_data = KeyCloakAPI.get(
                url=url,
                token=token,
                realm=realm,
                response_debug_processor=response_debug_processor,
                response_data_type=GroupRepresentation,
            )
            if group_data.success:
                members_data = KeyCloakAPI.get_group_members(group_url=url,
                                                             token=token)
                if isinstance(group_data.data, dict):
                    group_data.data['members'] = members_data.data
                else:
                    group_data.data.members = members_data.data
            return group_data
    elif search:
        search_kwarg['search'] = search

    return KeyCloakAPI.get(url=url,
                           token=token,
                           realm=realm,
                           response_debug_processor=response_debug_processor,
                           response_data_type=GroupRepresentation,
                           **search_kwarg)