i am using pysaml2 library in python. which has a method defined as below:
def _store_request(self, saml_msg):
key = sha1(saml_msg["SAMLRequest"]).hexdigest()
IDP.ticket[key] = saml_msg
return key
where, IDP.ticket = {}
is an application wide simple key value mapping(in a raw WSGI application).here IDP.ticket
persists between multiple requests . some context below.
The route that is called is SSO().redirect,
- Initially
self.user
isFalse
so key is being set by self._store_request(saml_msg) - Then user is returned with a form with the key set in form so that the request can be identified. On form submit SSO().redirect is called again & value set in
IDP.ticket
during previous request was obtained.
i want to port this method to a flask application but when using flask with Flask-Cache when i do cache.set(key,saml_msg)
i get error:
TypeError: can't pickle CompiledFFI objects
I gues its because saml_msg
contains a key
req_info': <saml2.request.AuthnRequest object at 0x1072302d0>
How can i store state between multiple requests in flask other than using FLask-Cache?
EDIT:.
this question is similar to this question but I have already tried that solution .I need to save an object
not a simple data type so when i save it using Flask-Cache i get `can't pickle CompiledFFI objects(maybe this is something with AuthnRequest object ,it can't be pickled)
Using flask.session
to store key between requests, it gives TypeError: Object of type AuthnRequest is not JSON serializable
.
full stack trace.
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