p@21
|
1 """
|
p@21
|
2 The MIT License
|
p@21
|
3
|
p@21
|
4 Copyright (c) 2007-2010 Leah Culver, Joe Stump, Mark Paschal, Vic Fryzel
|
p@21
|
5
|
p@21
|
6 Permission is hereby granted, free of charge, to any person obtaining a copy
|
p@21
|
7 of this software and associated documentation files (the "Software"), to deal
|
p@21
|
8 in the Software without restriction, including without limitation the rights
|
p@21
|
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
p@21
|
10 copies of the Software, and to permit persons to whom the Software is
|
p@21
|
11 furnished to do so, subject to the following conditions:
|
p@21
|
12
|
p@21
|
13 The above copyright notice and this permission notice shall be included in
|
p@21
|
14 all copies or substantial portions of the Software.
|
p@21
|
15
|
p@21
|
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
p@21
|
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
p@21
|
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
p@21
|
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
p@21
|
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
p@21
|
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
p@21
|
22 THE SOFTWARE.
|
p@21
|
23 """
|
p@21
|
24
|
p@21
|
25 import oauth2
|
p@21
|
26 import smtplib
|
p@21
|
27 import base64
|
p@21
|
28
|
p@21
|
29
|
p@21
|
30 class SMTP(smtplib.SMTP):
|
p@21
|
31 """SMTP wrapper for smtplib.SMTP that implements XOAUTH."""
|
p@21
|
32
|
p@21
|
33 def authenticate(self, url, consumer, token):
|
p@21
|
34 if consumer is not None and not isinstance(consumer, oauth2.Consumer):
|
p@21
|
35 raise ValueError("Invalid consumer.")
|
p@21
|
36
|
p@21
|
37 if token is not None and not isinstance(token, oauth2.Token):
|
p@21
|
38 raise ValueError("Invalid token.")
|
p@21
|
39
|
p@21
|
40 self.docmd('AUTH', 'XOAUTH %s' % \
|
p@21
|
41 base64.b64encode(oauth2.build_xoauth_string(url, consumer, token)))
|