Skip to content

Instantly share code, notes, and snippets.

@look4regev
Created July 19, 2018 08:58
Show Gist options
  • Save look4regev/55613ad3719375ca42f1e3e1e2d72806 to your computer and use it in GitHub Desktop.
Save look4regev/55613ad3719375ca42f1e3e1e2d72806 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# main.py
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.security import forget
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound
from authomatic import Authomatic
from authomatic.adapters import WebObAdapter
import os
from authomatic.providers import oauth2
CONFIG = {
'google': {
'class_': oauth2.Google,
# Google is an AuthorizationProvider too.
'consumer_key': os.environ['GOOGLE_LOGIN_CONSUMER_KEY'],
'consumer_secret': os.environ['GOOGLE_LOGIN_CONSUMER_SECRET'],
'short_name': 1,
# But it is also an OAuth 2.0 provider and it needs scope.
'scope': ['https://www.googleapis.com/auth/userinfo.email'],
},
}
authomatic = Authomatic(config=CONFIG, secret='some random secret string')
@view_config(name='login')
def login(request):
# We will need the response to pass it to the WebObAdapter.
response = Response()
# Get the internal provider name URL variable.
provider_name = 'Google' # request.matchdict.get('provider_name')
# Start the login procedure.
result = authomatic.login(WebObAdapter(request, response), provider_name)
# Do not write anything to the response if there is no result!
if result:
# If there is result, the login procedure is over and we can write to
# response.
response.write('<a href="..">Home</a>')
if result.error:
# Login procedure finished with an error.
response.write(
u'<h2>Damn that error: {0}</h2>'.format(result.error.message))
elif result.user:
# Hooray, we have the user!
# OAuth 2.0 and OAuth 1.0a provide only limited user data on login,
# We need to update the user to get more info.
if not (result.user.name and result.user.id):
result.user.update()
# Welcome the user.
response.write(u'<h1>Hi {0}</h1>'.format(result.user.name))
response.write(u'<h2>Your email is: {0}</h2>'.format(result.user.email))
# Seems like we're done, but there's more we can do...
# If there are credentials (only by AuthorizationProvider),
# we can _access user's protected resources.
if result.user.credentials:
# Each provider has it's specific API.
if result.provider.name == 'google':
response.write('You are logged in with Google.<br />')
# It won't work if you don't return the response
return response
@view_config(name='logout')
def logout(request):
headers = forget(request)
return HTTPFound(location=request.resource_path(request.context, ''),
headers=headers)
@view_config(name='home')
def home(request):
return Response('''Login with <a href="login/google">Google</a>.<br />''')
if __name__ == '__main__':
config = Configurator()
config.add_route('home', '/')
config.add_view(home, route_name='home')
config.add_route('login', '/login')
config.add_view(login, route_name='login')
config.add_route('logout', '/logout')
config.add_view(logout, route_name='logout')
app = config.make_wsgi_app()
server = make_server('localhost', 3000, app)
print "Serving... http://localhost:3000"
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment