launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #20713
[Merge] lp:~blr/launchpad/generate-access-token-script into lp:launchpad
Kit Randel has proposed merging lp:~blr/launchpad/generate-access-token-script into lp:launchpad.
Commit message:
Add script to generate an oauth access key for a given user.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~blr/launchpad/generate-access-token-script/+merge/299086
Particularly useful for developers wanting to quickly setup SSO <-> LP integration locally.
--
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~blr/launchpad/generate-access-token-script into lp:launchpad.
=== added file 'scripts/generate-access-token.py'
--- scripts/generate-access-token.py 1970-01-01 00:00:00 +0000
+++ scripts/generate-access-token.py 2016-07-04 20:12:25 +0000
@@ -0,0 +1,67 @@
+#!/usr/bin/python -S
+
+# Conveniently generates access token and outputs relevant settings.
+# You will most likely want to run this with
+# LP_DISABLE_SSL_CERTIFICATE_VALIDATION=1 locally.
+
+import _pythonpath
+
+import sys
+
+from zope.component import getUtility
+
+from lp.registry.interfaces.person import IPersonSet
+from lp.services.oauth.interfaces import IOAuthConsumerSet
+from lp.services.webapp.interfaces import OAuthPermission
+from lp.services.scripts.base import LaunchpadScript
+
+
+LP_API_URL = 'https://api.launchpad.dev/devel'
+
+
+def print_local_settings(user, key, token, secret):
+ print("Access token for {user} generated with the following settings:\n\n"
+ "LP_API_URL = '{url}'\n"
+ "LP_API_CONSUMER_KEY = '{key}'\n"
+ "LP_API_TOKEN = '{token}'\n"
+ "LP_API_TOKEN_SECRET = '{secret}'").format(
+ user=user,
+ url=LP_API_URL,
+ key=key,
+ token=token,
+ secret=secret)
+
+
+class AccessTokenGenerator(LaunchpadScript):
+
+
+ def add_my_options(self):
+ self.parser.usage = "%prog username [-n CONSUMER NAME]"
+ self.parser.add_option("-n", "--name", dest="consumer_name")
+
+ def main(self):
+ if len(self.args) < 1:
+ self.parser.error('No username supplied')
+ username = self.args[0]
+
+ key = unicode(self.options.consumer_name)
+ consumer = getUtility(IOAuthConsumerSet).new(key, u'')
+ request_token, _ = consumer.newRequestToken()
+
+ # review by username
+ person = getUtility(IPersonSet).getByName(username)
+ if not person:
+ print('Error: No account for username %s.' % username)
+ sys.exit(1)
+ request_token.review(person, OAuthPermission.WRITE_PRIVATE)
+
+ # create access token
+ access_token, access_secret = request_token.createAccessToken()
+ print_local_settings(person.name,
+ self.options.consumer_name,
+ access_token.key,
+ access_secret)
+
+
+if __name__ == '__main__':
+ AccessTokenGenerator('generate-access-token').lock_and_run()
Follow ups