Beispiel #1
0
    def post(self, request, *args, **kwargs):
        ip = request.META.get('REMOTE_ADDR')
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response_status, cookies, response_body = authenticate_company(
            base_url=serializer.validated_data['base_url'],
            username=serializer.validated_data['username'],
            password=serializer.validated_data['password'],
        )
        server_type = serializer.validated_data['type']
        if response_status == 200:
            auth_json = ModifyAuthJson()
            json_file_data = auth_json.get_auth_data()
            json_file_data[server_type] = {
                'base_url': serializer.validated_data['base_url'],
                'username': serializer.validated_data['username'],
                'password': serializer.validated_data['password'],
                'cookies': cookies.get_dict()
            }
            auth_json.set_auth_data(json_file_data)

            return Response({
                'success': True,
                'data': {
                    server_type + '-authenticated': True
                }
            }, status=status.HTTP_200_OK)

        return Response({
            'success': False,
            'data': {
                'response_status': response_status,
                'auth_response_body': response_body
            }
        }, status=status.HTTP_401_UNAUTHORIZED)
def re_auth(server_type):
    auth_json = ModifyAuthJson()
    json_file_data = auth_json.get_auth_data()
    response_status, cookies, response_body = authenticate_company(
        base_url=auth_json.get_base_url(server_type),
        username=auth_json.get_username(server_type),
        password=auth_json.get_password(server_type),
    )
    if response_status != 200:
        raise Exception('Not able to authenticate source server')

    json_file_data[server_type]['cookies'] = cookies.get_dict()
    if server_type == 'target':
        json_file_data[server_type]['cookies']['XSRF-TOKEN'] = get_account(
            base_url=auth_json.get_base_url(server_type),
            cookies=cookies.get_dict())

    auth_json.set_auth_data(json_file_data)