3

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 is False 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
anekix
  • 2,393
  • 2
  • 30
  • 57
  • 1
    @Craicerjack, even though the question is somewhat similar context is completely different, i read answers there before posting question. Flask-cache is being suggested as one answer but i mentioned explicitly in my question The error is being thrown by flask-cache, i tried that so looking for solution – anekix Oct 07 '19 at 09:25
  • The flask `session` object no good? - http://flask.palletsprojects.com/en/1.1.x/quickstart/#sessions – Craicerjack Oct 07 '19 at 09:29
  • Your question does not include enough information to make an informed answer. The stack-trace would help a big bunch already. I've tried googling around but with the little information given it is like looking for a needle in a haystack – exhuma Oct 07 '19 at 09:32
  • @Craicerjack i missed that , my bad :( .i will check & should i delete this question if it works? or just leave it here ? – anekix Oct 07 '19 at 09:33
  • ... additionally, try to formulate a [mcve](https://stackoverflow.com/help/minimal-reproducible-example). Often by isolating the issue like this helps already figuring out important things yourself, and, if not, makes it a hell of a lot easier for finding an answer to the question – exhuma Oct 07 '19 at 09:34
  • @exhuma oky, will make sure – anekix Oct 07 '19 at 09:36
  • @Craicerjack on using flask `session` i get `TypeError: Object of type AuthnRequest is not JSON serializable` – anekix Oct 07 '19 at 09:44

0 Answers0