Skip to content
Snippets Groups Projects
Commit 98f27523 authored by Bruce Flynn's avatar Bruce Flynn
Browse files

tweak, rinse, repeat

parent dc3d0d48
No related branches found
No related tags found
No related merge requests found
# API Authentication Example App
## Getting Started
I highly recommend using either [Conda](http://conda.pydata.org/miniconda.html)
or [VirtualEnv](https://virtualenv.pypa.io/en/stable/).
1. Install the app and its dependencies:
```
pip install -e .
pserve --reload development.ini
```
2. Open your browser and navigate to http://localhost:6543/
3. Register
from pyramid.config import Configurator
from pyramid.security import Allow, Everyone, Authenticated
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.authentication import RemoteUserAuthenticationPolicy
class RootFactory(object):
__acl__ = [
# any member of the Science Team will have ApiAccess permission
(Allow, 'group:scienceteam', 'ApiAccess')
]
......@@ -14,17 +14,28 @@ class RootFactory(object):
class ScienceTeamAuthPolicy(object):
"""
Identifies users as members of the ScienceTeam. In theory this infomation
would be obtained from a user database or something.
"""
# any user in this list will be a member of the science team
SCIENCETEAM = ['brucef']
# IAuthenticationPolicy
# we don't need to remember or forget
remember = lambda *a: []
forget = lambda *a: []
def _noop(self, *args):
return []
remember = _noop
forget = _noop
# IAuthenticationPolicy
def unauthenticated_userid(self, request):
"""
Return userid if they are authenticated.
"""
apikey = request.params.get('apikey')
userdb = request.registry.settings['userdb']
# check registered user db to see if apikey matches
for user, data in userdb.items():
if data['apikey'] == apikey:
return user
......@@ -46,7 +57,7 @@ class ScienceTeamAuthPolicy(object):
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
# in memory user database
# in memory user database, populated by registraion page
settings['userdb'] = {}
config = Configurator(root_factory=RootFactory, settings=settings)
......@@ -61,4 +72,4 @@ def main(global_config, **settings):
config.scan()
return config.make_wsgi_app()
\ No newline at end of file
return config.make_wsgi_app()
from setuptools import setup, find_packages
requires = [
'pyramid',
'pyramid==1.7',
'pyramid_chameleon',
'pyramid_debugtoolbar',
'waitress',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment