def _show_authorize_link(provider_module, email_address=None): """ Show authorization link. Prints out a message to the console containing a link that the user can click on that will bring them to a page that allows them to authorize access to their account. """ try: redirect_uri = provider_module.OAUTH_REDIRECT_URI client_id = provider_module.OAUTH_CLIENT_ID scope = provider_module.OAUTH_SCOPE authenticate_url = provider_module.OAUTH_AUTHENTICATE_URL except AttributeError: log.error("Provider module must define OAUTH_REDIRECT_URI, " "OAUTH_CLIENT_ID, OAUTH_SCOPE and OAUTH_AUTHENTICATE_URL") raise args = { 'redirect_uri': redirect_uri, 'client_id': client_id, 'response_type': 'code', 'scope': scope, 'access_type': 'offline', # to get a refresh token } # TODO: Is there some way to make this generic? Outlook doesn't # accept a login_hint. -cg3 if provider_module.PROVIDER == 'gmail' and email_address: args['login_hint'] = email_address # Prompt user for authorization + get auth_code url = url_concat(authenticate_url, args) print("\n\n{}").format(url)
def test_empty_response_when_latest_cursor_given(db, api_client, default_namespace): cursor = get_cursor(api_client, int(time.time() + 22), default_namespace) url = url_concat('/delta/streaming', {'timeout': .1, 'cursor': cursor}) r = api_client.get_raw(url) assert r.status_code == 200 assert r.data.strip() == ''
def interactive_auth(self, email_address=None): url_args = { 'redirect_uri': self.OAUTH_REDIRECT_URI, 'client_id': self.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': self.OAUTH_SCOPE, 'access_type': 'offline' } url = url_concat(self.OAUTH_AUTHENTICATE_URL, url_args) print( 'Please visit the following url to allow access to this ' 'application. The response will provide ' 'code=[AUTHORIZATION_CODE]&lc=XXXX in the location. Paste the' ' AUTHORIZATION_CODE here:') print '\n{}'.format(url) while True: auth_code = raw_input('Enter authorization code: ').strip() try: auth_response = self._get_authenticated_user(auth_code) return auth_response except OAuthError: print '\nInvalid authorization code, try again...\n' auth_code = None
def interactive_auth(self, email_address=None): url_args = { 'redirect_uri': self.OAUTH_REDIRECT_URI, 'client_id': self.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': self.OAUTH_SCOPE, 'access_type': 'offline' } if email_address: url_args['login_hint'] = email_address url = url_concat(self.OAUTH_AUTHENTICATE_URL, url_args) print 'To authorize Inbox, visit this URL and follow the directions:' print '\n{}'.format(url) while True: auth_code = raw_input('Enter authorization code: ').strip() try: auth_response = self._get_authenticated_user(auth_code) auth_response['contacts'] = True auth_response['events'] = True return auth_response except OAuthError: print "\nInvalid authorization code, try again...\n" auth_code = None
def test_empty_response_when_latest_cursor_given(db, api_client, default_namespace): cursor = get_cursor(api_client, int(time.time() + 22), default_namespace) url = url_concat("/delta/streaming", {"timeout": 0.1, "cursor": cursor}) r = api_client.get_raw(url) assert r.status_code == 200 assert r.get_data(as_text=True).strip() == ""
def _show_authorize_link(provider_module, email_address=None): """ Show authorization link. Prints out a message to the console containing a link that the user can click on that will bring them to a page that allows them to authorize access to their account. """ try: redirect_uri = provider_module.OAUTH_REDIRECT_URI client_id = provider_module.OAUTH_CLIENT_ID scope = provider_module.OAUTH_SCOPE authenticate_url = provider_module.OAUTH_AUTHENTICATE_URL except AttributeError: log.error("Provider module must define OAUTH_REDIRECT_URI, " "OAUTH_CLIENT_ID, OAUTH_SCOPE and OAUTH_AUTHENTICATE_URL") raise args = { 'redirect_uri': redirect_uri, 'client_id': client_id, 'response_type': 'code', 'scope': scope, 'access_type': 'offline', # to get a refresh token } # TODO: Is there some way to make this generic? Outlook doesn't # accept a login_hint. -cg3 if provider_module.PROVIDER == 'gmail' and email_address: args['login_hint'] = email_address # Prompt user for authorization + get auth_code url = url_concat(authenticate_url, args) print ("\n\n{}").format(url)
def interactive_auth(self, email_address=None): url_args = { "redirect_uri": self.OAUTH_REDIRECT_URI, "client_id": self.OAUTH_CLIENT_ID, "response_type": "code", "scope": self.OAUTH_SCOPE, "prompt": "select_account", } if email_address: url_args["login_hint"] = email_address url = url_concat(self.OAUTH_AUTHENTICATE_URL, url_args) print("To authorize Nylas, visit this URL and follow the directions:") print("\n{}".format(url)) while True: auth_code = input("Enter authorization code: ").strip() try: auth_response = self._get_authenticated_user(auth_code) return MicrosoftAccountData( email=auth_response["email"], secret_type=SecretType.Token, secret_value=auth_response["refresh_token"], client_id=self.OAUTH_CLIENT_ID, scope=auth_response["scope"], sync_email=True, ) except OAuthError: print("\nInvalid authorization code, try again...\n")
def test_response_when_old_cursor_given(db, api_client, default_namespace): url = url_concat('/delta/streaming', {'timeout': .1, 'cursor': '0'}) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.data.split('\n') for response_string in responses: if response_string: validate_response_format(response_string)
def test_response_when_old_cursor_given(db, api_client, default_namespace): url = url_concat("/delta/streaming", {"timeout": 0.1, "cursor": "0"}) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.get_data(as_text=True).split("\n") for response_string in responses: if response_string: validate_response_format(response_string)
def test_empty_response_when_latest_cursor_given(db, api_prefix, streaming_test_client, default_namespace): cursor = get_cursor(streaming_test_client, int(time.time()), default_namespace) url = url_concat(api_prefix, {'timeout': .1, 'cursor': cursor}) r = streaming_test_client.get(url) assert r.status_code == 200 assert r.data == ''
def test_gracefully_handle_no_transactions(db, streaming_test_client): new_namespace = Namespace() db.session.add(new_namespace) db.session.commit() url = url_concat('/n/{}/delta/streaming'.format(new_namespace.public_id), {'timeout': .1}) r = streaming_test_client.get(url) assert r.status_code == 200 assert r.data == ''
def test_response_when_cursor_given(db, api_prefix, streaming_test_client, default_namespace): old_transaction = db.session.query(Transaction).filter( Transaction.namespace_id == default_namespace.id). \ order_by(asc(Transaction.id)).first() url = url_concat(api_prefix, {'timeout': .1, 'cursor': old_transaction.public_id}) r = streaming_test_client.get(url) assert r.status_code == 200 assert json.loads(r.data) == {'namespace_id': default_namespace.public_id}
def test_gracefully_handle_new_namespace(db, streaming_test_client): new_namespace = Namespace() db.session.add(new_namespace) db.session.commit() cursor = get_cursor(streaming_test_client, int(time.time()), new_namespace) url = url_concat('/n/{}/delta/streaming'.format(new_namespace.public_id), {'timeout': .1, 'cursor': cursor}) r = streaming_test_client.get(url) assert r.status_code == 200
def test_exclude_and_include_object_types(db, api_client, thread, default_namespace): add_fake_message(db.session, default_namespace.id, thread, from_addr=[("Bob", "*****@*****.**")]) # Check that we do get message and contact changes by default. url = url_concat("/delta/streaming", {"timeout": 0.1, "cursor": "0"}) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.get_data(as_text=True).split("\n") parsed_responses = [json.loads(resp) for resp in responses if resp != ""] assert any(resp["object"] == "message" for resp in parsed_responses) assert any(resp["object"] == "contact" for resp in parsed_responses) # And check that we don't get message/contact changes if we exclude them. url = url_concat( "/delta/streaming", { "timeout": 0.1, "cursor": "0", "exclude_types": "message,contact" }, ) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.get_data(as_text=True).split("\n") parsed_responses = [json.loads(resp) for resp in responses if resp != ""] assert not any(resp["object"] == "message" for resp in parsed_responses) assert not any(resp["object"] == "contact" for resp in parsed_responses) # And check we only get message objects if we use include_types url = url_concat("/delta/streaming", { "timeout": 0.1, "cursor": "0", "include_types": "message" }) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.get_data(as_text=True).split("\n") parsed_responses = [json.loads(resp) for resp in responses if resp != ""] assert all(resp["object"] == "message" for resp in parsed_responses)
def test_empty_response_when_latest_cursor_given(db, api_prefix, streaming_test_client, default_namespace): newest_transaction = db.session.query(Transaction).filter( Transaction.namespace_id == default_namespace.id). \ order_by(desc(Transaction.id)).first() url = url_concat(api_prefix, {'timeout': .1, 'cursor': newest_transaction.public_id}) r = streaming_test_client.get(url) assert r.status_code == 200 assert r.data == ''
def test_gracefully_handle_new_namespace(db, streaming_test_client): new_namespace = Namespace() db.session.add(new_namespace) db.session.commit() cursor = get_cursor(streaming_test_client, int(time.time()), new_namespace) url = url_concat('/n/{}/delta/streaming'.format(new_namespace.public_id), { 'timeout': .1, 'cursor': cursor }) r = streaming_test_client.get(url) assert r.status_code == 200
def test_exclude_and_include_object_types(db, api_client, thread, default_namespace): add_fake_message( db.session, default_namespace.id, thread, from_addr=[('Bob', '*****@*****.**')]) # Check that we do get message and contact changes by default. url = url_concat('/delta/streaming', {'timeout': .1, 'cursor': '0'}) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert any(resp['object'] == 'message' for resp in parsed_responses) assert any(resp['object'] == 'contact' for resp in parsed_responses) # And check that we don't get message/contact changes if we exclude them. url = url_concat('/delta/streaming', { 'timeout': .1, 'cursor': '0', 'exclude_types': 'message,contact' }) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert not any(resp['object'] == 'message' for resp in parsed_responses) assert not any(resp['object'] == 'contact' for resp in parsed_responses) # And check we only get message objects if we use include_types url = url_concat('/delta/streaming', { 'timeout': .1, 'cursor': '0', 'include_types': 'message' }) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert all(resp['object'] == 'message' for resp in parsed_responses)
def test_exclude_and_include_object_types(db, api_prefix, streaming_test_client, thread, default_namespace): add_fake_message(db.session, default_namespace.id, thread, from_addr=[('Bob', '*****@*****.**')]) # Check that we do get message and contact changes by default. url = url_concat(api_prefix, {'timeout': .1, 'cursor': '0'}) r = streaming_test_client.get(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert any(resp['object'] == 'message' for resp in parsed_responses) assert any(resp['object'] == 'contact' for resp in parsed_responses) # And check that we don't get message/contact changes if we exclude them. url = url_concat(api_prefix, { 'timeout': .1, 'cursor': '0', 'exclude_types': 'message,contact' }) r = streaming_test_client.get(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert not any(resp['object'] == 'message' for resp in parsed_responses) assert not any(resp['object'] == 'contact' for resp in parsed_responses) # And check we only get message objects if we use include_types url = url_concat(api_prefix, { 'timeout': .1, 'cursor': '0', 'include_types': 'message' }) r = streaming_test_client.get(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert all(resp['object'] == 'message' for resp in parsed_responses)
def test_exclude_object_types(db, api_prefix, streaming_test_client): # Check that we do get message and contact changes by default. url = url_concat(api_prefix, {'timeout': .1, 'cursor': '0'}) r = streaming_test_client.get(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert any(resp['object'] == 'message' for resp in parsed_responses) assert any(resp['object'] == 'contact' for resp in parsed_responses) # And check that we don't get message/contact changes if we exclude them. url = url_concat(api_prefix, {'timeout': .1, 'cursor': '0', 'exclude_types': 'message,contact'}) r = streaming_test_client.get(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert not any(resp['object'] == 'message' for resp in parsed_responses) assert not any(resp['object'] == 'contact' for resp in parsed_responses)
def test_exclude_object_types(db, api_prefix, streaming_test_client): # Check that we do get message and contact changes by default. url = url_concat(api_prefix, {'timeout': .1, 'cursor': '0'}) r = streaming_test_client.get(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert any(resp['object'] == 'message' for resp in parsed_responses) assert any(resp['object'] == 'contact' for resp in parsed_responses) # And check that we don't get message/contact changes if we exclude them. url = url_concat(api_prefix, { 'timeout': .1, 'cursor': '0', 'exclude_types': 'message,contact' }) r = streaming_test_client.get(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] assert not any(resp['object'] == 'message' for resp in parsed_responses) assert not any(resp['object'] == 'contact' for resp in parsed_responses)
def test_expanded_view(db, api_client, thread, message, default_namespace): url = url_concat('/delta/streaming', {'timeout': .1, 'cursor': '0', 'include_types': 'message,thread', 'view': 'expanded'}) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.data.split('\n') parsed_responses = [json.loads(resp) for resp in responses if resp != ''] for delta in parsed_responses: if delta['object'] == 'message': assert 'headers' in delta['attributes'] elif delta['object'] == 'thread': assert 'messages' in delta['attributes']
def get_oauth_url(self, email_address=None): url_args = { 'redirect_uri': self.OAUTH_REDIRECT_URI, 'client_id': self.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': self.OAUTH_SCOPE, 'access_type': 'offline' } if email_address: url_args['login_hint'] = email_address url = url_concat(self.OAUTH_AUTHENTICATE_URL, url_args) return url
def test_gracefully_handle_new_namespace(db, api_client): new_namespace = Namespace() new_account = GenericAccount() new_account.password = '******' new_namespace.account = new_account db.session.add(new_namespace) db.session.add(new_account) db.session.commit() cursor = get_cursor(api_client, int(time.time()), new_namespace) url = url_concat('/n/{}/delta/streaming'.format(new_namespace.public_id), {'timeout': .1, 'cursor': cursor}) r = api_client.get_raw(url) assert r.status_code == 200
def test_longpoll_delta_timeout(db, api_client, default_namespace): test_timeout = 2 cursor = get_cursor(api_client, int(time.time() + 22), default_namespace) url = url_concat('/delta/longpoll', {'timeout': test_timeout, 'cursor': cursor}) start_time = time.time() resp = api_client.get_raw(url) end_time = time.time() assert resp.status_code == 200 assert end_time - start_time - test_timeout < GEVENT_EPSILON parsed_responses = json.loads(resp.data) assert len(parsed_responses['deltas']) == 0 assert type(parsed_responses['deltas']) == list assert parsed_responses['cursor_start'] == cursor assert parsed_responses['cursor_end'] == cursor
def test_longpoll_delta_newitem(db, api_client, default_namespace, thread): cursor = get_cursor(api_client, int(time.time() + 22), default_namespace) url = url_concat("/delta/longpoll", {"cursor": cursor}) start_time = time.time() # Spawn the request in background greenlet longpoll_greenlet = Greenlet.spawn(api_client.get_raw, url) # This should make it return immediately add_fake_message(db.session, default_namespace.id, thread, from_addr=[("Bob", "*****@*****.**")]) longpoll_greenlet.join() # now block and wait end_time = time.time() assert end_time - start_time < LONGPOLL_EPSILON parsed_responses = json.loads(longpoll_greenlet.value.data) assert len(parsed_responses["deltas"]) == 3 assert set(k["object"] for k in parsed_responses["deltas"]) == set( [u"message", u"contact", u"thread"])
def test_longpoll_delta_timeout(db, api_client, default_namespace): test_timeout = 2 cursor = get_cursor(api_client, int(time.time() + 22), default_namespace) url = url_concat("/delta/longpoll", { "timeout": test_timeout, "cursor": cursor }) start_time = time.time() resp = api_client.get_raw(url) end_time = time.time() assert resp.status_code == 200 assert end_time - start_time - test_timeout < GEVENT_EPSILON parsed_responses = json.loads(resp.data) assert len(parsed_responses["deltas"]) == 0 assert type(parsed_responses["deltas"]) == list assert parsed_responses["cursor_start"] == cursor assert parsed_responses["cursor_end"] == cursor
def test_longpoll_delta_newitem(db, api_client, default_namespace, thread): cursor = get_cursor(api_client, int(time.time() + 22), default_namespace) url = url_concat('/delta/longpoll', {'cursor': cursor}) start_time = time.time() # Spawn the request in background greenlet longpoll_greenlet = Greenlet.spawn(api_client.get_raw, url) # This should make it return immediately add_fake_message(db.session, default_namespace.id, thread, from_addr=[('Bob', '*****@*****.**')]) longpoll_greenlet.join() # now block and wait end_time = time.time() assert end_time - start_time < LONGPOLL_EPSILON parsed_responses = json.loads(longpoll_greenlet.value.data) assert len(parsed_responses['deltas']) == 3 assert set(k['object'] for k in parsed_responses['deltas']) == \ set([u'message', u'contact', u'thread'])
def test_expanded_view(db, api_client, thread, message, default_namespace): url = url_concat( "/delta/streaming", { "timeout": 0.1, "cursor": "0", "include_types": "message,thread", "view": "expanded", }, ) r = api_client.get_raw(url) assert r.status_code == 200 responses = r.get_data(as_text=True).split("\n") parsed_responses = [json.loads(resp) for resp in responses if resp != ""] for delta in parsed_responses: if delta["object"] == "message": assert "headers" in delta["attributes"] elif delta["object"] == "thread": assert "messages" in delta["attributes"]
def test_longpoll_delta_newitem(db, longpoll_prefix, streaming_test_client, default_namespace, thread): cursor = get_cursor(streaming_test_client, int(time.time() + 22), default_namespace) url = url_concat(longpoll_prefix, {'cursor': cursor}) start_time = time.time() # Spawn the request in background greenlet longpoll_greenlet = Greenlet.spawn(streaming_test_client.get, url) # This should make it return immediately add_fake_message(db.session, default_namespace.id, thread, from_addr=[('Bob', '*****@*****.**')]) longpoll_greenlet.join() # now block and wait end_time = time.time() assert end_time - start_time < LONGPOLL_EPSILON parsed_responses = json.loads(longpoll_greenlet.value.data) assert len(parsed_responses['deltas']) == 3 assert set(k['object'] for k in parsed_responses['deltas']) == \ set([u'message', u'contact', u'thread'])
def test_longpoll_delta_timeout(db, longpoll_prefix, streaming_test_client, default_namespace): test_timeout = 2 cursor = get_cursor(streaming_test_client, int(time.time() + 22), default_namespace) url = url_concat(longpoll_prefix, { 'timeout': test_timeout, 'cursor': cursor }) start_time = time.time() resp = streaming_test_client.get(url) end_time = time.time() assert resp.status_code == 200 assert end_time - start_time - test_timeout < GEVENT_EPSILON parsed_responses = json.loads(resp.data) assert len(parsed_responses['deltas']) == 0 assert type(parsed_responses['deltas']) == list assert parsed_responses['cursor_start'] == cursor assert parsed_responses['cursor_end'] == cursor
def interactive_auth(self, email_address=None): url_args = {'redirect_uri': self.OAUTH_REDIRECT_URI, 'client_id': self.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': self.OAUTH_SCOPE, 'access_type': 'offline'} if email_address: url_args['login_hint'] = email_address url = url_concat(self.OAUTH_AUTHENTICATE_URL, url_args) print 'To authorize Inbox, visit this URL and follow the directions:' print '\n{}'.format(url) while True: auth_code = raw_input('Enter authorization code: ').strip() try: auth_response = self._get_authenticated_user(auth_code) return auth_response except OAuthError: print "\nInvalid authorization code, try again...\n" auth_code = None
def google_auth(email, password): session = requests.Session() url_args = { "redirect_uri": GmailAuthHandler.OAUTH_REDIRECT_URI, "client_id": GmailAuthHandler.OAUTH_CLIENT_ID, "response_type": "code", "scope": GmailAuthHandler.OAUTH_SCOPE, "access_type": "offline", "login_hint": email, } url = url_concat(GmailAuthHandler.OAUTH_AUTHENTICATE_URL, url_args) req = session.get(url) assert req.ok auth_parser = GoogleAuthParser() auth_parser.feed(req.text) params = auth_parser.params action = auth_parser.action params["Email"] = email params["Passwd"] = password req = session.post(action, data=params) assert req.ok connect_parser = GoogleConnectParser() connect_parser.feed(req.text) params = connect_parser.params action = connect_parser.action params["submit_access"] = "true" req = session.post(action, data=params) assert req.ok token_parser = GoogleTokenParser() token_parser.feed(req.text) return token_parser.code
def google_auth(email, password): session = requests.Session() url_args = { 'redirect_uri': GmailAuthHandler.OAUTH_REDIRECT_URI, 'client_id': GmailAuthHandler.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': GmailAuthHandler.OAUTH_SCOPE, 'access_type': 'offline', 'login_hint': email } url = url_concat(GmailAuthHandler.OAUTH_AUTHENTICATE_URL, url_args) req = session.get(url) assert req.ok auth_parser = GoogleAuthParser() auth_parser.feed(req.text) params = auth_parser.params action = auth_parser.action params['Email'] = email params['Passwd'] = password req = session.post(action, data=params) assert req.ok connect_parser = GoogleConnectParser() connect_parser.feed(req.text) params = connect_parser.params action = connect_parser.action params['submit_access'] = 'true' req = session.post(action, data=params) assert req.ok token_parser = GoogleTokenParser() token_parser.feed(req.text) return token_parser.code
def _show_authorize_link(email_address=None): """ Show authorization link. Prints out a message to the console containing a link that the user can click on that will bring them to a page that allows them to authorize access to their account. """ args = { 'redirect_uri': REDIRECT_URI, 'client_id': GOOGLE_OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': OAUTH_SCOPE, 'access_type': 'offline', # to get a refresh token } if email_address: args['login_hint'] = email_address # DEBUG args['approval_prompt'] = 'force' # Prompt user for authorization + get auth_code url = url_concat(OAUTH_AUTHENTICATE_URL, args) print ("To authorize Inbox, visit this url and follow the directions:" "\n\n{}").format(url)
def interactive_auth(self, email_address=None): url_args = {'redirect_uri': self.OAUTH_REDIRECT_URI, 'client_id': self.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': self.OAUTH_SCOPE, 'access_type': 'offline'} url = url_concat(self.OAUTH_AUTHENTICATE_URL, url_args) print ('Please visit the following url to allow access to this ' 'application. The response will provide ' 'code=[AUTHORIZATION_CODE]&lc=XXXX in the location. Paste the' ' AUTHORIZATION_CODE here:') print '\n{}'.format(url) while True: auth_code = raw_input('Enter authorization code: ').strip() try: auth_response = self._get_authenticated_user(auth_code) return auth_response except OAuthError: print '\nInvalid authorization code, try again...\n' auth_code = None
def outlook_auth(email, password): session = requests.Session() url_args = { 'redirect_uri': OutlookAuthHandler.OAUTH_REDIRECT_URI, 'client_id': OutlookAuthHandler.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': OutlookAuthHandler.OAUTH_SCOPE, 'access_type': 'offline', 'login_hint': email } url = url_concat(OutlookAuthHandler.OAUTH_AUTHENTICATE_URL, url_args) req = session.get(url) assert req.ok auth_parser = OutlookAuthParser() auth_parser.feed(req.text) params = auth_parser.params params['login'] = email params['passwd'] = password req = session.post(auth_parser.action, data=params) assert req.ok update_parser = OutlookUpdateParser() update_parser.feed(req.text) req = session.post(update_parser.action, data=update_parser.params) assert req.ok consent_parser = OutlookConsentParser() consent_parser.feed(req.text) req = session.post(update_parser.action, data=consent_parser.params) assert req.ok code = re.match('https.*code=(.*)&lc=1033', req.url).group(1) return code
def google_auth(email, password): session = requests.Session() url_args = {'redirect_uri': GmailAuthHandler.OAUTH_REDIRECT_URI, 'client_id': GmailAuthHandler.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': GmailAuthHandler.OAUTH_SCOPE, 'access_type': 'offline', 'login_hint': email} url = url_concat(GmailAuthHandler.OAUTH_AUTHENTICATE_URL, url_args) req = session.get(url) assert req.ok auth_parser = GoogleAuthParser() auth_parser.feed(req.text) params = auth_parser.params action = auth_parser.action params['Email'] = email params['Passwd'] = password req = session.post(action, data=params) assert req.ok connect_parser = GoogleConnectParser() connect_parser.feed(req.text) params = connect_parser.params action = connect_parser.action params['submit_access'] = 'true' req = session.post(action, data=params) assert req.ok token_parser = GoogleTokenParser() token_parser.feed(req.text) return token_parser.code
def outlook_auth(email, password): session = requests.Session() url_args = { "redirect_uri": OutlookAuthHandler.OAUTH_REDIRECT_URI, "client_id": OutlookAuthHandler.OAUTH_CLIENT_ID, "response_type": "code", "scope": OutlookAuthHandler.OAUTH_SCOPE, "access_type": "offline", "login_hint": email, } url = url_concat(OutlookAuthHandler.OAUTH_AUTHENTICATE_URL, url_args) req = session.get(url) assert req.ok auth_parser = OutlookAuthParser() auth_parser.feed(req.text) params = auth_parser.params params["login"] = email params["passwd"] = password req = session.post(auth_parser.action, data=params) assert req.ok update_parser = OutlookUpdateParser() update_parser.feed(req.text) req = session.post(update_parser.action, data=update_parser.params) assert req.ok consent_parser = OutlookConsentParser() consent_parser.feed(req.text) req = session.post(update_parser.action, data=consent_parser.params) assert req.ok code = re.match("https.*code=(.*)&lc=1033", req.url).group(1) return code
def authorize_redirect_url(email_address=None): args = { 'redirect_uri': REDIRECT_URI, 'client_id': GOOGLE_OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': OAUTH_SCOPE, 'access_type': 'offline', # to get a refresh token } if email_address: args['login_hint'] = email_address # DEBUG args['approval_prompt'] = 'force' # Prompt user for authorization + get auth_code url = url_concat(OAUTH_AUTHENTICATE_URL, args) print ''' To authorize Inbox, visit this url and follow the directions: {0} '''.format(url) auth_code = raw_input('Enter authorization code: ').strip() return auth_code
def outlook_auth(email, password): session = requests.Session() url_args = {'redirect_uri': OutlookAuthHandler.OAUTH_REDIRECT_URI, 'client_id': OutlookAuthHandler.OAUTH_CLIENT_ID, 'response_type': 'code', 'scope': OutlookAuthHandler.OAUTH_SCOPE, 'access_type': 'offline', 'login_hint': email} url = url_concat(OutlookAuthHandler.OAUTH_AUTHENTICATE_URL, url_args) req = session.get(url) assert req.ok auth_parser = OutlookAuthParser() auth_parser.feed(req.text) params = auth_parser.params params['login'] = email params['passwd'] = password req = session.post(auth_parser.action, data=params) assert req.ok update_parser = OutlookUpdateParser() update_parser.feed(req.text) req = session.post(update_parser.action, data=update_parser.params) assert req.ok consent_parser = OutlookConsentParser() consent_parser.feed(req.text) req = session.post(update_parser.action, data=consent_parser.params) assert req.ok code = re.match('https.*code=(.*)&lc=1033', req.url).group(1) return code
def test_empty_response_when_no_changes(streaming_test_client, api_prefix): url = url_concat(api_prefix, {'timeout': .1}) r = streaming_test_client.get(url) assert r.status_code == 200 assert r.data == ''