Beispiel #1
0
 def is_course_in_catalog(self, course_id):
     """
     Check whether course exists in enterprise customer catalog.
     """
     enterprise_customer = utils.get_enterprise_customer(
         self._enterprise_customer.uuid)
     return enterprise_customer.catalog_contains_course(course_id)
Beispiel #2
0
    def get(self, request, *args, **kwargs):  # pylint: disable=arguments-differ
        enterprise_customer_uuid = request.GET.get('ciid')
        if not enterprise_customer_uuid:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    "message":
                    (u"Cornerstone course list API expects ciid parameter.")
                })

        enterprise_customer = get_enterprise_customer(enterprise_customer_uuid)
        if not enterprise_customer:
            return Response(status=status.HTTP_404_NOT_FOUND,
                            data={
                                "message":
                                (u"No enterprise data found for given ciid.")
                            })

        worker_user = get_enterprise_worker_user()
        enterprise_config = CornerstoneEnterpriseCustomerConfiguration.objects.get(
            enterprise_customer=enterprise_customer)
        exporter = enterprise_config.get_content_metadata_exporter(worker_user)
        transmitter = enterprise_config.get_content_metadata_transmitter()
        data = transmitter.transmit(exporter.export())
        return Response(data)
Beispiel #3
0
 def test_get_enterprise_customer(self, factory, items, returns_obj):
     if factory:
         create_items(factory, items)
     enterprise_customer = utils.get_enterprise_customer(TEST_UUID)
     if returns_obj:
         self.assertIsNotNone(enterprise_customer)
     else:
         self.assertIsNone(enterprise_customer)
Beispiel #4
0
 def clean_course(self):
     """
     Verify course ID has an associated course in LMS.
     """
     course_id = self.cleaned_data[self.Fields.COURSE].strip()
     if not course_id:
         return None
     enterprise_customer = utils.get_enterprise_customer(self._enterprise_customer.uuid)
     utils.validate_course_exists_for_enterprise(enterprise_customer, course_id)
     return course_id
Beispiel #5
0
 def clean_course(self):
     """
     Verify course ID and retrieve course details.
     """
     course_id = self.cleaned_data[self.Fields.COURSE].strip()
     if not course_id:
         return None
     enterprise_customer = utils.get_enterprise_customer(self._enterprise_customer.uuid)
     course_details = utils.validate_course_exists_for_enterprise(enterprise_customer, course_id)
     return course_details
Beispiel #6
0
def get_program_data_sharing_consent(username, program_uuid,
                                     enterprise_customer_uuid):
    """
    Get the data sharing consent object associated with a certain user of a customer for a program.

    :param username: The user that grants consent.
    :param program_uuid: The program for which consent is granted.
    :param enterprise_customer_uuid: The consent requester.
    :return: The data sharing consent object
    """
    enterprise_customer = get_enterprise_customer(enterprise_customer_uuid)
    discovery_client = CourseCatalogApiServiceClient(enterprise_customer.site)
    course_ids = discovery_client.get_program_course_keys(program_uuid)
    child_consents = (get_data_sharing_consent(username,
                                               enterprise_customer_uuid,
                                               course_id=individual_course_id)
                      for individual_course_id in course_ids)
    return ProxyDataSharingConsent.from_children(program_uuid, *child_consents)
Beispiel #7
0
    def get(self, request, *args, **kwargs):  # pylint: disable=arguments-differ
        app_config = apps.get_app_config('blackboard')
        oauth_token_path = app_config.oauth_token_auth_path

        # Check if encountered an error when generating the oauth code.
        request_error = request.GET.get('error')
        if request_error:
            raise APIException(
                "Blackboard OAuth API encountered an error when generating client code - "
                "error: {} description: {}".format(
                    request_error, request.GET.get('error_description')))

        # Retrieve the newly generated code and state (Enterprise user's ID)
        client_code = request.GET.get('code')
        enterprise_customer_uuid = request.GET.get('state')
        if not enterprise_customer_uuid:
            raise ParseError(
                "Enterprise ID (as 'state' url param) needed to obtain refresh token"
            )

        if not client_code:
            raise ParseError(
                "'code' url param was not provided, needed to obtain refresh token"
            )

        enterprise_customer = get_enterprise_customer(enterprise_customer_uuid)
        if not enterprise_customer:
            raise NotFound(
                "No enterprise data found for given uuid: {}.".format(
                    enterprise_customer_uuid))

        try:
            enterprise_config = BlackboardEnterpriseCustomerConfiguration.objects.get(
                enterprise_customer=enterprise_customer)
        except BlackboardEnterpriseCustomerConfiguration.DoesNotExist as error:
            raise NotFound(
                "No Blackboard configuration found for enterprise: {}".format(
                    enterprise_customer_uuid)) from error

        auth_header = self._create_auth_header(enterprise_config)

        access_token_request_params = {
            'grant_type': 'authorization_code',
            'redirect_uri':
            settings.LMS_INTERNAL_ROOT_URL + "/blackboard/oauth-complete",
            'code': client_code,
        }

        auth_token_url = urljoin(enterprise_config.blackboard_base_url,
                                 oauth_token_path)
        auth_response = requests.post(auth_token_url,
                                      access_token_request_params,
                                      headers={
                                          'Authorization':
                                          auth_header,
                                          'Content-Type':
                                          'application/x-www-form-urlencoded'
                                      })

        try:
            data = auth_response.json()
            refresh_token = data['refresh_token']
        except KeyError as exception:
            raise ParseError(
                "BLACKBOARD: failed to find refresh_token in auth response. "
                "Auth response text: {}, Response code: {}, JSON response: {}".
                format(
                    auth_response.text,
                    auth_response.status_code,
                    data,
                )) from exception
        except ValueError as exception:
            raise ParseError(
                "BLACKBOARD: auth response is invalid json. auth_response: {}".
                format(auth_response)) from exception

        enterprise_config.refresh_token = refresh_token
        enterprise_config.save()

        return Response()
Beispiel #8
0
    def get(self, request, *args, **kwargs):  # pylint: disable=arguments-differ
        app_config = apps.get_app_config('canvas')
        canvas_oauth_token_path = app_config.oauth_token_auth_path

        # Check if Canvas encountered an error when generating the oauth code.
        canvas_request_error = request.GET.get('error')
        if canvas_request_error:
            raise APIException(
                'Canvas OAuth API encountered an error when generating client code- error: {} description: {}'
                .format(canvas_request_error,
                        request.GET.get('error_description')))

        # Retrieve the newly generated code and state (Enterprise user's ID)
        client_code = request.GET.get('code')
        enterprise_customer_uuid = request.GET.get('state')
        if not enterprise_customer_uuid:
            raise ParseError(
                "Enterprise ID required to integrate with Canvas.")

        if not client_code:
            raise ParseError("Client code required to integrate with Canvas.")

        enterprise_customer = get_enterprise_customer(enterprise_customer_uuid)
        if not enterprise_customer:
            raise NotFound(
                "No enterprise data found for given uuid: {}.".format(
                    enterprise_customer_uuid))

        try:
            enterprise_config = CanvasEnterpriseCustomerConfiguration.objects.get(
                enterprise_customer=enterprise_customer)
        except CanvasEnterpriseCustomerConfiguration.DoesNotExist as no_config_exception:
            raise NotFound(
                "No enterprise canvas configuration found associated with enterprise customer: {}"
                .format(enterprise_customer_uuid)) from no_config_exception

        access_token_request_params = {
            'grant_type': 'authorization_code',
            'client_id': enterprise_config.client_id,
            'client_secret': enterprise_config.client_secret,
            'redirect_uri':
            settings.LMS_INTERNAL_ROOT_URL + "/canvas/oauth-complete",
            'code': client_code,
        }

        auth_token_url = urljoin(enterprise_config.canvas_base_url,
                                 canvas_oauth_token_path)

        auth_response = requests.post(auth_token_url,
                                      access_token_request_params)

        try:
            data = auth_response.json()
            refresh_token = data['refresh_token']
        except (KeyError, ValueError) as error:
            raise requests.RequestException(response=auth_response) from error

        enterprise_config.refresh_token = refresh_token
        enterprise_config.save()

        return Response()