def test_ok(self): httpretty.register_uri(method=httpretty.POST, uri=self.url, body=b'{"correct_json": true}') result = make_request(url=self.url, method='POST') expected_result = {'correct_json': True} self.assertDictEqual(result, expected_result)
def esia_request(settings, url, token, accept_schema=None): headers = {"Authorization": "Bearer " + token} if accept_schema: headers["Accept"] = "application/json; schema='" + accept_schema + "'" else: headers["Accept"] = "application/json" full_esia_url = get_esia_base_url(settings.get("esia_url")) + url return make_request(url=full_esia_url, headers=headers)
def complete_authorization(self, code, state, validate_token=True, redirect_uri=None): """ Exchanges received code and state to access token, validates token (optionally), extracts ESIA user id from token and returns ESIAInformationConnector instance. :type code: str :type state: str :param boolean validate_token: perform token validation :param str or None redirect_uri: uri, where browser will be redirected after authorization. :rtype: EsiaInformationConnector :raises IncorrectJsonError: if response contains invalid json body :raises HttpError: if response status code is not 2XX :raises IncorrectMarkerError: if validate_token set to True and received token cannot be validated """ params = { 'client_id': self.settings.esia_client_id, 'code': code, 'grant_type': 'authorization_code', 'redirect_uri': redirect_uri or self.settings.redirect_uri, 'timestamp': get_timestamp(), 'token_type': 'Bearer', 'scope': self.settings.esia_scope, 'state': state, } params = sign_params(params, certificate_file=self.settings.certificate_file, private_key_file=self.settings.private_key_file) url = '{base_url}{token_url}'.format( base_url=self.settings.esia_service_url, token_url=self._TOKEN_EXCHANGE_URL) response_json = make_request(url=url, method='POST', data=params) id_token = response_json['id_token'] if validate_token: payload = self._validate_token(id_token) else: payload = self._parse_token(id_token) return EsiaInformationConnector( access_token=response_json['access_token'], oid=self._get_user_id(payload), settings=self.settings)
def esia_request(self, endpoint_url, accept_schema=None): """ Makes request to ESIA REST service and returns response JSON data. :param str endpoint_url: endpoint url :param str or None accept_schema: optional schema (version) for response data format :rtype: dict :raises IncorrectJsonError: if response contains invalid json body :raises HttpError: if response status code is not 2XX """ headers = {'Authorization': "Bearer %s" % self.token} if accept_schema: headers['Accept'] = 'application/json; schema="%s"' % accept_schema else: headers['Accept'] = 'application/json' return make_request(url=endpoint_url, headers=headers)
def esia_request(self, endpoint_url, accept_schema=None): """ Makes request to ESIA REST service and returns response JSON data. :param str endpoint_url: endpoint url :param str or None accept_schema: optional schema (version) for response data format :rtype: dict :raises IncorrectJsonError: if response contains invalid json body :raises HttpError: if response status code is not 2XX """ headers = { 'Authorization': "Bearer %s" % self.token } if accept_schema: headers['Accept'] = 'application/json; schema="%s"' % accept_schema else: headers['Accept'] = 'application/json' return make_request(url=endpoint_url, headers=headers)
def complete_authorization(self, code, state, validate_token=True, redirect_uri=None): """ Exchanges received code and state to access token, validates token (optionally), extracts ESIA user id from token and returns ESIAInformationConnector instance. :type code: str :type state: str :param boolean validate_token: perform token validation :param str or None redirect_uri: uri, where browser will be redirected after authorization. :rtype: EsiaInformationConnector :raises IncorrectJsonError: if response contains invalid json body :raises HttpError: if response status code is not 2XX :raises IncorrectMarkerError: if validate_token set to True and received token cannot be validated """ params = { 'client_id': self.settings.esia_client_id, 'code': code, 'grant_type': 'authorization_code', 'redirect_uri': redirect_uri or self.settings.redirect_uri, 'timestamp': get_timestamp(), 'token_type': 'Bearer', 'scope': self.settings.esia_scope, 'state': state, } params = sign_params(params, certificate_file=self.settings.certificate_file, private_key_file=self.settings.private_key_file) url = '{base_url}{token_url}'.format(base_url=self.settings.esia_service_url, token_url=self._TOKEN_EXCHANGE_URL) response_json = make_request(url=url, method='POST', data=params) id_token = response_json['id_token'] if validate_token: payload = self._validate_token(id_token) else: payload = self._parse_token(id_token) return EsiaInformationConnector(access_token=response_json['access_token'], oid=self._get_user_id(payload), settings=self.settings)
def complete_authorization(settings, code, state, validate=True, redirect_uri=None): params = { 'client_id': settings.get("mnemonic"), 'code': code, 'grant_type': 'authorization_code', 'redirect_uri': redirect_uri or settings.get("redirect_uri"), 'timestamp': get_timestamp(), 'token_type': 'Bearer', 'scope': settings.get("scope"), 'state': state, } params = sign_params_gost( params, public_cert_file_path=settings.get("certificate_file"), private_key_file_path=settings.get("private_key_file")) url = settings.get('esia_url') + TOKEN_EXCHANGE_URL response_json = make_request(url=url, method='POST', data=params) # id_token = response_json['id_token'] id_token = response_json['access_token'] if validate: payload = validate_token(settings, id_token) else: payload = utils.parse_token(id_token) return { "token": response_json['access_token'], "user_id": get_user_from_token_params(payload) }