Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

GitHub authentication #25

Open
wants to merge 7 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
6 changes: 5 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def reset_database(request, app):
engine.dispose()
subprocess.check_call('dropdb %s' % db_name, shell=True)

subprocess.check_call('createdb -E utf-8 %s' % db_name, shell=True)
subprocess.check_call('createdb -E utf-8 %s%s' %
(' -T %s ' % (os.environ['POSTGRESQL_TEMPLATE'])
if 'POSTGRESQL_TEMPLATE' in os.environ else '',
db_name),
shell=True)

command.upgrade(ALEMBIC_CONFIG, 'head')
return lambda: reset_database(request, app)
39 changes: 39 additions & 0 deletions freight/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import absolute_import

from .exceptions import AccessDeniedError, ProviderConfigurationError # noqa
from .providers import GoogleOAuth2Provider, GitHubOAuth2Provider


class Auth(object):
"""Flask extension for OAuth 2.0 authentication.

Assigns the configured provider to the ``auth_provider`` key of
``app.state`` when initialized.
"""

def __init__(self, app=None):
if app is not None:
self.init_app(app)

def init_app(self, app):
# For compatibility with previous versions of Freight, default to using
# Google as the authentication backend.
backend = app.config.get('AUTH_BACKEND')
if not backend:
backend = 'google'

# Resolve the provider setup function.
try:
provider_cls = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good, and if we want to extend it in the future we can just have AUTH_BACKEND support a full class path or similar

'google': GoogleOAuth2Provider,
'github': GitHubOAuth2Provider,
}[backend]
except KeyError:
raise RuntimeError('invalid authentication backend: %s' %
(backend))

# Set up the provider.
if not hasattr(app, 'state'):
app.state = {}

app.state['auth_provider'] = provider_cls.from_app_config(app.config)
18 changes: 18 additions & 0 deletions freight/auth/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import absolute_import


class AccessDeniedError(Exception):
"""Access denied.

Raised if authentication with the backend succeeded but the user is not
allowed access by configuration.
"""

pass


class ProviderConfigurationError(Exception):
"""Provider configuration error.
"""

pass
Loading