From e0e7250244e51f37a7ce783ba5db6b0d113e4ff2 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Mon, 20 May 2019 17:59:20 -0700 Subject: [PATCH] Move HTTP basic auth logic from WebsiteMode to Web, so it applies to all modes --- onionshare/__init__.py | 9 +++------ onionshare/web/base_share_mode.py | 19 +++++++++++++++++++ onionshare/web/web.py | 21 ++++++++++++++++++++- onionshare/web/website_mode.py | 22 ---------------------- 4 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 onionshare/web/base_share_mode.py diff --git a/onionshare/__init__.py b/onionshare/__init__.py index a96f2fca..5df59975 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -50,8 +50,8 @@ def main(cwd=None): parser.add_argument('--auto-stop-timer', metavar='', dest='autostop_timer', default=0, help="Stop sharing after a given amount of seconds") parser.add_argument('--connect-timeout', metavar='', dest='connect_timeout', default=120, help="Give up connecting to Tor after a given amount of seconds (default: 120)") parser.add_argument('--stealth', action='store_true', dest='stealth', help="Use client authorization (advanced)") - parser.add_argument('--receive', action='store_true', dest='receive', help="Receive shares instead of sending them") - parser.add_argument('--website', action='store_true', dest='website', help=strings._("help_website")) + parser.add_argument('--receive', action='store_true', dest='receive', help="Receive files instead of sending them") + parser.add_argument('--website', action='store_true', dest='website', help="Host a static website as an onion service") parser.add_argument('--config', metavar='config', default=False, help="Custom JSON config file location (optional)") parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', help="Log OnionShare errors to stdout, and web errors to disk") parser.add_argument('filename', metavar='filename', nargs='*', help="List of files or folders to share") @@ -174,7 +174,6 @@ def main(cwd=None): if mode == 'website': # Prepare files to share - print(strings._("preparing_website")) try: web.website_mode.set_file_info(filenames) except OSError as e: @@ -219,10 +218,8 @@ def main(cwd=None): # Build the URL if common.settings.get('public_mode'): url = 'http://{0:s}'.format(app.onion_host) - elif mode == 'website': - url = 'http://onionshare:{0:s}@{1:s}'.format(web.slug, app.onion_host) else: - url = 'http://{0:s}/{1:s}'.format(app.onion_host, web.slug) + url = 'http://onionshare:{0:s}@{1:s}'.format(web.slug, app.onion_host) print('') if autostart_timer > 0: diff --git a/onionshare/web/base_share_mode.py b/onionshare/web/base_share_mode.py new file mode 100644 index 00000000..64cf3dce --- /dev/null +++ b/onionshare/web/base_share_mode.py @@ -0,0 +1,19 @@ +import os +import sys +import tempfile +import zipfile +import mimetypes +import gzip +from flask import Response, request, render_template, make_response + +from .. import strings + + +class ShareModeWeb(object): + """ + This is the base class that includes shared functionality between share mode + and website mode + """ + def __init__(self, common, web): + self.common = common + self.web = web diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 0ba8c6b3..83c441d7 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -10,6 +10,7 @@ from urllib.request import urlopen import flask from flask import Flask, request, render_template, abort, make_response, __version__ as flask_version +from flask_httpauth import HTTPBasicAuth from .. import strings @@ -53,6 +54,7 @@ class Web(object): static_folder=self.common.get_resource_path('static'), template_folder=self.common.get_resource_path('templates')) self.app.secret_key = self.common.random_string(8) + self.auth = HTTPBasicAuth() # Verbose mode? if self.common.verbose: @@ -119,8 +121,25 @@ class Web(object): def define_common_routes(self): """ - Common web app routes between sending, receiving and website modes. + Common web app routes between all modes. """ + + @self.auth.get_password + def get_pw(username): + if username == 'onionshare': + return self.slug + else: + return None + + @self.app.before_request + def conditional_auth_check(): + if not self.common.settings.get('public_mode'): + @self.auth.login_required + def _check_login(): + return None + + return _check_login() + @self.app.errorhandler(404) def page_not_found(e): """ diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 39f41b3e..354c5aa7 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -3,7 +3,6 @@ import sys import tempfile import mimetypes from flask import Response, request, render_template, make_response, send_from_directory -from flask_httpauth import HTTPBasicAuth from .. import strings @@ -17,7 +16,6 @@ class WebsiteModeWeb(object): self.common.log('WebsiteModeWeb', '__init__') self.web = web - self.auth = HTTPBasicAuth() # Dictionary mapping file paths to filenames on disk self.files = {} @@ -26,8 +24,6 @@ class WebsiteModeWeb(object): # Reset assets path self.web.app.static_folder=self.common.get_resource_path('static') - self.users = { } - self.define_routes() def define_routes(self): @@ -35,24 +31,6 @@ class WebsiteModeWeb(object): The web app routes for sharing a website """ - @self.auth.get_password - def get_pw(username): - self.users['onionshare'] = self.web.slug - - if username in self.users: - return self.users.get(username) - else: - return None - - @self.web.app.before_request - def conditional_auth_check(): - if not self.common.settings.get('public_mode'): - @self.auth.login_required - def _check_login(): - return None - - return _check_login() - @self.web.app.route('/', defaults={'path': ''}) @self.web.app.route('/') def path_public(path):