Skip to content

Instantly share code, notes, and snippets.

@scott2b
Last active January 28, 2016 11:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scott2b/7539594 to your computer and use it in GitHub Desktop.
Save scott2b/7539594 to your computer and use it in GitHub Desktop.
login override for whitelisting/blacklisting and new-user forwarding for use with pyramid_persona
from pyramid.view import view_config
from pyramid.security import remember
from pyramid.security import authenticated_userid
from pyramid_persona.views import verify_login
USE_WHITELIST = False
WHITELIST_REJECT_MESSAGE = 'Sorry, you are not authorized to access this site.'
WHITELIST_REJECT_REDIRECT = '/'
USE_BLACKLIST = False
BLACKLIST_REJECT_MESSAGE = 'Sorry, you are not authorized to access this site.'
BLACKLIST_REJECT_REDIRECT = '/'
NEW_USER_REDIRECT = None
REDIRECT = '/'
def user_exists(email):
"""TODO: Implement me"""
return False
def create_profile(email):
"""TODO: Implement me"""
pass
def email_whitelist():
return []
def email_blacklist():
return []
@view_config(route_name='new_user', renderer='templates/new_user.jinja2')
def new_user_view(request):
email = authenticated_userid(request)
return {
'email': email
}
@view_config(route_name='login', check_csrf=True, renderer='json')
def login(request):
email = verify_login(request)
if USE_WHITELIST and email not in email_whitelist():
request.session.flash(WHITELIST_REJECT_MESSAGE)
return {
'redirect': '/',
'success': False
}
if USE_BLACKLIST and email in email_blacklist():
request.session.flash(BLACKLIST_REJECT_MESSAGE)
return {
'redirect': '/',
'success': False
}
request.response.headers.extend(remember(request, email))
if not user_exists(email):
create_profile(email)
if NEW_USER_REDIRECT is not None:
return {
'redirect': '/new-user',
'success': True
}
return {
'redirect': REDIRECT,
'success': True
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment