0

I'm setting up a Flask server, and want to use a Twisted ReverseProxyResource to proxy over another local server. In Flask, I have a current_user.is_authenticated boolean which I use to protect pages. How can I lock the ReverseProxyResource using this variable, so that it cannot be accessed when a user is logged out? The twisted.web.guard module requires me to set up another HTTP authentication system entirely, and the library doesn't seem to have any other built-in solution.

I've set up some demo code, (based off a previous question) attempting to place the Flask server inside of the Twisted reactor. I'm using the ReverseProxyResource to allow for two-way communication with a Shellinabox server on port 4200.

from flask import Flask
from twisted.internet import reactor
from twisted.web.proxy import ReverseProxyResource
from twisted.web.resource import Resource
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource

app = Flask(__name__)


@app.route('/example')
def index():
    return 'Flask within Twisted?'

flask_site = WSGIResource(reactor, reactor.getThreadPool(), app)

root = Resource()

site_example = ReverseProxyResource('localhost', 4200, b'')
root.putChild(b'ssh', site_example)


reactor.listenTCP(8081, Site(root))
reactor.run()

I'd be okay switching to another reverse proxy, but only Twisted has worked so far. I'm also not wanting to switch to the Klein framework due to what I already have established in Flask.

Jordan Mann
  • 402
  • 6
  • 16
  • Why can't you glue twisted.web.guard into whatever it is you use to initialize `is_authenticated` with Flask? – Jean-Paul Calderone Mar 23 '19 at 15:38
  • I'm modifying a [Flask app](https://github.com/duo-labs/py_webauthn) which uses the `login_user` method. How exactly would I "glue" `guard` into it? – Jordan Mann Mar 23 '19 at 22:23
  • I guess a good step would be to more completely understand how authentication works in your Flask app. Then you might be able to figure out how to perform that same authentication for other Twisted resources, or at least ask a more pointed question. – Jean-Paul Calderone Mar 24 '19 at 18:37

0 Answers0