From 793ab6b24b2f2f4a976fa81ea4c127a191367e59 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Fri, 28 May 2021 21:03:03 -0400 Subject: [PATCH] app: add a redirect endpoint Clients can use this in situations where a browser may prompt the user to take action prior to sending the request. After the user accepts, Moonraker will redirect the user to the url provided in the request. SIgned-off-by: Eric Callahan --- moonraker/app.py | 18 +++++++++++++++++- moonraker/components/authorization.py | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/moonraker/app.py b/moonraker/app.py index ee314ac..9d0e1a7 100644 --- a/moonraker/app.py +++ b/moonraker/app.py @@ -155,7 +155,8 @@ class MoonrakerApp: self.mutable_router = MutableRouter(self) app_handlers: List[Any] = [ (AnyMatches(), self.mutable_router), - (r"/websocket", WebSocket)] + (r"/websocket", WebSocket), + (r"/server/redirect", RedirectHandler)] self.app = tornado.web.Application(app_handlers, **app_args) self.get_handler_delegate = self.app.get_handler_delegate @@ -793,3 +794,18 @@ class AuthorizedErrorHandler(AuthorizedRequestHandler): err['traceback'] = "\n".join( traceback.format_exception(*kwargs['exc_info'])) self.finish({'error': err}) + +class RedirectHandler(AuthorizedRequestHandler): + def get(self, *args, **kwargs) -> None: + url: Optional[str] = self.get_argument('url', None) + if url is None: + try: + body_args: Dict[str, Any] = json.loads(self.request.body) + except json.JSONDecodeError: + body_args = {} + if 'url' not in body_args: + raise tornado.web.HTTPError( + 400, "No url argument provided") + url = body_args['url'] + assert url is not None + self.redirect(url) diff --git a/moonraker/components/authorization.py b/moonraker/components/authorization.py index 6c8b22f..0f62d0e 100644 --- a/moonraker/components/authorization.py +++ b/moonraker/components/authorization.py @@ -154,6 +154,7 @@ class Authorization: self.prune_handler.start() # Register Authorization Endpoints + self.permitted_paths.add("/server/redirect") self.permitted_paths.add("/access/login") self.permitted_paths.add("/access/refresh_jwt") self.server.register_endpoint(