Example #1
0
async def run_as_login(request, user: str, raw: bool = False) -> web.Response:
    """User/password authentication to get an access token.
    This method should be called to get an API token using an authorization context body. This token will expire at some time. # noqa: E501

    Parameters
    ----------
    request : connexion.request
    user : str
        Name of the user who wants to be authenticated.
    raw : bool, optional
        Respond in raw format. Default `False`

    Returns
    -------
    web.Response
        Raw or JSON response with the generated access token.
    """
    f_kwargs = {'user_id': user, 'auth_context': await request.json()}

    dapi = DistributedAPI(f=preprocessor.get_permissions,
                          f_kwargs=remove_nones_to_dict(f_kwargs),
                          request_type='local_master',
                          is_async=False,
                          logger=logger
                          )
    data = raise_if_exc(await dapi.distribute_function())

    token = None
    try:
        token = generate_token(user_id=user, data=data.dikt, run_as=True)
    except WazuhException as e:
        raise_if_exc(e)

    return web.Response(text=token, content_type='text/plain', status=200) if raw \
        else web.json_response(data=WazuhResult({'data': TokenResponseModel(token=token)}), status=200, dumps=dumps)
Example #2
0
async def login_user(request, user: str, raw=False):
    """User/password authentication to get an access token.
    This method should be called to get an API token. This token will expire at some time. # noqa: E501

    Parameters
    ----------
    request : connexion.request
    user : str
        Name of the user who wants to be authenticated
    raw : bool, optional
        Respond in raw format

    Returns
    -------
    TokenResponseModel
    """
    f_kwargs = {'user_id': user}
    try:
        # Add authorization context in case there is body in request
        f_kwargs['auth_context'] = await request.json()
    except JSONDecodeError:
        pass

    dapi = DistributedAPI(f=preprocessor.get_permissions,
                          f_kwargs=remove_nones_to_dict(f_kwargs),
                          request_type='local_master',
                          is_async=False,
                          logger=logger)
    data = raise_if_exc(await dapi.distribute_function())

    token = None
    try:
        token = generate_token(user_id=user,
                               data=data.dikt,
                               run_as='auth_context' in f_kwargs.keys())
    except WazuhException as e:
        raise_if_exc(e)

    if raw:
        return web.Response(text=token, content_type='text/plain', status=200)
    else:
        return web.json_response(data=WazuhResult(
            {'data': TokenResponseModel(token=token)}),
                                 status=200,
                                 dumps=dumps)