Пример #1
0
    async def enroll_factor(self,
                            userId,
                            user_factor,
                            query_params={},
                            keep_empty_params=False):
        """
        Enrolls a user with a supported factor.
        Args:
            user_id {str}
            {user_factor}
            query_params {dict}: Map of query parameters for request
            [query_params.updatePhone] {str}
            [query_params.templateId] {str}
            [query_params.tokenLifetimeSeconds] {str}
            [query_params.activate] {str}
        Returns:
            UserFactor
        """
        http_method = "post".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/users/{userId}/factors
            """)
        if query_params:
            encoded_query_params = urlencode(query_params)
            api_url += f"/?{encoded_query_params}"

        if isinstance(user_factor, dict):
            body = user_factor
        else:
            body = user_factor.as_dict()
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        form = {}

        request, error = await self._request_executor.create_request(
            http_method,
            api_url,
            body,
            headers,
            form,
            keep_empty_params=keep_empty_params)

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, UserFactor)

        if error:
            return (None, response, error)

        try:
            body = self.form_response_body(response.get_body())
            result = find_factor_model(body["factorType"])(body)
        except Exception as error:
            return (None, response, error)
        return (result, response, None)
Пример #2
0
    async def activate_factor(self,
                              userId,
                              factorId,
                              activate_factor_request,
                              keep_empty_params=False):
        """
        The `sms` and `token:software:totp` factor types requir
        e activation to complete the enrollment process.
        Args:
            user_id {str}
            factor_id {str}
            {activate_factor_request}
        Returns:
            UserFactor
        """
        http_method = "post".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/users/{userId}/factors/{factorId}/lifecycle
                /activate
            """)

        if isinstance(activate_factor_request, dict):
            body = activate_factor_request
        else:
            body = activate_factor_request.as_dict()
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        form = {}

        request, error = await self._request_executor.create_request(
            http_method,
            api_url,
            body,
            headers,
            form,
            keep_empty_params=keep_empty_params)

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, UserFactor)

        if error:
            return (None, response, error)

        try:
            body = self.form_response_body(response.get_body())
            result = find_factor_model(body["factorType"])(body)
        except Exception as error:
            return (None, response, error)
        return (result, response, None)
Пример #3
0
    async def list_factors(self, userId, keep_empty_params=False):
        """
        Enumerates all the enrolled factors for the specified u
        ser
        Args:
            user_id {str}
        Returns:
            list: Collection of UserFactor instances.
        """
        http_method = "get".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/users/{userId}/factors
            """)

        body = {}
        headers = {}
        form = {}

        request, error = await self._request_executor.create_request(
            http_method,
            api_url,
            body,
            headers,
            form,
            keep_empty_params=keep_empty_params)

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, UserFactor)

        if error:
            return (None, response, error)

        try:
            result = []
            for item in response.get_body():
                result.append(
                    find_factor_model(item["factorType"])(
                        self.form_response_body(item)))
        except Exception as error:
            return (None, response, error)
        return (result, response, None)
Пример #4
0
    async def get_factor(self, userId, factorId, keep_empty_params=False):
        """
        Fetches a factor for the specified user
        Args:
            user_id {str}
            factor_id {str}
        Returns:
            UserFactor
        """
        http_method = "get".upper()
        api_url = format_url(f"""
            {self._base_url}
            /api/v1/users/{userId}/factors/{factorId}
            """)

        body = {}
        headers = {}
        form = {}

        request, error = await self._request_executor.create_request(
            http_method,
            api_url,
            body,
            headers,
            form,
            keep_empty_params=keep_empty_params)

        if error:
            return (None, None, error)

        response, error = await self._request_executor\
            .execute(request, UserFactor)

        if error:
            return (None, response, error)

        try:
            body = self.form_response_body(response.get_body())
            result = find_factor_model(body["factorType"])(body)
        except Exception as error:
            return (None, response, error)
        return (result, response, None)