def authenticate(self, url, consumer, token): if consumer is not None and not isinstance(consumer, oauth2.Consumer): raise ValueError("Invalid consumer.") if token is not None and not isinstance(token, oauth2.Token): raise ValueError("Invalid token.") imaplib.IMAP4_SSL.authenticate(self, 'XOAUTH', lambda x: oauth2.build_xoauth_string(url, consumer, token))
def authenticate(self, url, consumer, token): if consumer is not None and not isinstance(consumer, oauth2.Consumer): raise ValueError("Invalid consumer.") if token is not None and not isinstance(token, oauth2.Token): raise ValueError("Invalid token.") self.docmd('AUTH', 'XOAUTH %s' % \ base64.b64encode(oauth2.build_xoauth_string(url, consumer, token)))
def oauth_login(self, url, oauth_token, oauth_token_secret, consumer_key="anonymous", consumer_secret="anonymous"): """Authenticate using the OAUTH method. This only works with IMAP servers that support OAUTH (eg. Gmail). """ if oauth2: token = oauth2.Token(oauth_token, oauth_token_secret) consumer = oauth2.Consumer(consumer_key, consumer_secret) xoauth_callable = lambda x: oauth2.build_xoauth_string(url, consumer, token) return self._command_and_check("authenticate", "XOAUTH", xoauth_callable, unpack=True) else: raise self.Error("The optional oauth2 dependency is needed for oauth authentication")
def authenticate(self, url, consumer, token): if consumer is not None and not isinstance(consumer, oauth.Consumer): raise ValueError("Invalid consumer.") if token is not None and not isinstance(token, oauth.Token): raise ValueError("Invalid token.") xoauth_string = oauth.build_xoauth_string(url, consumer, token) code, resp = self.docmd("AUTH", "XOAUTH %s" % base64.b64encode(xoauth_string)) if code >= 500: raise smtplib.SMTPResponseException(code, resp) return code, resp
def authenticate(self, url, consumer, token): if consumer is not None and not isinstance(consumer, oauth.Consumer): raise ValueError("Invalid consumer.") if token is not None and not isinstance(token, oauth.Token): raise ValueError("Invalid token.") xoauth_string = oauth.build_xoauth_string(url, consumer, token) code, resp = self.docmd('AUTH', 'XOAUTH %s' % base64.b64encode(xoauth_string)) if code >= 500: raise smtplib.SMTPResponseException(code, resp) return code, resp
def oauth_login(self, url, oauth_token, oauth_token_secret, consumer_key='anonymous', consumer_secret='anonymous'): """Authenticate using the OAUTH method. This only works with IMAP servers that support OAUTH (e.g. Gmail). """ if oauth_module: token = oauth_module.Token(oauth_token, oauth_token_secret) consumer = oauth_module.Consumer(consumer_key, consumer_secret) xoauth_callable = lambda x: oauth_module.build_xoauth_string(url, consumer, token) return self._command_and_check('authenticate', 'XOAUTH', xoauth_callable, unpack=True) else: raise self.Error('The optional oauth2 package is needed for OAUTH authentication')
def test_build_xoauth_string(self): consumer = oauth.Consumer('consumer_token', 'consumer_secret') token = oauth.Token('user_token', 'user_secret') url = "https://mail.google.com/mail/b/[email protected]/imap/" xoauth_string = oauth.build_xoauth_string(url, consumer, token) method, oauth_url, oauth_string = xoauth_string.split(' ') self.assertEqual("GET", method) self.assertEqual(url, oauth_url) returned = {} parts = oauth_string.split(',') for part in parts: var, val = part.split('=') returned[var] = val.strip('"') self.assertEquals('HMAC-SHA1', returned['oauth_signature_method']) self.assertEquals('user_token', returned['oauth_token']) self.assertEquals('consumer_token', returned['oauth_consumer_key']) self.assertTrue('oauth_signature' in returned, 'oauth_signature')
def test_build_xoauth_string(self): consumer = oauth.Consumer("consumer_token", "consumer_secret") token = oauth.Token("user_token", "user_secret") url = "https://mail.google.com/mail/b/[email protected]/imap/" xoauth_string = oauth.build_xoauth_string(url, consumer, token) method, oauth_url, oauth_string = xoauth_string.split(" ") self.assertEqual("GET", method) self.assertEqual(url, oauth_url) returned = {} parts = oauth_string.split(",") for part in parts: var, val = part.split("=") returned[var] = val.strip('"') self.assertEquals("HMAC-SHA1", returned["oauth_signature_method"]) self.assertEquals("user_token", returned["oauth_token"]) self.assertEquals("consumer_token", returned["oauth_consumer_key"]) self.assertTrue("oauth_signature" in returned, "oauth_signature")
def oauth_login(self, url, oauth_token, oauth_token_secret, consumer_key='anonymous', consumer_secret='anonymous'): """Authenticate using the OAUTH method. This only works with IMAP servers that support OAUTH (e.g. Gmail). """ if oauth_module: token = oauth_module.Token(oauth_token, oauth_token_secret) consumer = oauth_module.Consumer(consumer_key, consumer_secret) xoauth_callable = lambda x: oauth_module.build_xoauth_string( url, consumer, token) return self._command_and_check('authenticate', 'XOAUTH', xoauth_callable, unpack=True) else: raise self.Error( 'The optional oauth2 package is needed for OAUTH authentication' )