Larpe now manage custom output filters in plugin
* conf/filters: folder with filter templates * conf/filters/output_ciril_net_rh.py: output filter for Ciril which control if the user is authenticate in larpe * larpe/Defaults.py: remove useless OUTPUT_FILTER_BASE parameter * larpe/admin/hosts.ptl: manage multi filters with plugins * larpe/plugins/site_authentication/ciril_net_rh.py: load Ciril filter * setup.py: cosmetic git-svn-id: svn+ssh://labs.libre-entreprise.org/svnroot/larpe@489 3ed937ae-f919-0410-9a43-8e6f19e4ba6e
This commit is contained in:
parent
555be63708
commit
3d0cfe2509
|
@ -0,0 +1,63 @@
|
|||
import re
|
||||
import os
|
||||
import pickle
|
||||
|
||||
from larpe import sessions
|
||||
from mod_python import Cookie
|
||||
|
||||
def is_auth_ok(req):
|
||||
""" Test if you are authenticate on the Larpe server """
|
||||
cookies = Cookie.get_cookies(req, Cookie.MarshalCookie, secret='secret007')
|
||||
sessions_dir = os.path.join("%(larpe_dir)s", "sessions")
|
||||
for name, cookie in cookies.iteritems():
|
||||
value = cookie.value.replace('"', '')
|
||||
if "larpe-" in name and value in os.listdir(sessions_dir):
|
||||
try:
|
||||
file = open(os.path.join(sessions_dir, value), "rb")
|
||||
session = pickle.load(file)
|
||||
if not session.users or not session.id:
|
||||
return False
|
||||
return True
|
||||
except Exception, err:
|
||||
return False
|
||||
return False
|
||||
|
||||
|
||||
def filter_page(filter, page):
|
||||
r = re.compile(r"""(<a.*?href=["']).*?(["'].*?>D.*?connexion</a>)""")
|
||||
page = r.sub(r"""\1%(logout_url)s\2""", page)
|
||||
return page
|
||||
|
||||
def outputfilter(filter):
|
||||
""" Apache called this function by default """
|
||||
if not re.search("^/liberty/.*", filter.req.uri) and not is_auth_ok(filter.req):
|
||||
filter.write('<meta http-equiv="refresh" content="0; url=%(login_url)s" />')
|
||||
filter.close()
|
||||
return
|
||||
|
||||
if filter.req.content_type is not None:
|
||||
is_html = re.search('text/html', filter.req.content_type)
|
||||
if filter.req.content_type is None or not is_html:
|
||||
filter.pass_on()
|
||||
else:
|
||||
if not hasattr(filter.req, 'temp_doc'):
|
||||
# Create a new attribute to hold the document
|
||||
filter.req.temp_doc = []
|
||||
# If content-length ended up wrong, Gecko browsers truncated data
|
||||
if 'Content-Length' in filter.req.headers_out:
|
||||
del filter.req.headers_out['Content-Length']
|
||||
|
||||
temp_doc = filter.req.temp_doc
|
||||
s = filter.read()
|
||||
# Could get '' at any point, but only get None at end
|
||||
while s:
|
||||
temp_doc.append(s)
|
||||
s = filter.read()
|
||||
|
||||
# The end
|
||||
if s is None:
|
||||
page = ''.join(temp_doc)
|
||||
page = filter_page(filter, page)
|
||||
filter.write(page)
|
||||
filter.close()
|
||||
|
|
@ -1,5 +1,10 @@
|
|||
import re
|
||||
|
||||
def filter_page(filter, page):
|
||||
current_form = re.compile('<form [^>]*?action="%(auth_form_action)s".*?>.*?</form>', re.DOTALL)
|
||||
page = current_form.sub('<form method="post" action="/liberty/%(name)s/login"><input type="submit" value="Connexion" /></form>', page)
|
||||
return page
|
||||
|
||||
def outputfilter(filter):
|
||||
# Only filter html code
|
||||
if filter.req.content_type is not None:
|
|
@ -7,4 +7,3 @@ WEB_ROOT = 'larpe'
|
|||
APACHE_MAIN_VHOST = '/etc/apache2/sites-available/apache2-vhost-larpe'
|
||||
APACHE_VHOST_COMMON = '/etc/larpe/apache2-vhost-larpe-common'
|
||||
APACHE_RELOAD = '/usr/sbin/larpe-reload-apache2'
|
||||
OUTPUT_FILTER_BASE = '/usr/share/larpe/output_filter_base.py'
|
||||
|
|
|
@ -16,11 +16,11 @@ from larpe import site_authentication
|
|||
from larpe import errors
|
||||
from larpe import misc
|
||||
from larpe.hosts import Host
|
||||
from larpe.admin.apache import Location
|
||||
from larpe.admin.liberty_utils import *
|
||||
#from larpe.filter import filter_misc
|
||||
from larpe.admin.apache import write_apache2_vhosts
|
||||
from larpe.admin.forms_prefill import FormsDirectory
|
||||
from larpe.Defaults import OUTPUT_FILTER_BASE
|
||||
from larpe.Defaults import DATA_DIR
|
||||
from larpe.plugins import site_authentication_plugins
|
||||
|
||||
def check_basic_configuration(form):
|
||||
|
@ -795,32 +795,33 @@ POST request. You can desactivate some or all of them, or change their value.'''
|
|||
self.host.apache_python_paths = []
|
||||
self.host.apache_output_python_filters = []
|
||||
site_auth = site_authentication.get_site_authentication(self.host)
|
||||
filters = site_auth.get_filters()
|
||||
output_filters = site_auth.output_filters
|
||||
replace_login_form = self.host.auth_form_places == 'form_everywhere' and \
|
||||
self.host.auth_form_action
|
||||
if filters or replace_login_form:
|
||||
if replace_login_form and not 'output_replace_form' in output_filters:
|
||||
output_filters.append('output_replace_form')
|
||||
if output_filters:
|
||||
print output_filters
|
||||
location = Location(self.host)
|
||||
conf = { 'auth_form_action': self.host.auth_form_action,
|
||||
'name': self.host.name,
|
||||
'larpe_dir': get_publisher().app_dir,
|
||||
'logout_url': location.new_logout_url,
|
||||
'login_url': location.new_auth_url }
|
||||
# Set Python filter path for Apache configuration
|
||||
python_path = os.path.join(self.host.site_dir, 'filters')
|
||||
if python_path not in self.host.apache_python_paths:
|
||||
self.host.apache_python_paths.append(python_path)
|
||||
|
||||
# Write Python filter
|
||||
python_file = open(os.path.join(self.host.site_dir, 'filters', 'output_replace_form.py'), 'w')
|
||||
python_file.write(open(OUTPUT_FILTER_BASE).read())
|
||||
python_file.write("def filter_page(filter, page):\n")
|
||||
if replace_login_form:
|
||||
python_file.write('''\
|
||||
current_form = re.compile('<form [^>]*?action="%(auth_form_action)s".*?>.*?</form>', re.DOTALL)
|
||||
page = current_form.sub('<form method="post" action="/liberty/%(name)s/login"><input type="submit" value="Connexion" /></form>', page)'''
|
||||
% { 'auth_form_action': self.host.auth_form_action, 'name': self.host.name })
|
||||
for filter in filters:
|
||||
python_file.write(" r = re.compile(r\"\"\"%s\"\"\")\n" % filter["re"])
|
||||
python_file.write(" page = r.sub(r\"\"\"%s\"\"\", page)\n" % filter["sub"])
|
||||
python_file.write(" return page\n")
|
||||
python_file.close()
|
||||
# Set Python filter for Apache configuration
|
||||
if not 'output_replace_form' in self.host.apache_output_python_filters:
|
||||
self.host.apache_output_python_filters.append('output_replace_form')
|
||||
for filter in output_filters:
|
||||
python_file = open(
|
||||
os.path.join(self.host.site_dir, 'filters', filter + ".py"),
|
||||
'w')
|
||||
python_file.write(
|
||||
open(os.path.join(DATA_DIR, "filters", filter + ".py")).read() % conf
|
||||
)
|
||||
if not filter in self.host.apache_output_python_filters:
|
||||
self.host.apache_output_python_filters.append(filter)
|
||||
|
||||
def sso_init_link [html] (self):
|
||||
form = self.form_sso_init_link()
|
||||
|
|
|
@ -8,6 +8,7 @@ from larpe.site_authentication import SiteAuthentication
|
|||
|
||||
class CirilSiteAuthentication(SiteAuthentication):
|
||||
plugin_name = 'ciril'
|
||||
output_filters = ['output_ciril_net_rh']
|
||||
|
||||
def auto_detect_site(cls, html_doc):
|
||||
if re.search(
|
||||
|
@ -36,19 +37,6 @@ class CirilSiteAuthentication(SiteAuthentication):
|
|||
|
||||
return success, return_content
|
||||
|
||||
def get_filters(self):
|
||||
""" Rewrite the logout link """
|
||||
filters = []
|
||||
logout = {}
|
||||
if not hasattr(self.host, 'base_url'):
|
||||
return None
|
||||
base_url_tokens = self.host.base_url.split('/')
|
||||
base_url_tokens[-1] = 'logout'
|
||||
new_logout_url = '/'.join(base_url_tokens)
|
||||
logout["re"] = """(<a.*?href=["']).*?(["'].*?>D.*?connexion</a>)"""
|
||||
logout["sub"] = r"\1%s\2" % new_logout_url
|
||||
filters.append(logout)
|
||||
return filters
|
||||
|
||||
|
||||
site_authentication_plugins.register(CirilSiteAuthentication)
|
||||
|
|
|
@ -26,8 +26,10 @@ from users import User
|
|||
from federations import Federation
|
||||
|
||||
class SiteAuthentication:
|
||||
|
||||
def __init__(self, host):
|
||||
self.host = host
|
||||
self.output_filters = []
|
||||
|
||||
def federate(self, username, password, provider_id, cookies, select):
|
||||
user = get_session().get_user(provider_id)
|
||||
|
@ -105,18 +107,6 @@ class SiteAuthentication:
|
|||
value = values[0], options = options)
|
||||
return form
|
||||
|
||||
def get_filters(self):
|
||||
""" Allows to filters the page
|
||||
Example with a logout link:
|
||||
filters = []
|
||||
filter = {}
|
||||
filer["re"] = "<a.*?href=\"home.php\.*?\">Logout</a>"
|
||||
filer["sub"] = "<a href=\"/logout\">Logout</a>" % logout_url
|
||||
filters.append(filter)
|
||||
return filters
|
||||
"""
|
||||
return list()
|
||||
|
||||
def submit_local_auth_form(self, form):
|
||||
username = form.get_widget('username').parse()
|
||||
password = form.get_widget('password').parse()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#! /usr/bin/env python
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
import distutils.core
|
||||
|
|
Reference in New Issue