Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
making the example closer to plugin already published on PyPI
  • Loading branch information
frgomes committed Oct 30, 2013
commit d2a4875ae53b94651d0b2d01ddd1269e2f4fa59a
39 changes: 28 additions & 11 deletions examples/kotti_velruse/README.rst
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
Install Kotti
For the impatient
-----------------

1. Simply run script run-server.sh

2. Navigate to page /login like the example below:

$ firefox http://localhost:6543/login


Configuration
-------------

$ python setup.py develop
1. Please adjust variable *realm* in development.ini.

2. Several providers need to be configured according to your affiliation
keys with providers like Google OAuth2, Twitter, Facebook, etc.

Start the server
----------------
Several providers work out of the box, like Google Hybrid, Yahoo and most
of OpenID providers.

$ run-server.sh

About this example
------------------

Navigate
--------
This example evolved to a proper plugin, which is available from PyPI at
https://pypi.python.org/pypi/kotti_velruse

$ firefox http://localhost:6543/login

Dependencies
------------

Notes
-----
This example depends on a modified versions of velruse and openid-selector:

This example program utilizes openid-selector, which is cloned from github when first start the server.
* velruse: https://pypi.python.org/pypi/rgomes_velruse

* openid-selector: https://pypi.python.org/pypi/openid-selector

Sources for these changed sources are available at:

* velruse: https://github.com/frgomes/velruse/tree/feature.kotti_auth

* openid-selector: https://github.com/frgomes/velruse
40 changes: 26 additions & 14 deletions examples/kotti_velruse/development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ kotti.site_title = My Kotti site
kotti.secret = qwerty

kotti.configurators = kotti_tinymce.kotti_configure

kotti.includes = kotti_velruse
kotti_velruse.kotti_configure

### --------------------------------------------------------------------------

Expand All @@ -79,11 +78,18 @@ kotti.includes = kotti_velruse
#
# NOTE: these configurations must be inside [app:kotti]
#
# Icons copied from:
# https://github.com/diversen/openid-selector
###

realm=http://apps.xkbm.net
#---
# Please adjust variable REALM
#
# Make sure that:
#
# 1. your browser is able to resolve the FQDN
# 2. your Kotti server is able to resolve the FQDN
#
#---
realm=http://www.example.com

endpoint = %(realm)s:6543/logged_in
store = memory
Expand All @@ -101,10 +107,10 @@ provider.openid.realm=%(realm)s
provider.openid.store=openid.store.memstore:MemoryStore

# Google (this an alias to Google Hybrid, for backward compatibility)
#provider.google.realm=%(realm)s
#provider.google.consumer_key=CHANGE-ME
#provider.google.consumer_secret=CHANGE-ME
#provider.google.scope=CHANGE-ME
provider.google.realm=%(realm)s
provider.google.consumer_key=CHANGE-ME
provider.google.consumer_secret=CHANGE-ME
provider.google.scope=CHANGE-ME

# Google Hybrid
#provider.google_hybrid.realm=%(realm)s
Expand All @@ -118,9 +124,9 @@ provider.google_oauth2.consumer_secret=CHANGE-ME
provider.google_oauth2.scope=CHANGE-ME

# Yahoo
#provider.yahoo.realm=%(realm)s
#provider.yahoo.consumer_key=CHANGE-ME
#provider.yahoo.consumer_secret=CHANGE-ME
provider.yahoo.realm=%(realm)s
provider.yahoo.consumer_key=CHANGE-ME
provider.yahoo.consumer_secret=CHANGE-ME

# Live
provider.live.client_id=CHANGE-ME
Expand All @@ -137,21 +143,27 @@ provider.facebook.app_id=CHANGE-ME
provider.facebook.app_secret=CHANGE-ME
provider.facebook.consumer_key=CHANGE-ME
provider.facebook.consumer_secret=CHANGE-ME
provider.facebook.scope=email,publish_stream,read_stream,create_event,offline_access

# LinkedIn
provider.linkedin.consumer_key=CHANGE-ME
provider.linkedin.consumer_secret=CHANGE-ME

# Github
provider.github.app_id=CHANGE-ME
provider.github.app_secret=CHANGE-ME
provider.github.consumer_key=CHANGE-ME
provider.github.consumer_secret=CHANGE-ME
provider.github.scope=CHANGE-ME

# BitBucket
provider.bitbucket.consumer_key=CHANGE-ME
provider.bitbucket.consumer_secret=CHANGE-ME

# MailRU
provider.mailru.app_id=CHANGE-ME
provider.mailru.app_secret=CHANGE-ME
provider.mailru.consumer_key=CHANGE-ME
provider.mailru.consumer_secret=CHANGE-ME

### --------------------------------------------------------------------------


Expand Down
14 changes: 7 additions & 7 deletions examples/kotti_velruse/kotti_velruse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from pyramid.config import Configurator

import velruse.app
import views
from pyramid.i18n import TranslationStringFactory


log = __import__('logging').getLogger(__name__)


def includeme(config):
velruse.app.includeme(config)
views.includeme(config)
_ = TranslationStringFactory('kotti_velruse')


def kotti_configure(settings):
settings['pyramid.includes'] += ' velruse.app'
settings['pyramid.includes'] += ' kotti_velruse.views'
116 changes: 65 additions & 51 deletions examples/kotti_velruse/kotti_velruse/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from velruse.api import login_url
from velruse.app import find_providers


log = __import__('logging').getLogger(__name__)
from kotti_velruse import log, _


def includeme(config):
Expand All @@ -28,80 +27,95 @@ def includeme(config):
config.add_route('logged_in', '/logged_in')
config.add_route('logout', '/logout')

config.add_static_view(name='static', path='kotti_velruse:static')

####################################################################################
# This route named '' MUST BE THE LAST ONE in the global list of routes.
# It means that plugin kotti_velruse MUST BE THE LAST ONE in the list of includes.
#
# It's definitely a bad idea to employ a route named ''.
# But, in order to avoid this, we would have to change openid-selector too much :(
# ... which is outside of our requirements for this demo.
####################################################################################
config.add_static_view(name='', path='kotti_velruse:openid-selector')

try:
import openid_selector
log.info('openid_selector loaded successfully')
config.add_static_view(name='js', path='openid_selector:/js')
config.add_static_view(name='css', path='openid_selector:/css')
config.add_static_view(name='images', path='openid_selector:/images')
except Exception as e:
log.error(e)
raise e
log.info('kotti_velruse views are configured.')


def login(request):
settings = request.registry.settings
project = settings['kotti.site_title']
return {
'project' : project,
'login_url': request.route_url('login_'),
}
try:
#TODO:: before_kotti_velruse_loggedin(request)
return {
'project' : settings['kotti.site_title'],
'login_url': request.route_url('login_'),
}
except Exception as e:
log.error(e.message)
raise HTTPNotFound(e.message).exception


def login_(request):
####################################################################################
# Let's clarify the difference between "provider" and "method":
#
# * Conceptually, methods can be understood pretty much like protocols or transports.
# So, methods would be for example: OpenID, OAuth2, CAS, LDAP.
# * A provider is simply an entity, like Verisign, Google, Yahoo, Launchpad and
# hundreds of other entities which employ popular methods like OpenID and OAuth2.
# * In particular, certain entities implement their own methods (or protocols) or
# they eventually offer several authentication methods. For this reason, there are
# specific methods for "yahoo", "tweeter", "google_hybrid", "google_oauth2", etc.
#
# For the SAKE OF SIMPLICITY we arbitrarity consider providers and methods simply
# as entities in this function in particular.
####################################################################################
provider=request.params['method']
######################################################################################
# #
# Let's clarify the difference between "provider" and "method" in this function: #
# #
# * Conceptually, [authentication] methods can be understood pretty much like #
# protocols or transports. So, methods would be for example: OpenID, OAuth2 and #
# other authentication protocols supported by Velruse. #
# #
# * A provider is simply an entity, like Google, Yahoo, Twitter, Facebook, Verisign, #
# Github, Launchpad and hundreds of other entities which employ authentication #
# methods like OpenID, OAuth2 and others supported by Velruse. #
# #
# * In particular, certain entities implement their own authentication methods or #
# they eventually offer several authentication methods. For this reason, there are #
# specific methods for "yahoo", "tweeter", "google_hybrid", "google_oauth2", etc. #
# #
######################################################################################

provider = request.params['provider']
method = request.params['method']

settings = request.registry.settings
if not provider in find_providers(settings):
raise HTTPNotFound('Provider "{}" is not configured'.format(provider)).exception
if not method in find_providers(settings):
raise HTTPNotFound('Provider/method {}/{} is not configured'.format(provider, method)).exception

velruse_url = login_url(request, provider)
velruse_url = login_url(request, method)

payload = dict(request.params)
if 'yahoo' == provider: payload['oauth'] = 'true'
if 'facebook' == provider: payload['scope'] = 'email,publish_stream,read_stream,create_event,offline_access'
if 'openid' == provider: payload['use_popup'] = 'false'
if 'yahoo' == method: payload['oauth'] = 'true'
if 'openid' == method: payload['use_popup'] = 'false'
payload['format'] = 'json'
del payload['provider']
del payload['method']

redirect = Request.blank(velruse_url, POST=payload)
try:
response = request.invoke_subrequest( redirect )
return response
except:
message = 'Provider "{}" is probably misconfigured'.format(provider)
except Exception as e:
log.error(e.message)
message = _(u'Provider/method: {}/{} :: {}').format(provider, method, e.message)
raise HTTPNotFound(message).exception



def logged_in(request):
token = request.params['token']
storage = request.registry.velruse_store
try:
return storage.retrieve(token)
except KeyError:
message = 'invalid token "{}"'.format(token)
log.error(message)
return { 'error' : message }
json = storage.retrieve(token)
return json
except Exception as e:
log.error(e.message)
raise HTTPNotFound(e.message).exception


def logout(request):
from pyramid.security import forget
request.session.invalidate()
request.session.flash('Session logoff.')
headers = forget(request)
return HTTPFound(location=request.route_url('login'), headers=headers)
try:
request.session.invalidate()
request.session.flash( _(u'Session logged out.') )
headers = forget(request)
return HTTPFound(location=request.application_url, headers=headers)
except Exception as e:
log.error(e.message)
raise HTTPNotFound(e.message).exception
36 changes: 22 additions & 14 deletions examples/kotti_velruse/run-server.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#/bin/bash


# give a little push to Kotti installation
pip install -r https://raw.github.com/Kotti/Kotti/0.9.2/requirements.txt $PIP_OPTIONS

# installs Kotti and kotti_velruse
python setup.py develop

# uninstall velruse cos rgomes-velruse replaces it for the time being
pip uninstall velruse << EOF
y
EOF

# make sure proxy settings are ignored
`env | fgrep -i _proxy | cut -d= -f1 | xargs echo unset`

if [ ! -d kotti_velruse/openid-selector ] ;then
if [ -d ~/sources/frgomes/openid-selector/master/openid-selector ] ;then
ln -s ~/sources/frgomes/openid-selector/master/openid-selector kotti_velruse/openid-selector
else
which git
if [ $? -ne 0 ] ; then
sudo apt-get install git -y
fi
pushd kotti_velruse
git clone https://github.com/frgomes/openid-selector.git
popd
fi
fi

# start server
echo .
echo .
echo '*************************************************'
echo '* *'
echo '* Starting the server... *'
echo '* *'
echo '* Please visit context /login when it is ready. *'
echo '* *'
echo '*************************************************'
pserve development.ini --reload
4 changes: 1 addition & 3 deletions examples/kotti_velruse/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

requires = [
'Kotti',
'kotti_velruse',
]

setup(name='kotti_velruse',
Expand All @@ -29,7 +30,4 @@
include_package_data=True,
zip_safe=False,
install_requires=requires,
entry_points="""
# -*- Entry points: -*-
""",
)