Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customize deriva_ctx.deriva_response.set_cookie to observe webauthn2_… #86

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion webauthn2/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,30 @@ def request_trace(tracedata):
webauthn2_context=deriva_ctx.webauthn2_context,
))

class _Response (flask.Response):
"""Like flask.Response but customizing set_cookie behavior for webauthn"""

def set_cookie(self, key, value='', max_age=None, expires=None, path=None, domain=None, secure=None, httponly=False):
"""Allow webauthn config to customize default cookie parameters"""
if path is None:
path = _manager.config.get('web_cookie_path', '/')

if domain is None:
domain = _manager.config.get('web_cookie_domain', None)
if domain is True:
domain = '.%s' % (flask.request.host,)

if secure is None:
secure = _manager.config.get('web_cookie_secure', None)

return super(_Response, self).set_cookie(
key, value=value, max_age=max_age, expires=expires, path=path, domain=domain, secure=secure, httponly=httponly)

@app.before_request
def before_request():
# request context init
deriva_ctx.webauthn_dispatched_handler = None
deriva_ctx.deriva_response = flask.Response() # allow us to accumulate response content by side-effect
deriva_ctx.deriva_response = _Response() # allow us to accumulate response content by side-effect
deriva_ctx.webauthn_request_guid = base64.b64encode( struct.pack('Q', random.getrandbits(64)) ).decode()
deriva_ctx.webauthn_start_time = datetime.datetime.now(timezone.utc)
deriva_ctx.webauthn_request_content_range = None
Expand Down
Loading