def generic(self, method, path, data='', content_type=None, secure=False, **extra): extra.update({ 'HTTP_HOST': 'testserver', 'HTTP_ACCEPT': self.content_type, }) if extra.get('authorized', False): device = Device.objects.get(pk=self.device_id) token = encode_token(device.id, device.public_key) extra['HTTP_AUTHORIZATION'] = 'JWT {}'.format(token) return super().generic(method, path, data, self.content_type, secure, **extra)
def request(self, method, url, *args, **kwargs): if not is_secure_transport(url): raise InsecureTransport('Please make sure to use HTTPS') if not verify_host(url, [self.host]): raise InvalidHost( 'Please verify the client is using "{}" has host'.format(self.host)) data = kwargs.get('data', {}) parse_result = urllib.parse.urlparse(url) dist = pkg_resources.get_distribution('keybar') headers = { 'User-Agent': user_agent('keybar', dist.version), 'Host': parse_result.netloc, 'Method': method, 'Path': parse_result.path, 'Accept': self.content_type, 'Content-Type': self.content_type, } if self.device_id and self.secret: headers['Authorization'] = 'JWT {}'.format( encode_token(self.device_id, self.secret) ) headers.update(kwargs.pop('headers', {})) kwargs.update({ 'headers': headers, 'data': data, 'cert': (settings.KEYBAR_CLIENT_CERTIFICATE, settings.KEYBAR_CLIENT_KEY), 'verify': settings.KEYBAR_CA_BUNDLE, 'timeout': self.timeout }) return super(Client, self).request(method, url, *args, **kwargs)