=== modified file 'ssh_import_id/__init__.py' --- ssh_import_id/__init__.py 2017-07-11 20:51:44 +0000 +++ ssh_import_id/__init__.py 2018-03-03 15:01:23 +0000 @@ -41,6 +41,7 @@ parser.add_argument('-o', '--output', metavar='FILE', help='Write output to file (default ~/.ssh/authorized_keys)') parser.add_argument('-r', '--remove', help='Remove a key from authorized keys file', action="store_true", default=False) parser.add_argument('-u', '--useragent', metavar='USERAGENT', help='Append to the http user agent string', default="") +parser.add_argument('-g', '--githubtoken', metavar='GITHUBTOKEN', help='Specify GitHub token to use for requests', default=False) parser.add_argument('userids', nargs='+', metavar="USERID", help='User IDs to import') parser.options = None TEMPFILES = [] @@ -193,19 +194,19 @@ return keys -def fetch_keys(proto, username, useragent): +def fetch_keys(proto, username, useragent, githubtoken): """ Call out to a subcommand to handle the specified protocol and username """ if proto == "lp": return fetch_keys_lp(username, useragent) elif proto == "gh": - return fetch_keys_gh(username, useragent) + return fetch_keys_gh(username, useragent, githubtoken) else: die("ssh-import-id protocol handler %s: not found or cannot execute" % (proto_cmd_path)) -def import_keys(proto, username, useragent): +def import_keys(proto, username, useragent, githubtoken): """ Import keys from service at 'proto' for 'username', appending to output file """ @@ -215,7 +216,7 @@ result = [] keyfile_lines = [] comment_string = "# ssh-import-id %s:%s" % (proto, username) - for line in fetch_keys(proto, username, useragent).split('\n'): + for line in fetch_keys(proto, username, useragent, githubtoken).split('\n'): # Validate/clean-up key text try: line = line.decode('utf-8').strip() @@ -290,13 +291,15 @@ return keys -def fetch_keys_gh(ghid, useragent): +def fetch_keys_gh(ghid, useragent, githubtoken): x_ratelimit_remaining = 'x-ratelimit-remaining' help_url = 'https://developer.github.com/v3/#rate-limiting' keys = "" try: url = "https://api.github.com/users/%s/keys" % (quote_plus(ghid)) headers = {'User-Agent': user_agent()} + if githubtoken: + headers['Authorization'] = 'token %s' % (githubtoken) resp = requests.get(url, headers=headers, verify=True) text = resp.text data = json.loads(text) @@ -304,7 +307,7 @@ print('Username "%s" not found at GitHub API' % ghid) os._exit(1) if x_ratelimit_remaining in resp.headers and int(resp.headers[x_ratelimit_remaining]) == 0: - print('GitHub REST API rate-limited this IP address. See %s' % help_url) + print('GitHub REST API rate-limited this IP address. See %s, and/or use `-g` to specify the GitHub token.' % help_url) os._exit(1) for keyobj in data: keys += "%s %s@github/%s\n" % (keyobj['key'], ghid, keyobj['id']) === modified file 'usr/bin/ssh-import-id' --- usr/bin/ssh-import-id 2016-01-30 15:04:58 +0000 +++ usr/bin/ssh-import-id 2018-03-03 14:17:43 +0000 @@ -42,7 +42,7 @@ keys.extend(k) action = "Removed" else: - k = import_keys(proto, username, parser.options.useragent) + k = import_keys(proto, username, parser.options.useragent, parser.options.githubtoken) keys.extend(k) action = "Authorized" if len(k) == 0: