Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement basic authentication #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ndscheduler/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,12 @@
logging.getLogger().setLevel(logging.INFO)


# To enable basic authentication, define the 'user' and 'pass'
BASIC_AUTH_CONFIG = {
'user': '',
'pass': '',
'realm': 'Nextdoor Scheduler'
}

# Packages that contains job classes, e.g., simple_scheduler.jobs
JOB_CLASS_PACKAGES = []
33 changes: 32 additions & 1 deletion ndscheduler/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
SchedulerServer.run()
"""

import base64
import logging
import signal
import sys
Expand All @@ -21,6 +22,36 @@
logger = logging.getLogger(__name__)


def require_basic_auth(handler_class, config=None):
config = config or dict()
config.setdefault('user', '')
config.setdefault('pass', '')
config.setdefault('realm', 'Next Scheduler')

if config['user'] and config['pass']:
def wrap_execute(handler_execute):
def check_auth(handler):
auth_header = handler.request.headers.get('Authorization')
if auth_header and auth_header.startswith('Basic '):
auth_decoded = base64.decodestring(auth_header[6:])
if '%s:%s' % (config['user'], config['pass']) == auth_decoded:
return True
handler.set_status(401)
handler.set_header('WWW-Authenticate', 'Basic realm=%s' % config['realm'])
handler._transforms = []
handler.finish()
return False

def _execute(self, transforms, *args, **kwargs):
if not check_auth(self):
return False
return handler_execute(self, transforms, *args, **kwargs)
return _execute

handler_class._execute = wrap_execute(handler_class._execute)
return handler_class


class SchedulerServer:

VERSION = 'v1'
Expand All @@ -41,7 +72,7 @@ def __init__(self, scheduler_instance):
# Setup server
URLS = [
# Index page
(r'/', index.Handler),
(r'/', require_basic_auth(index.Handler, settings.BASIC_AUTH_CONFIG)),

# APIs
(r'/api/%s/jobs' % self.VERSION, jobs.Handler),
Expand Down