1
2
3
4
5
6
7
8
9
10
11 import os
12 from random import random
13 from datetime import datetime
14
15 from OFS.Folder import Folder
16 from Products.PluggableAuthService import plugins
17 from Products.PluggableAuthService import interfaces
18 from Products.PluggableAuthService import PluggableAuthService
19
20 from zope import component
21 import ZPublisher.interfaces
22
23 ZENOSS_ROLES = ['ZenUser', 'ZenManager']
35
38 """
39 Note: copied and adapted from AccessControl.User.BasicUser
40
41 If there are no users or only one user in this user folder,
42 populates from the 'inituser' file in the instance home.
43 We have to do this even when there is already a user
44 just in case the initial user ignored the setup messages.
45 We don't do it for more than one user to avoid
46 abuse of this mechanism.
47 Called only by OFS.Application.initialize().
48 """
49 from AccessControl.User import readUserAccessFile
50
51 plugins = self.plugins.listPlugins(
52 interfaces.plugins.IUserEnumerationPlugin)
53
54 userCounts = [ len(plugin.listUserInfo()) for id, plugin in plugins if hasattr(plugin, "listUserInfo")]
55
56 if len(userCounts) <= 1:
57 info = readUserAccessFile('inituser')
58 if info:
59 import App.config
60 name, password, domains, remote_user_mode = info
61 userManagers = self.plugins.listPlugins(interfaces.plugins.IUserAdderPlugin)
62 roleManagers = self.plugins.listPlugins(interfaces.plugins.IRolesPlugin)
63 for pluginId, userPlugin in userManagers:
64
65 try:
66 userPlugin.removeUser(name)
67 except KeyError:
68
69 pass
70
71 userPlugin.doAddUser(name, password)
72
73 for pluginId, rolePlugin in roleManagers:
74 rolePlugin.assignRoleToPrincipal('Manager', name)
75 cfg = App.config.getConfiguration()
76
77 try:
78 os.remove(os.path.join(cfg.instancehome, 'inituser'))
79 except:
80 pass
81
92
95 acl = context.acl_users
96 id = 'basicAuthHelper'
97 if not hasattr(acl, id):
98 plugins.HTTPBasicAuthHelper.addHTTPBasicAuthHelper(acl, id)
99 interfaces = []
100 physPath = '/'.join(context.getPhysicalPath())
101 if physPath == '':
102 interfaces = ['IExtractionPlugin', 'IChallengePlugin',
103 'ICredentialsResetPlugin']
104 elif physPath == '/zport':
105 interfaces = ['IExtractionPlugin', 'IChallengePlugin']
106 acl.basicAuthHelper.manage_activateInterfaces(interfaces)
107
110 acl = context.acl_users
111 id = 'cookieAuthHelper'
112 if not hasattr(acl, id):
113 plugins.CookieAuthHelper.addCookieAuthHelper(acl, id)
114 interfaces = []
115
116
117 physPath = '/'.join(context.getPhysicalPath())
118 if physPath == '':
119 interfaces = ['IExtractionPlugin']
120 elif physPath == '/zport':
121 interfaces = ['IExtractionPlugin',
122 'ICredentialsResetPlugin',
123 'IChallengePlugin']
124 if primaryAuth:
125 interfaces.append('ICredentialsUpdatePlugin')
126 acl.cookieAuthHelper.manage_activateInterfaces(interfaces)
127
129 acl = context.acl_users
130 id = 'sessionAuthHelper'
131 if not hasattr(acl, id):
132 plugins.SessionAuthHelper.manage_addSessionAuthHelper(acl, id)
133
134 interfaces = ['IExtractionPlugin',
135 'ICredentialsResetPlugin']
136 if primaryAuth:
137 interfaces.append('ICredentialsUpdatePlugin')
138 acl.sessionAuthHelper.manage_activateInterfaces(interfaces)
139
141 """
142 This sets cookie authentication as the primary auth
143 mechanism. This means that the users credentials will be stored
144 encoded in a cookie.
145 """
146 setupCookieHelper(context, primaryAuth=True)
147 setupSessionHelper(context, primaryAuth=False)
148
150 """
151 Stores the user credentials in the session and the token is sent
152 to the server. The user will be forced to re-login when zope
153 restarts or the session times out.
154 """
155 setupCookieHelper(context, primaryAuth=False)
156 setupSessionHelper(context, primaryAuth=True)
157
159 acl = context.acl_users
160 id = 'roleManager'
161 if not hasattr(acl, id):
162 plugins.ZODBRoleManager.addZODBRoleManager(acl, id)
163 acl.roleManager.manage_activateInterfaces(['IRolesPlugin',
164 'IRoleEnumerationPlugin', 'IRoleAssignerPlugin'])
165
166 for role in ZENOSS_ROLES:
167 try:
168 acl.roleManager.addRole(role)
169 except KeyError:
170
171 pass
172
175 acl = context.acl_users
176 id = 'userManager'
177 if not hasattr(acl, id):
178 plugins.ZODBUserManager.addZODBUserManager(acl, id)
179 acl.userManager.manage_activateInterfaces(['IAuthenticationPlugin',
180 'IUserEnumerationPlugin', 'IUserAdderPlugin'])
181
184 acl = context.acl_users
185 id = 'requestTypeSniffer'
186 if not hasattr(acl, id):
187 plugins.RequestTypeSniffer.addRequestTypeSnifferPlugin(acl, id)
188 acl.requestTypeSniffer.manage_activateInterfaces(['IRequestTypeSniffer'])
189
192 acl = context.acl_users
193 id = 'protocolChooser'
194 if not hasattr(acl, id):
195 plugins.ChallengeProtocolChooser.addChallengeProtocolChooserPlugin(acl,
196 id)
197 acl.protocolChooser.manage_activateInterfaces([
198 'IChallengeProtocolChooser'])
199 protocolMapping = {}
200
201 physPath = '/'.join(context.getPhysicalPath())
202 if physPath == '':
203 protocolMapping = {
204 'Browser': ['http'],
205 'FTP': ['http'],
206 'WebDAV': ['http'],
207 'XML-RPC': ['http'],
208 }
209 elif physPath == '/zport':
210 protocolMapping = {
211 'FTP': ['http'],
212 'WebDAV': ['http'],
213 'XML-RPC': ['http'],
214 }
215
216 icookie = plugins.CookieAuthHelper.ICookieAuthHelper
217 ichallenge = interfaces.plugins.IChallengePlugin
218 challenge = [ p for id, p in acl.plugins.listPlugins(ichallenge) ]
219
220 cookiePlugins = [ p for p in challenge if icookie.providedBy(p) ]
221
222
223
224
225 cookie = cookiePlugins[0]
226 index = challenge.index(cookie)
227 for i in xrange(index):
228 acl.plugins.movePluginsUp(ichallenge, [cookie.id])
229 acl.protocolChooser.manage_updateProtocolMapping(protocolMapping)
230
242
273
311
312 @component.adapter(ZPublisher.interfaces.IPubEnd)
314 """Zope session cookie should only accesible from the server side"""
315 if '_ZopeId' in event.request.response.cookies and 'http_only' not in event.request.response.cookies['_ZopeId']:
316 event.request.response.cookies['_ZopeId']['http_only'] = True
317