1
by Martin Mrazik
initial commit |
1 |
import sys |
2 |
import time |
|
3 |
from launchpadlib.credentials import RequestTokenAuthorizationEngine |
|
4 |
from lazr.restfulclient.errors import HTTPError |
|
5 |
||
6 |
||
7 |
ACCESS_TOKEN_POLL_TIME = 1 |
|
8 |
WAITING_FOR_USER = """Open this link: |
|
9 |
{}
|
|
10 |
to authorize this program to access Launchpad on your behalf.
|
|
11 |
Waiting to hear from Launchpad about your decision. . . ."""
|
|
12 |
||
13 |
||
14 |
class AuthorizeRequestTokenWithConsole(RequestTokenAuthorizationEngine): |
|
15 |
"""Authorize a token in a server environment (with no browser).
|
|
16 |
||
17 |
Print a link for the user to copy-and-paste into his/her browser
|
|
18 |
for authentication.
|
|
19 |
"""
|
|
20 |
||
21 |
def __init__(self, *args, **kwargs): |
|
22 |
# as implemented in AuthorizeRequestTokenWithBrowser
|
|
23 |
kwargs['consumer_name'] = None |
|
24 |
kwargs.pop('allow_access_levels', None) |
|
25 |
super(AuthorizeRequestTokenWithConsole, self).__init__(*args, **kwargs) |
|
26 |
||
27 |
def make_end_user_authorize_token(self, credentials, request_token): |
|
28 |
"""Ask the end-user to authorize the token in their browser.
|
|
29 |
||
30 |
"""
|
|
31 |
authorization_url = self.authorization_url(request_token) |
|
32 |
print WAITING_FOR_USER.format(authorization_url) |
|
33 |
# if we don't flush we may not see the message
|
|
34 |
sys.stdout.flush() |
|
35 |
while credentials.access_token is None: |
|
36 |
time.sleep(ACCESS_TOKEN_POLL_TIME) |
|
37 |
try: |
|
38 |
credentials.exchange_request_token_for_access_token( |
|
44.1.1
by Martin Mrazik
pep8izing |
39 |
self.web_root) |
40 |
break
|
|
1
by Martin Mrazik
initial commit |
41 |
except HTTPError, e: |
42 |
if e.response.status == 403: |
|
43 |
# The user decided not to authorize this
|
|
44 |
# application.
|
|
45 |
raise e |
|
44.1.6
by Martin Mrazik
first part of pyflakes issues being fixed |
46 |
elif e.response.status == 401: |
1
by Martin Mrazik
initial commit |
47 |
# The user has not made a decision yet.
|
48 |
pass
|
|
49 |
else: |
|
50 |
# There was an error accessing the server.
|
|
51 |
raise e |
|
52 |