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
|
====================
Address registration
====================
Before users can join a mailing list, they must first register with Mailman.
The only thing they must supply is an email address, although there is
additional information they may supply. All registered email addresses must
be verified before Mailman will send them any list traffic.
The ``IUserManager`` manages users, but it does so at a fairly low level.
Specifically, it does not handle verifications, email address syntax validity
checks, etc. The ``IRegistrar`` is the interface to the object handling all
this stuff.
>>> from mailman.interfaces.registrar import IRegistrar
>>> from zope.component import getUtility
>>> registrar = getUtility(IRegistrar)
Here is a helper function to check the token strings.
>>> def check_token(token):
... assert isinstance(token, basestring), 'Not a string'
... assert len(token) == 40, 'Unexpected length: %d' % len(token)
... assert token.isalnum(), 'Not alphanumeric'
... print 'ok'
Here is a helper function to extract tokens from confirmation messages.
>>> import re
>>> cre = re.compile('http://lists.example.com/confirm/(.*)')
>>> def extract_token(msg):
... mo = cre.search(msg.get_payload())
... return mo.group(1)
Invalid email addresses
=======================
Addresses are registered within the context of a mailing list, mostly so that
confirmation emails can come from some place. You also need the email
address of the user who is registering.
>>> mlist = create_list('alpha@example.com')
Some amount of sanity checks are performed on the email address, although
honestly, not as much as probably should be done. Still, some patently bad
addresses are rejected outright.
>>> registrar.register(mlist, '')
Traceback (most recent call last):
...
InvalidEmailAddressError: u''
>>> registrar.register(mlist, 'some name@example.com')
Traceback (most recent call last):
...
InvalidEmailAddressError: u'some name@example.com'
>>> registrar.register(mlist, '<script>@example.com')
Traceback (most recent call last):
...
InvalidEmailAddressError: u'<script>@example.com'
>>> registrar.register(mlist, '\xa0@example.com')
Traceback (most recent call last):
...
InvalidEmailAddressError: u'\xa0@example.com'
>>> registrar.register(mlist, 'noatsign')
Traceback (most recent call last):
...
InvalidEmailAddressError: u'noatsign'
>>> registrar.register(mlist, 'nodom@ain')
Traceback (most recent call last):
...
InvalidEmailAddressError: u'nodom@ain'
Register an email address
=========================
Registration of an unknown address creates nothing until the confirmation step
is complete. No ``IUser`` or ``IAddress`` is created at registration time,
but a record is added to the pending database, and the token for that record
is returned.
>>> token = registrar.register(mlist, 'aperson@example.com', 'Anne Person')
>>> check_token(token)
ok
There should be no records in the user manager for this address yet.
>>> from mailman.interfaces.usermanager import IUserManager
>>> from zope.component import getUtility
>>> user_manager = getUtility(IUserManager)
>>> print user_manager.get_user('aperson@example.com')
None
>>> print user_manager.get_address('aperson@example.com')
None
But this address is waiting for confirmation.
>>> from mailman.interfaces.pending import IPendings
>>> pendingdb = getUtility(IPendings)
>>> dump_msgdata(pendingdb.confirm(token, expunge=False))
address : aperson@example.com
list_name: alpha@example.com
real_name: Anne Person
type : registration
Verification by email
=====================
There is also a verification email sitting in the virgin queue now. This
message is sent to the user in order to verify the registered address.
>>> from mailman.testing.helpers import get_queue_messages
>>> items = get_queue_messages('virgin')
>>> len(items)
1
>>> print items[0].msg.as_string()
MIME-Version: 1.0
...
Subject: confirm ...
From: alpha-confirm+...@example.com
To: aperson@example.com
...
<BLANKLINE>
Email Address Registration Confirmation
<BLANKLINE>
Hello, this is the GNU Mailman server at example.com.
<BLANKLINE>
We have received a registration request for the email address
<BLANKLINE>
aperson@example.com
<BLANKLINE>
Before you can start using GNU Mailman at this site, you must first
confirm that this is your email address. You can do this by replying to
this message, keeping the Subject header intact. Or you can visit this
web page
<BLANKLINE>
http://lists.example.com/confirm/...
<BLANKLINE>
If you do not wish to register this email address simply disregard this
message. If you think you are being maliciously subscribed to the list,
or have any other questions, you may contact
<BLANKLINE>
postmaster@example.com
<BLANKLINE>
>>> dump_msgdata(items[0].msgdata)
_parsemsg : False
listname : alpha@example.com
nodecorate : True
recipients : [u'aperson@example.com']
reduced_list_headers: True
version : 3
The confirmation token shows up in several places, each of which provides an
easy way for the user to complete the confirmation. The token will always
appear in a URL in the body of the message.
>>> sent_token = extract_token(items[0].msg)
>>> sent_token == token
True
The same token will appear in the ``From`` header.
>>> items[0].msg['from'] == 'alpha-confirm+' + token + '@example.com'
True
It will also appear in the ``Subject`` header.
>>> items[0].msg['subject'] == 'confirm ' + token
True
The user would then validate their registered address by clicking on a url or
responding to the message. Either way, the confirmation process extracts the
token and uses that to confirm the pending registration.
>>> registrar.confirm(token)
True
Now, there is an ``IAddress`` in the database matching the address, as well as
an ``IUser`` linked to this address. The ``IAddress`` is verified.
>>> found_address = user_manager.get_address('aperson@example.com')
>>> found_address
<Address: Anne Person <aperson@example.com> [verified] at ...>
>>> found_user = user_manager.get_user('aperson@example.com')
>>> found_user
<User "Anne Person" at ...>
>>> found_user.controls(found_address.address)
True
>>> from datetime import datetime
>>> isinstance(found_address.verified_on, datetime)
True
Non-standard registrations
==========================
If you try to confirm a registration token twice, of course only the first one
will work. The second one is ignored.
>>> token = registrar.register(mlist, 'bperson@example.com')
>>> check_token(token)
ok
>>> items = get_queue_messages('virgin')
>>> len(items)
1
>>> sent_token = extract_token(items[0].msg)
>>> token == sent_token
True
>>> registrar.confirm(token)
True
>>> registrar.confirm(token)
False
If an address is in the system, but that address is not linked to a user yet
and the address is not yet validated, then no user is created until the
confirmation step is completed.
>>> user_manager.create_address('cperson@example.com')
<Address: cperson@example.com [not verified] at ...>
>>> token = registrar.register(
... mlist, 'cperson@example.com', 'Claire Person')
>>> print user_manager.get_user('cperson@example.com')
None
>>> items = get_queue_messages('virgin')
>>> len(items)
1
>>> sent_token = extract_token(items[0].msg)
>>> registrar.confirm(sent_token)
True
>>> user_manager.get_user('cperson@example.com')
<User "Claire Person" at ...>
>>> user_manager.get_address('cperson@example.com')
<Address: cperson@example.com [verified] at ...>
Even if the address being registered has already been verified, the
registration sends a confirmation.
>>> token = registrar.register(mlist, 'cperson@example.com')
>>> token is not None
True
Discarding
==========
A confirmation token can also be discarded, say if the user changes his or her
mind about registering. When discarded, no ``IAddress`` or ``IUser`` is
created.
::
>>> token = registrar.register(mlist, 'eperson@example.com', 'Elly Person')
>>> check_token(token)
ok
>>> registrar.discard(token)
>>> print pendingdb.confirm(token)
None
>>> print user_manager.get_address('eperson@example.com')
None
>>> print user_manager.get_user('eperson@example.com')
None
# Clear the virgin queue of all the preceding confirmation messages.
>>> ignore = get_queue_messages('virgin')
Registering a new address for an existing user
==============================================
When a new address for an existing user is registered, there isn't too much
different except that the new address will still need to be verified before it
can be used.
::
>>> dperson = user_manager.create_user(
... 'dperson@example.com', 'Dave Person')
>>> dperson
<User "Dave Person" at ...>
>>> address = user_manager.get_address('dperson@example.com')
>>> address.verified_on = datetime.now()
>>> from operator import attrgetter
>>> sorted((addr for addr in dperson.addresses), key=attrgetter('address'))
[<Address: Dave Person <dperson@example.com> [verified] at ...>]
>>> dperson.register('david.person@example.com', 'David Person')
<Address: David Person <david.person@example.com> [not verified] at ...>
>>> token = registrar.register(mlist, 'david.person@example.com')
>>> items = get_queue_messages('virgin')
>>> len(items)
1
>>> sent_token = extract_token(items[0].msg)
>>> registrar.confirm(sent_token)
True
>>> user = user_manager.get_user('david.person@example.com')
>>> user is dperson
True
>>> user
<User "Dave Person" at ...>
>>> sorted((addr for addr in user.addresses), key=attrgetter('address'))
[<Address: David Person <david.person@example.com> [verified] at ...>,
<Address: Dave Person <dperson@example.com> [verified] at ...>]
Corner cases
============
If you try to confirm a token that doesn't exist in the pending database, the
confirm method will just return False.
>>> registrar.confirm(bytes('no token'))
False
Likewise, if you try to confirm, through the ``IUserRegistrar`` interface, a
token that doesn't match a registration event, you will get ``None``.
However, the pending event matched with that token will still be removed.
>>> from mailman.interfaces.pending import IPendable
>>> from zope.interface import implements
>>> class SimplePendable(dict):
... implements(IPendable)
>>> pendable = SimplePendable(type='foo', bar='baz')
>>> token = pendingdb.add(pendable)
>>> registrar.confirm(token)
False
>>> print pendingdb.confirm(token)
None
Registration and subscription
=============================
Fred registers with Mailman at the same time that he subscribes to a mailing
list.
>>> token = registrar.register(
... mlist, 'fred.person@example.com', 'Fred Person')
Before confirmation, Fred is not a member of the mailing list.
>>> print mlist.members.get_member('fred.person@example.com')
None
But after confirmation, he is.
>>> registrar.confirm(token)
True
>>> print mlist.members.get_member('fred.person@example.com')
<Member: Fred Person <fred.person@example.com>
on alpha@example.com as MemberRole.member>
|