Created
December 21, 2015 19:30
-
-
Save jonasfj/27387d57d25c257a9b2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def authenticate(description=None): | |
""" | |
Open a web-browser to login.taskcluster.net and listen on localhost for | |
a callback with credentials in query-string. | |
The description will be shown on login.taskcluster.net, if not provided | |
a default message with script path will be displayed. | |
""" | |
# Importing here to avoid loading these 'obscure' module before it's needed. | |
# Most clients won't use this feature, so we don't want issues with these | |
# modules to affect the library. Maybe they don't work in some environments | |
import webbrowser | |
import urlparse | |
import BaseHTTPServer | |
import urllib | |
import os | |
import sys | |
if not description: | |
script = '[interpreter/unknown]' | |
main = sys.modules.get('__main__', None) | |
if main and hasattr(main, '__file__'): | |
script = os.path.abspath(main.__file__) | |
description = ( | |
"Python script: `%s`\n\nWould like some temporary credentials." | |
% script | |
) | |
creds = [None] | |
class AuthCallBackRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
def log_message(format, *args): | |
pass | |
def do_GET(self): | |
url = urlparse.urlparse(self.path) | |
query = urlparse.parse_qs(url.query) | |
clientId = query.get('clientId', [None])[0] | |
accessToken = query.get('accessToken', [None])[0] | |
certificate = query.get('certificate', [None])[0] | |
hasCreds = clientId and accessToken and certificate | |
if hasCreds: | |
creds[0] = { | |
"clientId": clientId, | |
"accessToken": accessToken, | |
"certificate": certificate | |
} | |
self.send_response(200) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
if hasCreds: | |
self.wfile.write(""" | |
<h1>Credentials transferred successfully</h1> | |
<i>You can close this window now.</i> | |
<script>window.close();</script> | |
""") | |
else: | |
self.wfile.write(""" | |
<h1>Transfer of credentials failed!</h1> | |
<p>Something went wrong, you can navigate back and try again...</p> | |
""") | |
return | |
# Create server on localhost at random port | |
retries = 5 | |
while retries > 0: | |
try: | |
server = BaseHTTPServer.HTTPServer(('', 0), AuthCallBackRequestHandler) | |
except: | |
retries -= 1 | |
break | |
port = server.server_address[1] | |
query = "?target=" + urllib.quote('http://localhost:' + str(port), '') | |
query += "&description=" + urllib.quote(description, '') | |
webbrowser.open('https://login.taskcluster.net' + query, 1, True) | |
print "" | |
print "-------------------------------------------------------" | |
print " Opening browser window to login.taskcluster.net" | |
print " Asking you to grant temporary credentials to:" | |
print " http://localhost:" + str(port) | |
print "-------------------------------------------------------" | |
print "" | |
while not creds[0]: | |
server.handle_request() | |
return creds[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment