summary refs log tree commit diff
path: root/synapse/rest/client/v2_alpha/_base.py
diff options
context:
space:
mode:
authorRichard van der Hoff <github@rvanderhoff.org.uk>2017-12-05 09:40:38 +0000
committerGitHub <noreply@github.com>2017-12-05 09:40:38 +0000
commitaa6ecf09846951bd822970ba2b6d7d38abeec0ff (patch)
treef675cc0f589fb974b529024ca3403be38f03a628 /synapse/rest/client/v2_alpha/_base.py
parentMerge pull request #2716 from matrix-org/rav/federation_client_post (diff)
parentRefactor UI auth implementation (diff)
downloadsynapse-aa6ecf09846951bd822970ba2b6d7d38abeec0ff.tar.xz
Merge pull request #2727 from matrix-org/rav/refactor_ui_auth_return
Refactor UI auth implementation
Diffstat (limited to 'synapse/rest/client/v2_alpha/_base.py')
-rw-r--r--synapse/rest/client/v2_alpha/_base.py41
1 files changed, 38 insertions, 3 deletions
diff --git a/synapse/rest/client/v2_alpha/_base.py b/synapse/rest/client/v2_alpha/_base.py

index 1f5bc24cc3..77434937ff 100644 --- a/synapse/rest/client/v2_alpha/_base.py +++ b/synapse/rest/client/v2_alpha/_base.py
@@ -15,12 +15,13 @@ """This module contains base REST classes for constructing client v1 servlets. """ - -from synapse.api.urls import CLIENT_V2_ALPHA_PREFIX +import logging import re -import logging +from twisted.internet import defer +from synapse.api.errors import InteractiveAuthIncompleteError +from synapse.api.urls import CLIENT_V2_ALPHA_PREFIX logger = logging.getLogger(__name__) @@ -57,3 +58,37 @@ def set_timeline_upper_limit(filter_json, filter_timeline_limit): filter_json['room']['timeline']["limit"] = min( filter_json['room']['timeline']['limit'], filter_timeline_limit) + + +def interactive_auth_handler(orig): + """Wraps an on_POST method to handle InteractiveAuthIncompleteErrors + + Takes a on_POST method which returns a deferred (errcode, body) response + and adds exception handling to turn a InteractiveAuthIncompleteError into + a 401 response. + + Normal usage is: + + @interactive_auth_handler + @defer.inlineCallbacks + def on_POST(self, request): + # ... + yield self.auth_handler.check_auth + """ + def wrapped(*args, **kwargs): + res = defer.maybeDeferred(orig, *args, **kwargs) + res.addErrback(_catch_incomplete_interactive_auth) + return res + return wrapped + + +def _catch_incomplete_interactive_auth(f): + """helper for interactive_auth_handler + + Catches InteractiveAuthIncompleteErrors and turns them into 401 responses + + Args: + f (failure.Failure): + """ + f.trap(InteractiveAuthIncompleteError) + return 401, f.value.result