/usr/share/pyshared/zope/principalregistry/principalregistry.py is in python-zope.principalregistry 3.7.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | ##############################################################################
#
# Copyright (c) 2001-2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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.
#
##############################################################################
"""Global Authentication Utility or Principal Registry
"""
from zope.component import getUtility
from zope.interface import implements
import zope.security.management
from zope.security.interfaces import IGroupAwarePrincipal
from zope.password.interfaces import IPasswordManager
from zope.authentication.interfaces import (
IAuthentication,
PrincipalLookupError,
ILoginPassword,
ILogout,
IUnauthenticatedPrincipal,
IUnauthenticatedGroup,
IAuthenticatedGroup,
IEveryoneGroup,
)
class DuplicateLogin(Exception):
pass
class DuplicateId(Exception):
pass
class PrincipalRegistry(object):
implements(IAuthentication, ILogout)
# Methods implementing IAuthentication
def authenticate(self, request):
a = ILoginPassword(request, None)
if a is not None:
login = a.getLogin()
if login is not None:
p = self.__principalsByLogin.get(login, None)
if p is not None:
password = a.getPassword()
if p.validate(password):
return p
return None
__defaultid = None
__defaultObject = None
def defineDefaultPrincipal(self, id, title, description='',
principal=None):
if id in self.__principalsById:
raise DuplicateId(id)
self.__defaultid = id
if principal is None:
principal = UnauthenticatedPrincipal(id, title, description)
principal.__name__ = id
principal.__parent__ = self
self.__defaultObject = principal
return principal
def unauthenticatedPrincipal(self):
return self.__defaultObject
def unauthorized(self, id, request):
if id is None or id is self.__defaultid:
a = ILoginPassword(request)
a.needLogin(realm="Zope")
def getPrincipal(self, id):
r = self.__principalsById.get(id)
if r is None:
if id == self.__defaultid:
return self.__defaultObject
if id == zope.security.management.system_user.id:
return zope.security.management.system_user
raise PrincipalLookupError(id)
return r
def getPrincipalByLogin(self, login):
return self.__principalsByLogin[login]
def getPrincipals(self, name):
name = name.lower()
return [p for p in self.__principalsById.itervalues()
if p.title.lower().startswith(name) or
p.getLogin().lower().startswith(name)]
def logout(self, request):
# not supporting basic auth logout -- no such thing
pass
# Management methods
def __init__(self):
self.__principalsById = {}
self.__principalsByLogin = {}
def definePrincipal(self, principal, title, description='',
login='', password='', passwordManagerName='Plain Text'):
id=principal
if login in self.__principalsByLogin:
raise DuplicateLogin(login)
if id in self.__principalsById or id == self.__defaultid:
raise DuplicateId(id)
p = Principal(id, title, description,
login, password, passwordManagerName)
p.__name__ = id
p.__parent__ = self
self.__principalsByLogin[login] = p
self.__principalsById[id] = p
return p
def registerGroup(self, group):
id = group.id
if id in self.__principalsById or id == self.__defaultid:
raise DuplicateId(id)
self.__principalsById[group.id] = group
def _clear(self):
self.__init__()
self.__defaultid = None
self.__defaultObject = None
principalRegistry = PrincipalRegistry()
# Register our cleanup with Testing.CleanUp to make writing unit tests
# simpler.
try:
from zope.testing.cleanup import addCleanUp
except ImportError:
pass
else:
addCleanUp(principalRegistry._clear)
del addCleanUp
class PrincipalBase(object):
__name__ = __parent__ = None
def __init__(self, id, title, description):
self.id = id
self.title = title
self.description = description
self.groups = []
class Group(PrincipalBase):
def getLogin(self):
return '' # to make registry search happy
class Principal(PrincipalBase):
implements(IGroupAwarePrincipal)
def __init__(self, id, title, description, login,
pw, pwManagerName="Plain Text"):
super(Principal, self).__init__(id, title, description)
self.__login = login
self.__pwManagerName = pwManagerName
self.__pw = pw
def __getPasswordManager(self):
return getUtility(IPasswordManager, self.__pwManagerName)
def getLogin(self):
return self.__login
def validate(self, pw):
pwManager = self.__getPasswordManager()
return pwManager.checkPassword(self.__pw, pw)
class UnauthenticatedPrincipal(PrincipalBase):
implements(IUnauthenticatedPrincipal)
fallback_unauthenticated_principal = (
UnauthenticatedPrincipal(
__name__+'.fallback_unauthenticated_principal',
'Fallback unauthenticated principal',
'The default unauthenticated principal. Used as a fallback to '
'allow challenging for a user even if the IAuthentication returned '
'None as the unauthenticated principal.'))
class UnauthenticatedGroup(Group):
implements(IUnauthenticatedGroup)
class AuthenticatedGroup(Group):
implements(IAuthenticatedGroup)
class EverybodyGroup(Group):
implements(IEveryoneGroup)
|