Add url prefix for reverse proxies (#106)

This commit is contained in:
vin01 2019-08-09 21:07:49 +00:00 committed by Jon Parise
parent 054c61ae89
commit 5747ee2d14
3 changed files with 10 additions and 0 deletions

View file

@ -82,6 +82,8 @@ need to change this.
``NO_SSL``: if you are not using SSL. ``NO_SSL``: if you are not using SSL.
``URL_PREFIX``: useful when running snappass behind a reverse proxy like `nginx`. Example: ``"/some/path/"``, Defaults to ``None``
``REDIS_HOST``: this should be set by Redis, but you can override it if you want. Defaults to ``"localhost"`` ``REDIS_HOST``: this should be set by Redis, but you can override it if you want. Defaults to ``"localhost"``
``REDIS_PORT``: is the port redis is serving on, defaults to 6379 ``REDIS_PORT``: is the port redis is serving on, defaults to 6379

View file

@ -12,6 +12,7 @@ from werkzeug.urls import url_quote_plus
from werkzeug.urls import url_unquote_plus from werkzeug.urls import url_unquote_plus
NO_SSL = os.environ.get('NO_SSL', False) NO_SSL = os.environ.get('NO_SSL', False)
URL_PREFIX = os.environ.get('URL_PREFIX', None)
TOKEN_SEPARATOR = '~' TOKEN_SEPARATOR = '~'
@ -165,6 +166,8 @@ def handle_password():
base_url = request.url_root base_url = request.url_root
else: else:
base_url = request.url_root.replace("http://", "https://") base_url = request.url_root.replace("http://", "https://")
if URL_PREFIX:
base_url = base_url + URL_PREFIX.strip("/") + "/"
link = base_url + url_quote_plus(token) link = base_url + url_quote_plus(token)
return render_template('confirm.html', password_link=link) return render_template('confirm.html', password_link=link)

View file

@ -115,6 +115,11 @@ class SnapPassRoutesTestCase(TestCase):
rv = self.app.post('/{0}'.format(key)) rv = self.app.post('/{0}'.format(key))
self.assertIn(password, rv.get_data(as_text=True)) self.assertIn(password, rv.get_data(as_text=True))
def test_url_prefix(self):
password = "I like novelty kitten statues!"
snappass.URL_PREFIX="/test/prefix"
rv = self.app.post('/', data={'password': password, 'ttl': 'hour'})
self.assertIn("localhost/test/prefix/", rv.get_data(as_text=True))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()