我在 python 中使用pysaml2库。它有一个定义如下的方法:
def _store_request(self, saml_msg):
key = sha1(saml_msg["SAMLRequest"]).hexdigest()
IDP.ticket[key] = saml_msg
return key
其中,IDP.ticket = {}
是应用程序范围的简单键值映射(在原始 WSGI 应用程序中) 。这里IDP.ticket
在多个请求之间持续存在。下面的一些上下文。
被调用的路由是SSO().redirect,
- 最初
self.user
是False
由 self._store_request(saml_msg)设置密钥 - 然后用户返回一个表单,表单中设置了键,以便可以识别请求。在表单上提交SSO().redirect再次被调用
IDP.ticket
并获得在先前请求期间设置的值。
我想将此方法移植到烧瓶应用程序,但是当我使用带有Flask-Cache的烧瓶时,cache.set(key,saml_msg)
我得到错误:
TypeError: can't pickle CompiledFFI objects
我猜是因为它saml_msg
包含一个密钥
req_info': <saml2.request.AuthnRequest object at 0x1072302d0>
除了使用 Flask-Cache 之外,我如何在烧瓶中的多个请求之间存储状态?
编辑:。
这个问题与这个问题类似, 但我已经尝试过这个解决方案。我需要保存object
一个不简单的数据类型,所以当我使用 Flask-Cache 保存它时,我会得到`can't pickle CompiledFFI objects(也许这是AuthnRequest的问题对象,不能腌制)
flask.session
用于在请求之间存储密钥,它给出TypeError: Object of type AuthnRequest is not JSON serializable
.
完整的堆栈跟踪。
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1969, in finalize_request
response = self.process_response(response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2268, in process_response
self.session_interface.save_session(self, ctx.session, response)
File "/usr/local/lib/python3.7/site-packages/flask/sessions.py", line 378, in save_session
val = self.get_signing_serializer(app).dumps(dict(session))
File "/usr/local/lib/python3.7/site-packages/itsdangerous/serializer.py", line 166, in dumps
payload = want_bytes(self.dump_payload(obj))
File "/usr/local/lib/python3.7/site-packages/itsdangerous/url_safe.py", line 42, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "/usr/local/lib/python3.7/site-packages/itsdangerous/serializer.py", line 133, in dump_payload
return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs))
File "/usr/local/lib/python3.7/site-packages/flask/json/tag.py", line 305, in dumps
return dumps(self.tag(value), separators=(",", ":"))
File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 211, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 100, in default
return _json.JSONEncoder.default(self, o)
File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type AuthnRequest is not JSON serializable