Replace existing example for creating authentic2 plugins (fixes #5275)

This plugin implements all possible hooks by an authentic2 plugin to
show how to implement them.

It should be kept in sync with the rest of authentic2.
This commit is contained in:
Benjamin Dauvergne 2014-08-13 18:21:44 +02:00
parent ce1e7b7cbb
commit 2976ab8785
22 changed files with 269 additions and 36 deletions

View File

@ -1,7 +0,0 @@
class Plugin(object):
def get_before_urls(self):
from . import urls
return urls.urlpatterns
def get_apps(self):
return [__name__]

View File

@ -1,4 +0,0 @@
from django.db import models
class MyModel(models.Model):
a = models.TextField()

View File

@ -1,4 +0,0 @@
from django.conf.urls import patterns, url
urlpatterns = patterns('a2_test_plugin.views',
url('^test/', 'test'))

View File

@ -1,4 +0,0 @@
from django.http import HttpResponse
def test(request):
return HttpResponse('coucou')

View File

@ -1,17 +0,0 @@
#!/usr/bin/python
from setuptools import setup, find_packages
import os
setup(name='authentic2-test-plugin',
version='1.0',
license='AGPLv3',
description='Authentic2 Test Plugin',
author="Entr'ouvert",
author_email="info@entrouvert.com",
packages=find_packages(os.path.dirname(__file__) or '.'),
entry_points={
'authentic2.plugin': [
'test-plugin = a2_test_plugin:Plugin',
],
},
)

View File

@ -0,0 +1,2 @@
authentic2-plugin-template is entirely under the copyright of Entr'ouvert and
distributed under the license AGPLv3 or later.

View File

@ -0,0 +1,3 @@
include COPYING
recursive-include src/authentic2_plugin_template/templates *.html
recursive-include src/authentic2_plugin_template/static *.js *.css *.png

View File

@ -0,0 +1,20 @@
** THIS IS A TEMPLATE PROJECT **
To rename it to your taste:
$ ./adapt.sh
** THIS IS A TEMPLATE PROJECT **
Authentic2 Plugin Template
==========================
Install
-------
You just have to install the package in your virtualenv and relaunch, it will
be automatically loaded by authentic2.
Settings
--------
** DESCRIBE CUSTOM SETTINGS HERE **

View File

@ -0,0 +1,38 @@
#!/bin/sh
set -x
echo "Give project name (it must match regexp ^[a-z][a-z0-9-]+$ )"
read PROJECT_NAME
if ! echo $PROJECT_NAME | grep -q '^[a-z][a-z0-9-]\+$'; then
echo "Invalid project name:" $PROJECT_NAME
exit 1
fi
UPPER_UNDERSCORED=`echo $PROJECT_NAME | tr a-z A-Z | sed 's/-/_/g'`
LOWER_UNDERSCORED=`echo $PROJECT_NAME | sed 's/-/_/g'`
TITLECASE=`echo $PROJECT_NAME | sed 's/-/ /g;s/.*/\L&/; s/[a-z]*/\u&/g'`
echo Project name: $PROJECT_NAME
echo Uppercase underscored: $UPPER_UNDERSCORED
echo Lowercase underscored: $LOWER_UNDERSCORED
echo Titlecase: $TITLECASE
if [ -d .git ]; then
MV='git mv'
else
MV=mv
fi
sed -i \
-e "s/authentic2_plugin_template/$LOWER_UNDERSCORED/g" \
-e "s/authentic2-plugin-template/$PROJECT_NAME/g" \
-e "s/A2_TEMPLATE_/A2_$UPPER_UNDERSCORED_/g" \
-e "s/Authentic2 Plugin Template/$TITLECASE/g" \
setup.py src/*/*.py README COPYING MANIFEST.in
$MV src/authentic2_plugin_template/static/authentic2_plugin_template \
src/authentic2_plugin_template/static/$LOWER_UNDERSCORED
$MV src/authentic2_plugin_template/templates/authentic2_plugin_template \
src/authentic2_plugin_template/templates/$LOWER_UNDERSCORED
$MV src/authentic2_plugin_template src/$LOWER_UNDERSCORED

View File

@ -0,0 +1,65 @@
#!/usr/bin/python
from setuptools import setup, find_packages
import os
def get_version():
import glob
import re
import os
version = None
for d in glob.glob('src/*'):
if not os.path.isdir(d):
continue
module_file = os.path.join(d, '__init__.py')
if not os.path.exists(module_file):
continue
for v in re.findall("""__version__ *= *['"](.*)['"]""",
open(module_file).read()):
assert version is None
version = v
if version:
break
assert version is not None
if os.path.exists('.git'):
import subprocess
p = subprocess.Popen(['git','describe','--dirty','--match=v*'],
stdout=subprocess.PIPE)
result = p.communicate()[0]
assert p.returncode == 0, 'git returned non-zero'
new_version = result.split()[0][1:]
assert new_version.split('-')[0] == version, '__version__ must match the last git annotated tag'
version = new_version.replace('-', '.')
return version
README = file(os.path.join(
os.path.dirname(__file__),
'README')).read()
setup(name='authentic2-plugin-template',
version=get_version(),
license='AGPLv3',
description='Authentic2 Plugin Template',
long_description=README,
author="Entr'ouvert",
author_email="info@entrouvert.com",
packages=find_packages('src'),
package_dir={
'': 'src',
},
package_data={
'authentic2_plugin_template': [
'templates/authentic2_plugin_template/*.html',
'static/authentic2_plugin_template/js/*.js',
'static/authentic2_plugin_template/css/*.css',
'static/authentic2_plugin_template/img/*.png',
],
},
install_requires=[
],
entry_points={
'authentic2.plugin': [
'authentic2-plugin-template= authentic2_plugin_template:Plugin',
],
},
)

View File

@ -0,0 +1,62 @@
__version__ = '1.0.0'
class Plugin(object):
def get_before_urls(self):
from . import urls
return urls.urlpatterns
def get_after_urls(self):
return []
def get_apps(self):
return [__name__]
def get_before_middleware(self):
return []
def get_after_middleware(self):
return []
def get_authentication_backends(self):
return []
def get_auth_frontends(self):
return []
def get_idp_backends(self):
return []
def get_admin_modules(self):
from . import dashboard
return dashboard.get_admin_modules()
def service_list(self, request):
'''For IdP plugins this method add links to the user homepage.
It must return a list of authentic2.utils.Service objects, each
object has a name and can have an url and some actions.
Service(name=name[, url=url[, actions=actions]])
Actions are a list of tuples, whose parts are
- first the name of the action,
- the HTTP method for calling the action,
- the URL for calling the action,
- the paramters to pass to this URL as a sequence of key-value tuples.
'''
return []
def logout_list(self, request):
'''For IdP or SP plugins this method add actions to logout from remote
IdP or SP.
It must returns a list of HTML fragments, each fragment is
responsible for calling the view doing the logout. Views are usually
called using <img/> or <iframge/> tags and finally redirect to an
icon indicating success or failure for the logout.
Authentic2 provide two such icons through the following URLs:
- os.path.join(settings.STATIC_URL, 'authentic2/img/ok.png')
- os.path.join(settings.STATIC_URL, 'authentic2/img/ok.png')
'''
return []

View File

@ -0,0 +1,5 @@
from django.contrib import admin
from . import models
# registrer your admin editable models here using admin.register

View File

@ -0,0 +1,23 @@
class AppSettings(object):
__DEFAULTS = {
'ENABLE': True,
}
def __init__(self, prefix):
self.prefix = prefix
def _setting(self, name, dflt):
from django.conf import settings
return getattr(settings, self.prefix+name, dflt)
def __getattr__(self, name):
if name not in self.__DEFAULTS:
raise AttributeError(name)
return self._setting(name, self.__DEFAULTS[name])
# Ugly? Guido recommends this himself ...
# http://mail.python.org/pipermail/python-ideas/2012-May/014969.html
import sys
app_settings = AppSettings('A2_PLUGIN_TEMPLATE_')
app_settings.__name__ = __name__
sys.modules[__name__] = app_settings

View File

@ -0,0 +1,11 @@
from django.utils.translation import ugettext_lazy as _
from admin_tools.dashboard import modules
def get_admin_modules():
'''Show Client model in authentic2 admin'''
model_list = modules.ModelList(_('Authentic2 Plugin Template'),
models=('authentic2_plugin_template.models.*',))
return (model_list,)

View File

@ -0,0 +1,14 @@
import functools
from django.http import Http404
from . import app_settings
def plugin_enabled(view):
'''If plugin is not enabled, return 404'''
@functools.wraps(view)
def wrapper(*args, **kwargs):
if not app_settings.ENABLED:
raise Http404
return view(*args, **kwargs)
return wrapper

View File

@ -0,0 +1,3 @@
from django import forms

View File

@ -0,0 +1,4 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
# put your models here

View File

@ -0,0 +1 @@
{% comment %}placeholder{% endcomment %}

View File

@ -0,0 +1,8 @@
from django.conf.urls import patterns, url
from .views import index
urlpatterns = patterns('',
url('^authentic2_plugin_template/$', index,
name='authentic2-plugin-template-index'),
)

View File

@ -0,0 +1,10 @@
from django.shortcuts import render
from . import decorators
__ALL_ = [ 'sso' ]
@decorators.plugin_enabled
def index(request):
return render(request, 'authentic2_plugin_template/index.html')