This file is indexed.

/usr/share/pyshared/zope/pluggableauth/README.txt is in python-zope.pluggableauth 1.3-0ubuntu1.

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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
================================
Pluggable-Authentication Utility
================================

The Pluggable-Authentication Utility (PAU) provides a framework for
authenticating principals and associating information with them. It uses
plugins and subscribers to get its work done.

For a pluggable-authentication utility to be used, it should be
registered as a utility providing the
`zope.authentication.interfaces.IAuthentication` interface.

Authentication
--------------

The primary job of PAU is to authenticate principals. It uses two types of
plug-ins in its work:

  - Credentials Plugins

  - Authenticator Plugins

Credentials plugins are responsible for extracting user credentials from a
request. A credentials plugin may in some cases issue a 'challenge' to obtain
credentials. For example, a 'session' credentials plugin reads credentials
from a session (the "extraction"). If it cannot find credentials, it will
redirect the user to a login form in order to provide them (the "challenge").

Authenticator plugins are responsible for authenticating the credentials
extracted by a credentials plugin. They are also typically able to create
principal objects for credentials they successfully authenticate.

Given a request object, the PAU returns a principal object, if it can. The PAU
does this by first iterateing through its credentials plugins to obtain a
set of credentials. If it gets credentials, it iterates through its
authenticator plugins to authenticate them.

If an authenticator succeeds in authenticating a set of credentials, the PAU
uses the authenticator to create a principal corresponding to the credentials.
The authenticator notifies subscribers if an authenticated principal is
created. Subscribers are responsible for adding data, especially groups, to
the principal. Typically, if a subscriber adds data, it should also add
corresponding interface declarations.

Simple Credentials Plugin
~~~~~~~~~~~~~~~~~~~~~~~~~

To illustrate, we'll create a simple credentials plugin::

  >>> from zope import interface
  >>> from zope.pluggableauth.authentication import interfaces

  >>> class MyCredentialsPlugin(object):
  ...
  ...     interface.implements(interfaces.ICredentialsPlugin)
  ...
  ...     def extractCredentials(self, request):
  ...         return request.get('credentials')
  ...
  ...     def challenge(self, request):
  ...         pass # challenge is a no-op for this plugin
  ...
  ...     def logout(self, request):
  ...         pass # logout is a no-op for this plugin

As a plugin, MyCredentialsPlugin needs to be registered as a named utility::

  >>> myCredentialsPlugin = MyCredentialsPlugin()
  >>> provideUtility(myCredentialsPlugin, name='My Credentials Plugin')

Simple Authenticator Plugin
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Next we'll create a simple authenticator plugin. For our plugin, we'll need
an implementation of IPrincipalInfo::

  >>> class PrincipalInfo(object):
  ...
  ...     interface.implements(interfaces.IPrincipalInfo)
  ...
  ...     def __init__(self, id, title, description):
  ...         self.id = id
  ...         self.title = title
  ...         self.description = description
  ...
  ...     def __repr__(self):
  ...         return 'PrincipalInfo(%r)' % self.id

Our authenticator uses this type when it creates a principal info::

  >>> class MyAuthenticatorPlugin(object):
  ...
  ...     interface.implements(interfaces.IAuthenticatorPlugin)
  ...
  ...     def authenticateCredentials(self, credentials):
  ...         if credentials == 'secretcode':
  ...             return PrincipalInfo('bob', 'Bob', '')
  ...
  ...     def principalInfo(self, id):
  ...         pass # plugin not currently supporting search

As with the credentials plugin, the authenticator plugin must be registered
as a named utility::

  >>> myAuthenticatorPlugin = MyAuthenticatorPlugin()
  >>> provideUtility(myAuthenticatorPlugin, name='My Authenticator Plugin')

Configuring a PAU
~~~~~~~~~~~~~~~~~

Finally, we'll create the PAU itself::

  >>> from zope.pluggableauth import authentication
  >>> pau = authentication.PluggableAuthentication('xyz_')

and configure it with the two plugins::

  >>> pau.credentialsPlugins = ('My Credentials Plugin', )
  >>> pau.authenticatorPlugins = ('My Authenticator Plugin', )

Using the PAU to Authenticate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  >>> from zope.pluggableauth.factories import AuthenticatedPrincipalFactory
  >>> provideAdapter(AuthenticatedPrincipalFactory)

We can now use the PAU to authenticate a sample request::

  >>> from zope.publisher.browser import TestRequest
  >>> print pau.authenticate(TestRequest())
  None

In this case, we cannot authenticate an empty request. In the same way, we
will not be able to authenticate a request with the wrong credentials::

  >>> print pau.authenticate(TestRequest(credentials='let me in!'))
  None

However, if we provide the proper credentials::

  >>> request = TestRequest(credentials='secretcode')
  >>> principal = pau.authenticate(request)
  >>> principal
  Principal('xyz_bob')

we get an authenticated principal.

Multiple Authenticator Plugins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The PAU works with multiple authenticator plugins. It uses each plugin, in the
order specified in the PAU's authenticatorPlugins attribute, to authenticate
a set of credentials.

To illustrate, we'll create another authenticator::

  >>> class MyAuthenticatorPlugin2(MyAuthenticatorPlugin):
  ...
  ...     def authenticateCredentials(self, credentials):
  ...         if credentials == 'secretcode':
  ...             return PrincipalInfo('black', 'Black Spy', '')
  ...         elif credentials == 'hiddenkey':
  ...             return PrincipalInfo('white', 'White Spy', '')

  >>> provideUtility(MyAuthenticatorPlugin2(), name='My Authenticator Plugin 2')

If we put it before the original authenticator::

  >>> pau.authenticatorPlugins = (
  ...     'My Authenticator Plugin 2',
  ...     'My Authenticator Plugin')

Then it will be given the first opportunity to authenticate a request::

  >>> pau.authenticate(TestRequest(credentials='secretcode'))
  Principal('xyz_black')

If neither plugins can authenticate, pau returns None::

  >>> print pau.authenticate(TestRequest(credentials='let me in!!'))
  None

When we change the order of the authenticator plugins::

  >>> pau.authenticatorPlugins = (
  ...     'My Authenticator Plugin',
  ...     'My Authenticator Plugin 2')

we see that our original plugin is now acting first::

  >>> pau.authenticate(TestRequest(credentials='secretcode'))
  Principal('xyz_bob')

The second plugin, however, gets a chance to authenticate if first does not::

  >>> pau.authenticate(TestRequest(credentials='hiddenkey'))
  Principal('xyz_white')

Multiple Credentials Plugins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As with with authenticators, we can specify multiple credentials plugins. To
illustrate, we'll create a credentials plugin that extracts credentials from
a request form::

  >>> class FormCredentialsPlugin:
  ...
  ...     interface.implements(interfaces.ICredentialsPlugin)
  ...
  ...     def extractCredentials(self, request):
  ...         return request.form.get('my_credentials')
  ...
  ...     def challenge(self, request):
  ...         pass
  ...
  ...     def logout(request):
  ...         pass

  >>> provideUtility(FormCredentialsPlugin(),
  ...                name='Form Credentials Plugin')

and insert the new credentials plugin before the existing plugin::

  >>> pau.credentialsPlugins = (
  ...     'Form Credentials Plugin',
  ...     'My Credentials Plugin')

The PAU will use each plugin in order to try and obtain credentials from a
request::

  >>> pau.authenticate(TestRequest(credentials='secretcode',
  ...                              form={'my_credentials': 'hiddenkey'}))
  Principal('xyz_white')

In this case, the first credentials plugin succeeded in getting credentials
from the form and the second authenticator was able to authenticate the
credentials. Specifically, the PAU went through these steps:

 - Get credentials using 'Form Credentials Plugin'

 - Got 'hiddenkey' credentials using 'Form Credentials Plugin', try to
   authenticate using 'My Authenticator Plugin'

 - Failed to authenticate 'hiddenkey' with 'My Authenticator Plugin', try
   'My Authenticator Plugin 2'

 - Succeeded in authenticating with 'My Authenticator Plugin 2'

Let's try a different scenario::

  >>> pau.authenticate(TestRequest(credentials='secretcode'))
  Principal('xyz_bob')

In this case, the PAU went through these steps::

  - Get credentials using 'Form Credentials Plugin'

  - Failed to get credentials using 'Form Credentials Plugin', try
    'My Credentials Plugin'

  - Got 'scecretcode' credentials using 'My Credentials Plugin', try to
    authenticate using 'My Authenticator Plugin'

  - Succeeded in authenticating with 'My Authenticator Plugin'

Let's try a slightly more complex scenario::

  >>> pau.authenticate(TestRequest(credentials='hiddenkey',
  ...                              form={'my_credentials': 'bogusvalue'}))
  Principal('xyz_white')

This highlights PAU's ability to use multiple plugins for authentication:

  - Get credentials using 'Form Credentials Plugin'

  - Got 'bogusvalue' credentials using 'Form Credentials Plugin', try to
    authenticate using 'My Authenticator Plugin'

  - Failed to authenticate 'boguskey' with 'My Authenticator Plugin', try
    'My Authenticator Plugin 2'

  - Failed to authenticate 'boguskey' with 'My Authenticator Plugin 2' --
    there are no more authenticators to try, so lets try the next credentials
    plugin for some new credentials

  - Get credentials using 'My Credentials Plugin'

  - Got 'hiddenkey' credentials using 'My Credentials Plugin', try to
    authenticate using 'My Authenticator Plugin'

  - Failed to authenticate 'hiddenkey' using 'My Authenticator Plugin', try
    'My Authenticator Plugin 2'

  - Succeeded in authenticating with 'My Authenticator Plugin 2' (shouts and
    cheers!)

Multiple Authenticator Plugins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As with the other operations we've seen, the PAU uses multiple plugins to
find a principal. If the first authenticator plugin can't find the requested
principal, the next plugin is used, and so on.

  >>> class AnotherAuthenticatorPlugin:
  ...
  ...     interface.implements(interfaces.IAuthenticatorPlugin)
  ...
  ...     def __init__(self):
  ...         self.infos = {}
  ...         self.ids = {}
  ...
  ...     def principalInfo(self, id):
  ...         return self.infos.get(id)
  ...
  ...     def authenticateCredentials(self, credentials):
  ...         id = self.ids.get(credentials)
  ...         if id is not None:
  ...             return self.infos[id]
  ...
  ...     def add(self, id, title, description, credentials):
  ...         self.infos[id] = PrincipalInfo(id, title, description)
  ...         self.ids[credentials] = id


To illustrate, we'll create and register two authenticators::

  >>> authenticator1 = AnotherAuthenticatorPlugin()
  >>> provideUtility(authenticator1, name='Authentication Plugin 1')

  >>> authenticator2 = AnotherAuthenticatorPlugin()
  >>> provideUtility(authenticator2, name='Authentication Plugin 2')

and add a principal to them::

  >>> authenticator1.add('bob', 'Bob', 'A nice guy', 'b0b')
  >>> authenticator1.add('white', 'White Spy', 'Sneaky', 'deathtoblack')
  >>> authenticator2.add('black', 'Black Spy', 'Also sneaky', 'deathtowhite')

When we configure the PAU to use both searchable authenticators (note the
order)::

  >>> pau.authenticatorPlugins = (
  ...     'Authentication Plugin 2',
  ...     'Authentication Plugin 1')

we register the factories for our principals::

  >>> from zope.pluggableauth.factories import FoundPrincipalFactory
  >>> provideAdapter(FoundPrincipalFactory)

we see how the PAU uses both plugins::

  >>> pau.getPrincipal('xyz_white')
  Principal('xyz_white')

  >>> pau.getPrincipal('xyz_black')
  Principal('xyz_black')

If more than one plugin know about the same principal ID, the first plugin is
used and the remaining are not delegated to. To illustrate, we'll add
another principal with the same ID as an existing principal::

  >>> authenticator2.add('white', 'White Rider', '', 'r1der')
  >>> pau.getPrincipal('xyz_white').title
  'White Rider'

If we change the order of the plugins::

  >>> pau.authenticatorPlugins = (
  ...     'Authentication Plugin 1',
  ...     'Authentication Plugin 2')

we get a different principal for ID 'white'::

  >>> pau.getPrincipal('xyz_white').title
  'White Spy'


Issuing a Challenge
-------------------

Part of PAU's IAuthentication contract is to challenge the user for
credentials when its 'unauthorized' method is called. The need for this
functionality is driven by the following use case:

  - A user attempts to perform an operation he is not authorized to perform.

  - A handler responds to the unauthorized error by calling IAuthentication
    'unauthorized'.

  - The authentication component (in our case, a PAU) issues a challenge to
    the user to collect new credentials (typically in the form of logging in
    as a new user).

The PAU handles the credentials challenge by delegating to its credentials
plugins.

Currently, the PAU is configured with the credentials plugins that don't
perform any action when asked to challenge (see above the 'challenge' methods).

To illustrate challenges, we'll subclass an existing credentials plugin and
do something in its 'challenge'::

  >>> class LoginFormCredentialsPlugin(FormCredentialsPlugin):
  ...
  ...     def __init__(self, loginForm):
  ...         self.loginForm = loginForm
  ...
  ...     def challenge(self, request):
  ...         request.response.redirect(self.loginForm)
  ...         return True

This plugin handles a challenge by redirecting the response to a login form.
It returns True to signal to the PAU that it handled the challenge.

We will now create and register a couple of these plugins::

  >>> provideUtility(LoginFormCredentialsPlugin('simplelogin.html'),
  ...                name='Simple Login Form Plugin')

  >>> provideUtility(LoginFormCredentialsPlugin('advancedlogin.html'),
  ...                name='Advanced Login Form Plugin')

and configure the PAU to use them::

  >>> pau.credentialsPlugins = (
  ...     'Simple Login Form Plugin',
  ...     'Advanced Login Form Plugin')

Now when we call 'unauthorized' on the PAU::

  >>> request = TestRequest()
  >>> pau.unauthorized(id=None, request=request)

we see that the user is redirected to the simple login form::

  >>> request.response.getStatus()
  302
  >>> request.response.getHeader('location')
  'simplelogin.html'

We can change the challenge policy by reordering the plugins::

  >>> pau.credentialsPlugins = (
  ...     'Advanced Login Form Plugin',
  ...     'Simple Login Form Plugin')

Now when we call 'unauthorized'::

  >>> request = TestRequest()
  >>> pau.unauthorized(id=None, request=request)

the advanced plugin is used because it's first::

  >>> request.response.getStatus()
  302
  >>> request.response.getHeader('location')
  'advancedlogin.html'

Challenge Protocols
~~~~~~~~~~~~~~~~~~~

Sometimes, we want multiple challengers to work together. For example, the
HTTP specification allows multiple challenges to be issued in a response. A
challenge plugin can provide a `challengeProtocol` attribute that effectively
groups related plugins together for challenging. If a plugin returns `True`
from its challenge and provides a non-None challengeProtocol, subsequent
plugins in the credentialsPlugins list that have the same challenge protocol
will also be used to challenge.

Without a challengeProtocol, only the first plugin to succeed in a challenge
will be used.

Let's look at an example. We'll define a new plugin that specifies an
'X-Challenge' protocol::

  >>> class XChallengeCredentialsPlugin(FormCredentialsPlugin):
  ...
  ...     challengeProtocol = 'X-Challenge'
  ...
  ...     def __init__(self, challengeValue):
  ...         self.challengeValue = challengeValue
  ...
  ...     def challenge(self, request):
  ...         value = self.challengeValue
  ...         existing = request.response.getHeader('X-Challenge', '')
  ...         if existing:
  ...             value += ' ' + existing
  ...         request.response.setHeader('X-Challenge', value)
  ...         return True

and register a couple instances as utilities::

  >>> provideUtility(XChallengeCredentialsPlugin('basic'),
  ...                name='Basic X-Challenge Plugin')

  >>> provideUtility(XChallengeCredentialsPlugin('advanced'),
  ...                name='Advanced X-Challenge Plugin')

When we use both plugins with the PAU::

  >>> pau.credentialsPlugins = (
  ...     'Basic X-Challenge Plugin',
  ...     'Advanced X-Challenge Plugin')

and call 'unauthorized'::

  >>> request = TestRequest()
  >>> pau.unauthorized(None, request)

we see that both plugins participate in the challange, rather than just the
first plugin::

  >>> request.response.getHeader('X-Challenge')
  'advanced basic'


Pluggable-Authentication Prefixes
---------------------------------

Principal ids are required to be unique system wide. Plugins will often provide
options for providing id prefixes, so that different sets of plugins provide
unique ids within a PAU. If there are multiple pluggable-authentication
utilities in a system, it's a good idea to give each PAU a unique prefix, so
that principal ids from different PAUs don't conflict. We can provide a prefix
when a PAU is created::

  >>> pau = authentication.PluggableAuthentication('mypau_')
  >>> pau.credentialsPlugins = ('My Credentials Plugin', )
  >>> pau.authenticatorPlugins = ('My Authenticator Plugin', )

When we create a request and try to authenticate::

  >>> pau.authenticate(TestRequest(credentials='secretcode'))
  Principal('mypau_bob')

Note that now, our principal's id has the pluggable-authentication
utility prefix.

We can still lookup a principal, as long as we supply the prefix::

  >> pau.getPrincipal('mypas_42')
  Principal('mypas_42', "{'domain': 42}")

  >> pau.getPrincipal('mypas_41')
  OddPrincipal('mypas_41', "{'int': 41}")