def expected_error_response(self, response): if isinstance(response, Response): # requests response response = ErrorResponse().from_json(response.content) if isinstance(response, ErrorResponse): if response["error"] not in self.expect_error["error"]: raise Break("Wrong error, got {} expected {}".format( response["error"], self.expect_error["error"])) if self.expect_error["stop"]: raise Break("Stop requested after received expected error") else: self.conv.trace.error("Expected error, didn't get it") raise Break("Did not receive expected error")
def expected_error_response(self, response): if isinstance(response, Response): # requests response # don't want bytes _txt = response.content.decode('utf8') response = ErrorResponse().from_json(_txt) if isinstance(response, ErrorResponse): self.conv.events.store(EV_PROTOCOL_RESPONSE, response, sender=self.__class__.__name__) if response["error"] not in self.expect_error["error"]: raise Break("Wrong error, got {} expected {}".format( response["error"], self.expect_error["error"])) try: if self.expect_error["stop"]: raise Break("Stop requested after received expected error") except KeyError: pass else: self.conv.trace.error("Expected error, didn't get it") raise Break("Did not receive expected error") return response
def run(self): _client = self.conv.entity url, body, ht_args, csi = _client.request_info( self.request, method=self.method, request_args=self.req_args, target=_client.provider_info["issuer"], **self.op_args) try: http_args = self.op_args["http_args"] except KeyError: http_args = ht_args else: http_args.update(ht_args) self.conv.events.store(EV_REQUEST, csi, sender=self.__class__.__name__) self.conv.trace.info(20 * "=" + " " + self.__class__.__name__ + " " + 20 * "=") self.conv.trace.request("URL: {}".format(url)) if body: self.conv.trace.request("BODY: {}".format(body)) http_response = self.do_request(_client, url, body, http_args) response = self.catch_exception(self.handle_response, r=http_response, csi=csi) self.conv.trace.response(response) self.conv.events.store(EV_RESPONSE, response, sender=self.__class__.__name__) # self.sequence.append((response, http_response.text)) if self.expect_error: response = self.expected_error_response(response) else: if isinstance(response, ErrorResponse): raise Break("Unexpected error response") return response
def parse_response(self, path, inut, message_factory): _ctype = self.response_type _conv = self.conv if self.csi is None: url, body, ht_args, csi = _conv.entity.request_info( self.request, method=self.method, request_args=self.req_args, **self.op_args) self.csi = csi try: response_mode = self.csi["response_mode"] except KeyError: response_mode = None if self.request_cls == "AuthorizationRequest": try: _rt = self.csi["response_type"] except KeyError: response_where = "" else: if _rt == ["code"]: response_where = "url" elif _rt == [""]: response_where = "" else: response_where = "fragment" else: response_where = self.response_where # parse the response if response_mode == "form_post": info = parse_qs(get_post(inut.environ)) _ctype = "dict" elif response_where == "url": info = inut.environ["QUERY_STRING"] _ctype = "urlencoded" elif response_where == "fragment": query = parse_qs(get_post(inut.environ)) try: info = query["fragment"][0] except KeyError: return inut.sorry_response(inut.conf.BASE, "missing fragment ?!") elif response_where == "": info = inut.environ["QUERY_STRING"] _ctype = "urlencoded" else: # resp_c.where == "body" info = get_post(inut.environ) logger.info("Response: %s" % info) _conv.trace.reply(info) ev_index = _conv.events.store(EV_RESPONSE, info, sender=self.__class__.__name__) resp_cls = message_factory(self.response_cls) algs = _conv.entity.sign_enc_algs("id_token") try: response = _conv.entity.parse_response(resp_cls, info, _ctype, self.csi["state"], keyjar=_conv.entity.keyjar, algs=algs) except ResponseError as err: return inut.err_response("run_sequence", err) except Exception as err: return inut.err_response("run_sequence", err) logger.info("Parsed response: %s" % response.to_dict()) _conv.trace.response(response) _conv.events.store(EV_PROTOCOL_RESPONSE, response, ref=ev_index, sender=self.__class__.__name__) if self.expect_error: self.expected_error_response(response) else: if isinstance(response, ErrorResponse): raise Break("Unexpected error response")