[Zope3-checkins] CVS: Zope3/src/zope/products/demo/passwdauth - MAINTAINER.txt:1.1 README.txt:1.1 __init__.py:1.1 configure.zcml:1.1 interfaces.py:1.1 passwd.sample:1.1 tests.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Feb 14 22:21:06 EST 2004


Update of /cvs-repository/Zope3/src/zope/products/demo/passwdauth
In directory cvs.zope.org:/tmp/cvs-serv21279/passwdauth

Added Files:
	MAINTAINER.txt README.txt __init__.py configure.zcml 
	interfaces.py passwd.sample tests.py 
Log Message:
Updated the passwdauth product and moved it to the correct location. It is
now the only up-to-date principal source, since the Persistent version is
not registered correctly and I think the code has some bugs too.

Note that this implementation is in sync with the cookbook now (at least 
with my local copy). 


=== Added File Zope3/src/zope/products/demo/passwdauth/MAINTAINER.txt ===
Stephan Richter

  Email: stephan.richter at tufts.edu

  IRC nick: srichter

=== Added File Zope3/src/zope/products/demo/passwdauth/README.txt ===
Passwd Principal Authentication Source Demo Product

  This product is developed as an example for the Zope 3 Cookbook. You can
  find the text at http://dev.zope.org/Zope3/DevelCookbook. Therefore the
  product will never be considered feature complete and look like a very crude
  product. However, it demonstrates nicely the necessary steps that have to be
  taken to create a principal authentication source.

  IMPORTANT: Please do not edit any of the code (except adding comments)
             unless you are willing to make the correct modifications in the
             corresponding recipe as well!

  Feel free to send comments to srichter at cosmos.phy.tufts.edu.


=== Added File Zope3/src/zope/products/demo/passwdauth/__init__.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""/etc/passwd Authentication Plugin

This package defines a new authentication plugin, which can use textfiles to
authenticate users.

$Id: __init__.py,v 1.1 2004/02/15 03:21:05 srichter Exp $
"""
import os
from persistence import Persistent
from zope.app.container.contained import Contained
from zope.app.location import locate
from zope.app.services.pluggableauth import SimplePrincipal
from zope.app.interfaces.services.pluggableauth import \
     ILoginPasswordPrincipalSource
from zope.exceptions import NotFoundError
from zope.interface import implements
from interfaces import IFileBasedPrincipalSource

class PasswdPrincipalSource(Contained, Persistent):
    """A Principal Source for /etc/passwd-like files."""

    implements(ILoginPasswordPrincipalSource, IFileBasedPrincipalSource)

    def __init__(self, filename=''):
        self.filename = filename

    def readPrincipals(self):
        if not os.path.exists(self.filename):
            return []
        file = open(self.filename, 'r')
        principals = []
        for line in file.readlines():
            if line.strip() != '':
                user_info = line.strip().split(':', 3)
                p = SimplePrincipal(*user_info)
                locate(p, self, p.id)
                p.id = p.login
                principals.append(p)
        return principals

    def getPrincipal(self, id):
        """See IPrincipalSource."""
        earmark, source_name, id = id.split('\t')
        for p in self.readPrincipals():
            if p.id == id:
                return p
        raise NotFoundError, id

    def getPrincipals(self, name):
        """See IPrincipalSource."""
        return filter(lambda p: p.login.find(name) != -1,
                      self.readPrincipals())

    def authenticate(self, login, password):
        """See ILoginPasswordPrincipalSource. """
        for user in self.readPrincipals():
            if user.login == login and user.password == password:
                return user


=== Added File Zope3/src/zope/products/demo/passwdauth/configure.zcml ===
<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser"
    i18n_domain="demo_passwdauth">

<content class=".PasswdPrincipalSource">
  <factory
      id="zope.app.principalsources.PasswdPrincipalSource"
      permission="zope.ManageServices"/>
  <allow interface=".interfaces.IFileBasedPrincipalSource"/>
  <require
      permission="zope.ManageContent"
      set_schema=".interfaces.IFileBasedPrincipalSource"/>
</content>

<browser:addform
    schema=".interfaces.IFileBasedPrincipalSource" 
    label="Add file-based Principal Source in /etc/passwd style" 
    content_factory=".PasswdPrincipalSource" 
    arguments="filename" 
    name="AddPasswdPrincipalSourceForm" 
    menu="add_principal_source" title="/etc/passwd Principal Source" 
    permission="zope.ManageContent" /> 

<browser:editform 
    schema=".interfaces.IFileBasedPrincipalSource" 
    label="Edit file-based Principal Source" 
    name="edit.html"        
    menu="zmi_views" title="Edit" 
    permission="zope.ManageContent" /> 

</configure>


=== Added File Zope3/src/zope/products/demo/passwdauth/interfaces.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""/etc/passwd Authentication Plugin interfaces

$Id: interfaces.py,v 1.1 2004/02/15 03:21:05 srichter Exp $
"""
from zope.schema import TextLine
from zope.app.i18n import ZopeMessageIDFactory as _

from zope.app.interfaces.services.pluggableauth import IPrincipalSource

class IFileBasedPrincipalSource(IPrincipalSource):
    """Describes file-based principal sources."""

    filename = TextLine(
        title = _(u'File Name'),
        description=_(u'File name of the data file.'),
        default = u'/etc/passwd')


=== Added File Zope3/src/zope/products/demo/passwdauth/passwd.sample ===
foo1:bar1:Foo Bar 1
foo2:bar2:Foo Bar 2


=== Added File Zope3/src/zope/products/demo/passwdauth/tests.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""/etc/passwd Authentication Plugin Tests

$Id: tests.py,v 1.1 2004/02/15 03:21:05 srichter Exp $
"""
import os
from zope.products.demo import passwdauth
from zope.exceptions import NotFoundError
from unittest import TestCase, TestSuite, main, makeSuite

class PasswdPrincipalSourceTest(TestCase):

    def setUp(self):
        dir = os.path.split(passwdauth.__file__)[0]
        self.source = passwdauth.PasswdPrincipalSource(
            os.path.join(dir, 'passwd.sample'))

    def test_getPrincipal(self):
        self.assertEqual(self.source.getPrincipal('\t\tfoo1').password, 'bar1')
        self.assertEqual(self.source.getPrincipal('\t\tfoo2').password, 'bar2')
        self.assertRaises(NotFoundError, self.source.getPrincipal, '\t\tfoo')

    def test_getPrincipals(self):
        self.assertEqual(len(self.source.getPrincipals('foo')), 2)
        self.assertEqual(len(self.source.getPrincipals('')), 2)
        self.assertEqual(len(self.source.getPrincipals('2')), 1)

    def test_authenticate(self):
        self.assertEqual(self.source.authenticate('foo1', 'bar1').id, 'foo1')
        self.assertEqual(self.source.authenticate('foo1', 'bar'), None)
        self.assertEqual(self.source.authenticate('foo', 'bar'), None)
    
def test_suite():
    return TestSuite((
        makeSuite(PasswdPrincipalSourceTest),
        ))
    
if __name__=='__main__':
    main(defaultTest='test_suite')
    




More information about the Zope3-Checkins mailing list