def make_service_request(service_name, endpoint, method=GET, data=None): """ Makes a JWT token-authenticated service request to the URL provided. Args: service_name: name of the service making this request. e.g. mp-users endpoint: the url to use method: HTTP method. supported methods are defined at this module's global variables data: request payload in case we are making a POST request Returns: text content of the response """ jwt_token = jwt_encode_handler(service_jwt_payload_handler(service_name)) headers = {'Authorization': 'JWT {}'.format(jwt_token), 'Content-Type': 'application/vnd.api+json'} if method not in [GET, POST, PUT, PATCH]: raise UnsupportedHTTPMethodException( "Method {0} is not supported. service_name: {1}, endpoint: {2}".format(method, service_name, endpoint)) response = getattr(requests, method)(endpoint, headers=headers, json=data) if 400 <= response.status_code < 600: http_error_msg = '{0} Error: {1} for {2}. Content: {3}'.format( response.status_code, response.reason, response.url, response.text) raise ServiceRequestException(http_error_msg, response) return response
def create_django_request_object(roles, query_string, method, user_id=None, body=None, http_host=None): """Create a Django HTTPRequest object using JWT for auth. Args: roles: roles used to cosntruct the JWT for authorization. qeuery_string: a query string like `?user=1` method: A http verb like 'get'. user_id: The id of the user making the request, used in the JWT. body: Data passed to the request. http_host: The DNS name of the requestor. Returns: HttpRequest object. """ from django.http import HttpRequest, QueryDict from zc_common.jwt_auth.utils import jwt_encode_handler if not http_host: http_host = 'local.zerocater.com' jwt_payload = {'roles': roles} if user_id: jwt_payload['id'] = user_id request = HttpRequest() # This is immediately after initializing request since setting the encoding # property will delete .GET from the object request.encoding = 'utf-8' # For Django < 1.9 request.GET = QueryDict(query_string) # For Django >= 1.10 request.query_params = QueryDict(query_string) # For Django REST Framework >= 3.4.7 request._read_started = False if body: request.raw_body = body request.method = method.upper() request.META = { 'HTTP_AUTHORIZATION': 'JWT {}'.format(jwt_encode_handler(jwt_payload)), 'QUERY_STRING': query_string, 'HTTP_HOST': http_host, 'CONTENT_TYPE': 'application/vnd.api+json', 'CONTENT_LENGTH': '99999', } return request
def create_django_request_object(roles, query_string, method, user_id=None, body=None, http_host=None): """ Create a Django HTTPRequest object with the appropriate attributes pulled from the event. """ from django.http import HttpRequest, QueryDict from zc_common.jwt_auth.utils import jwt_encode_handler if not http_host: http_host = 'local.zerocater.com' jwt_payload = {'roles': roles} if user_id: jwt_payload['id'] = user_id request = HttpRequest() # This is immediately after initializing request since setting the encoding # property will delete .GET from the object request.encoding = 'utf-8' # For Django < 1.9 request.GET = QueryDict(query_string) # For Django >= 1.10 request.query_params = QueryDict(query_string) # For Django REST Framework >= 3.4.7 request._read_started = False if body: request.raw_body = body request.method = method.upper() request.META = { 'HTTP_AUTHORIZATION': 'JWT {}'.format(jwt_encode_handler(jwt_payload)), 'QUERY_STRING': query_string, 'HTTP_HOST': http_host, 'CONTENT_TYPE': 'application/vnd.api+json', 'CONTENT_LENGTH': '99999', } return request
def generate_token(user): payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) return "JWT {}".format(token)