Move HTTP basic auth logic from WebsiteMode to Web, so it applies to all modes

This commit is contained in:
Micah Lee 2019-05-20 17:59:20 -07:00
parent 915ff0f4f3
commit 2a50bbc3bc
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
4 changed files with 42 additions and 29 deletions

View File

@ -50,8 +50,8 @@ def main(cwd=None):
parser.add_argument('--auto-stop-timer', metavar='<int>', dest='autostop_timer', default=0, help="Stop sharing after a given amount of seconds")
parser.add_argument('--connect-timeout', metavar='<int>', 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:

View File

@ -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

View File

@ -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):
"""

View File

@ -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('/<path:path>')
def path_public(path):