| 1 |
************ |
| 2 |
launchpadlib |
| 3 |
************ |
| 4 |
|
| 5 |
launchpadlib is the standalone Python language bindings to Launchpad's web |
| 6 |
services API. It is officially supported by Canonical, although third party |
| 7 |
packages may be available to provide bindings to other programming languages. |
| 8 |
|
| 9 |
Set up |
| 10 |
====== |
| 11 |
|
| 12 |
launchpadlib writes to $HOME, so isolate ourselves. |
| 13 |
|
| 14 |
>>> from fixtures import ( |
| 15 |
... EnvironmentVariable, |
| 16 |
... TempDir, |
| 17 |
... ) |
| 18 |
>>> tempdir_fixture = TempDir() |
| 19 |
>>> tempdir_fixture.setUp() |
| 20 |
>>> home_fixture = EnvironmentVariable('HOME', tempdir_fixture.path) |
| 21 |
>>> home_fixture.setUp() |
| 22 |
|
| 23 |
OAuth authentication |
| 24 |
==================== |
| 25 |
|
| 26 |
The Launchpad API requires user authentication via OAuth, and launchpadlib |
| 27 |
provides a high level interface to OAuth for the most common use cases. |
| 28 |
Several pieces of information are necessary to complete the OAuth request: |
| 29 |
|
| 30 |
* A consumer key, which is unique to the application using the API |
| 31 |
* An access token, which represents the user to the web service |
| 32 |
* An access token secret, essentially a password for the token |
| 33 |
|
| 34 |
Consumer keys are hard-baked into the application. They are generated by the |
| 35 |
application developer and registered with Launchpad independently of the use |
| 36 |
of the application. Since consumer keys are arbitrary, a registered consumer |
| 37 |
key can be paired with a secret, but most open source applications will forgo |
| 38 |
this since it's not really a secret anyway. |
| 39 |
|
| 40 |
The access token cannot be provided directly. Instead, the application |
| 41 |
generates an unauthenticated request token, exchanging this for an access |
| 42 |
token and a secret after obtaining approval to do so from the user. This |
| 43 |
permission is typically gained by redirecting the user through their trusted |
| 44 |
web browser, then back to the application. |
| 45 |
|
| 46 |
This entire exchange is managed by launchpadlib's credentials classes. |
| 47 |
Credentials can be stored in a file, though the security of this depends on |
| 48 |
the implementation of the file object. In the simplest case, the application |
| 49 |
will request a new access token every time. |
| 50 |
|
| 51 |
>>> from launchpadlib.credentials import Consumer |
| 52 |
>>> consumer = Consumer('launchpad-library') |
| 53 |
>>> consumer.key |
| 54 |
'launchpad-library' |
| 55 |
>>> consumer.secret |
| 56 |
'' |
| 57 |
|
| 58 |
Salgado has full access to the Launchpad API. Out of band, the application |
| 59 |
itself obtains Salgado's approval to access the Launchpad API on his behalf. |
| 60 |
How the application does this is up to the application, provided it conforms |
| 61 |
to the OAuth protocol. Once this happens, we have Salgado's credentials for |
| 62 |
accessing Launchpad. |
| 63 |
|
| 64 |
>>> from launchpadlib.credentials import AccessToken |
| 65 |
>>> access_token = AccessToken('salgado-change-anything', 'test') |
| 66 |
|
| 67 |
And now these credentials are used to access the root service on Salgado's |
| 68 |
behalf. |
| 69 |
|
| 70 |
>>> from launchpadlib.credentials import Credentials |
| 71 |
>>> credentials = Credentials( |
| 72 |
... consumer_name=consumer.key, consumer_secret=consumer.secret, |
| 73 |
... access_token=access_token) |
| 74 |
|
| 75 |
>>> from launchpadlib.testing.helpers import ( |
| 76 |
... TestableLaunchpad as Launchpad) |
| 77 |
>>> launchpad = Launchpad(credentials=credentials) |
| 78 |
>>> list(launchpad.people) |
| 79 |
[...] |
| 80 |
>>> list(launchpad.bugs) |
| 81 |
[...] |
| 82 |
|
| 83 |
If available, the Gnome keyring or KDE wallet will be used to store access |
| 84 |
tokens. If a keyring/wallet is not available, the application can store the |
| 85 |
credentials on the file system, so that the next time Salgado interacts with |
| 86 |
the application, he won't have to go through the whole OAuth request dance. |
| 87 |
|
| 88 |
>>> import os |
| 89 |
>>> import tempfile |
| 90 |
>>> fd, path = tempfile.mkstemp('.credentials') |
| 91 |
>>> os.close(fd) |
| 92 |
|
| 93 |
Once Salgado's credentials are obtained for the first time, just set the |
| 94 |
appropriate instance variables and use the save() method. |
| 95 |
|
| 96 |
>>> credentials.consumer = consumer |
| 97 |
>>> credentials.access_token = access_token |
| 98 |
>>> credentials_file = open(path, 'w') |
| 99 |
>>> credentials.save(credentials_file) |
| 100 |
>>> credentials_file.close() |
| 101 |
|
| 102 |
And the credentials are perfectly valid for accessing Launchpad. |
| 103 |
|
| 104 |
>>> launchpad = Launchpad(credentials=credentials) |
| 105 |
>>> list(launchpad.people) |
| 106 |
[...] |
| 107 |
>>> list(launchpad.bugs) |
| 108 |
[...] |
| 109 |
|
| 110 |
The credentials can also be retrieved from the file, so that the OAuth request |
| 111 |
dance can be avoided. |
| 112 |
|
| 113 |
>>> credentials = Credentials() |
| 114 |
>>> credentials_file = open(path) |
| 115 |
>>> credentials.load(credentials_file) |
| 116 |
>>> credentials_file.close() |
| 117 |
>>> credentials.consumer.key |
| 118 |
'launchpad-library' |
| 119 |
>>> credentials.consumer.secret |
| 120 |
'' |
| 121 |
>>> credentials.access_token.key |
| 122 |
'salgado-change-anything' |
| 123 |
>>> credentials.access_token.secret |
| 124 |
'test' |
| 125 |
|
| 126 |
These credentials too, are perfectly usable to access Launchpad. |
| 127 |
|
| 128 |
>>> launchpad = Launchpad(credentials=credentials) |
| 129 |
>>> list(launchpad.people) |
| 130 |
[...] |
| 131 |
>>> list(launchpad.bugs) |
| 132 |
[...] |
| 133 |
|
| 134 |
The security of the stored credentials is left up to the file-like object. |
| 135 |
Here, the application decides to use a dubious encryption algorithm to hide |
| 136 |
Salgado's credentials. |
| 137 |
|
| 138 |
>>> import io |
| 139 |
>>> from codecs import EncodedFile |
| 140 |
>>> encrypted_file = io.BytesIO() |
| 141 |
>>> stream = EncodedFile(encrypted_file, 'rot_13', 'ascii') |
| 142 |
>>> credentials.save(stream) |
| 143 |
>>> _ = stream.seek(0, 0) |
| 144 |
>>> print(''.join(sorted([line.decode() for line in encrypted_file]))) |
| 145 |
[1] |
| 146 |
<BLANKLINE> |
| 147 |
<BLANKLINE> |
| 148 |
npprff_frperg = grfg |
| 149 |
npprff_gbxra = fnytnqb-punatr-nalguvat |
| 150 |
pbafhzre_frperg = |
| 151 |
pbafhzre_xrl = ynhapucnq-yvoenel |
| 152 |
|
| 153 |
>>> _ = stream.seek(0) |
| 154 |
>>> credentials = Credentials() |
| 155 |
>>> credentials.load(stream) |
| 156 |
>>> credentials.consumer.key |
| 157 |
'launchpad-library' |
| 158 |
>>> credentials.consumer.secret |
| 159 |
'' |
| 160 |
>>> credentials.access_token.key |
| 161 |
'salgado-change-anything' |
| 162 |
>>> credentials.access_token.secret |
| 163 |
'test' |
| 164 |
|
| 165 |
|
| 166 |
Anonymous access |
| 167 |
================ |
| 168 |
|
| 169 |
An anonymous access token doesn't authenticate any particular |
| 170 |
user. Using it will give a client read-only access to the public parts |
| 171 |
of the Launchpad dataset. |
| 172 |
|
| 173 |
>>> from launchpadlib.credentials import AnonymousAccessToken |
| 174 |
>>> anonymous_token = AnonymousAccessToken() |
| 175 |
|
| 176 |
>>> from launchpadlib.credentials import Credentials |
| 177 |
>>> credentials = Credentials( |
| 178 |
... consumer_name="a consumer", access_token=anonymous_token) |
| 179 |
>>> launchpad = Launchpad(credentials=credentials) |
| 180 |
|
| 181 |
>>> salgado = launchpad.people['salgado'] |
| 182 |
>>> print(salgado.display_name) |
| 183 |
Guilherme Salgado |
| 184 |
|
| 185 |
An anonymous client can't modify the dataset, or read any data that's |
| 186 |
permission-controlled or scoped to a particular user. |
| 187 |
|
| 188 |
>>> launchpad.me |
| 189 |
Traceback (most recent call last): |
| 190 |
... |
| 191 |
lazr.restfulclient.errors.Unauthorized: HTTP Error 401: Unauthorized |
| 192 |
... |
| 193 |
|
| 194 |
>>> salgado.display_name = "This won't work." |
| 195 |
>>> salgado.lp_save() |
| 196 |
Traceback (most recent call last): |
| 197 |
... |
| 198 |
lazr.restfulclient.errors.Unauthorized: HTTP Error 401: Unauthorized |
| 199 |
... |
| 200 |
|
| 201 |
Convenience |
| 202 |
=========== |
| 203 |
|
| 204 |
When you want anonymous access, a convenience method is available for |
| 205 |
setting up a web service connection in one function call. All you have |
| 206 |
to provide is the consumer name. |
| 207 |
|
| 208 |
>>> launchpad = Launchpad.login_anonymously( |
| 209 |
... 'launchpad-library', service_root="test_dev") |
| 210 |
>>> list(launchpad.people) |
| 211 |
[...] |
| 212 |
|
| 213 |
>>> launchpad.me |
| 214 |
Traceback (most recent call last): |
| 215 |
... |
| 216 |
lazr.restfulclient.errors.Unauthorized: HTTP Error 401: Unauthorized |
| 217 |
... |
| 218 |
|
| 219 |
Otherwise, the application should obtain authorization from the user |
| 220 |
and get a new set of credentials directly from |
| 221 |
Launchpad. |
| 222 |
|
| 223 |
Unfortunately, we can't test this entire process because it requires |
| 224 |
opening up a web browser, but we can test the first step, which is to |
| 225 |
get a request token. |
| 226 |
|
| 227 |
>>> import launchpadlib.credentials |
| 228 |
>>> credentials = Credentials('consumer') |
| 229 |
|
| 230 |
>>> authorization_url = credentials.get_request_token( |
| 231 |
... context='firefox', web_root='test_dev') |
| 232 |
>>> print(authorization_url) |
| 233 |
http://launchpad.test:8085/+authorize-token?oauth_token=...&lp.context=firefox |
| 234 |
|
| 235 |
We use 'test_dev' as a shorthand for the root URL of the Launchpad |
| 236 |
installation. It's defined in the 'uris' module as |
| 237 |
'http://launchpad.test:8085/', and the launchpadlib code knows how to |
| 238 |
dereference it before using it as a URL. |
| 239 |
|
| 240 |
Information about the request token is kept in the _request_token |
| 241 |
attribute of the Credentials object. |
| 242 |
|
| 243 |
>>> credentials._request_token.key is not None |
| 244 |
True |
| 245 |
>>> credentials._request_token.secret is not None |
| 246 |
True |
| 247 |
>>> print(credentials._request_token.context) |
| 248 |
firefox |
| 249 |
|
| 250 |
Now the user must authorize that token, and this is the part we can't |
| 251 |
test--it requires opening a web browser. Once the token is authorized |
| 252 |
on the server side, we can call exchange_request_token_for_access_token() |
| 253 |
on our Credentials object, which will then be ready to use. |
| 254 |
|
| 255 |
The dictionary request token |
| 256 |
============================ |
| 257 |
|
| 258 |
By default, get_request_token returns the URL that the user needs to |
| 259 |
use when granting access to the token. But you can specify a different |
| 260 |
token_format and get a dictionary instead. |
| 261 |
|
| 262 |
>>> credentials = Credentials('consumer') |
| 263 |
>>> dictionary = credentials.get_request_token( |
| 264 |
... context='firefox', web_root='test_dev', |
| 265 |
... token_format=Credentials.DICT_TOKEN_FORMAT) |
| 266 |
|
| 267 |
The dictionary has useful information about the token and about the |
| 268 |
levels of authentication Launchpad offers. |
| 269 |
|
| 270 |
>>> for param in sorted(dictionary.keys()): |
| 271 |
... print(param) |
| 272 |
access_levels |
| 273 |
lp.context |
| 274 |
oauth_token |
| 275 |
oauth_token_consumer |
| 276 |
oauth_token_secret |
| 277 |
|
| 278 |
The _request_token attribute of the Credentials object has the same |
| 279 |
fields set as if you had asked for the default URI token format. |
| 280 |
|
| 281 |
>>> credentials._request_token.key is not None |
| 282 |
True |
| 283 |
>>> credentials._request_token.secret is not None |
| 284 |
True |
| 285 |
>>> print(credentials._request_token.context) |
| 286 |
firefox |
| 287 |
|
| 288 |
|
| 289 |
Credentials file errors |
| 290 |
======================= |
| 291 |
|
| 292 |
If the credentials file is empty, loading it raises an exception. |
| 293 |
|
| 294 |
>>> credentials = Credentials() |
| 295 |
>>> credentials.load(io.StringIO()) |
| 296 |
Traceback (most recent call last): |
| 297 |
... |
| 298 |
lazr.restfulclient.errors.CredentialsFileError: No configuration for |
| 299 |
version 1 |
| 300 |
|
| 301 |
It is an error to save a credentials file when no consumer or access token is |
| 302 |
available. |
| 303 |
|
| 304 |
>>> credentials.consumer = None |
| 305 |
>>> credentials.save(io.StringIO()) |
| 306 |
Traceback (most recent call last): |
| 307 |
... |
| 308 |
lazr.restfulclient.errors.CredentialsFileError: No consumer |
| 309 |
|
| 310 |
>>> credentials.consumer = consumer |
| 311 |
>>> credentials.access_token = None |
| 312 |
>>> credentials.save(io.StringIO()) |
| 313 |
Traceback (most recent call last): |
| 314 |
... |
| 315 |
lazr.restfulclient.errors.CredentialsFileError: No access token |
| 316 |
|
| 317 |
The credentials file is not intended to be edited, but because it's human |
| 318 |
readable, that's of course possible. If the credentials file gets corrupted, |
| 319 |
an error is raised. |
| 320 |
|
| 321 |
>>> credentials_file = io.StringIO("""\ |
| 322 |
... [1] |
| 323 |
... #consumer_key: aardvark |
| 324 |
... consumer_secret: badger |
| 325 |
... access_token: caribou |
| 326 |
... access_secret: dingo |
| 327 |
... """) |
| 328 |
>>> credentials.load(credentials_file) |
| 329 |
Traceback (most recent call last): |
| 330 |
... |
| 331 |
configparser.NoOptionError: No option 'consumer_key' in section: '1' |
| 332 |
|
| 333 |
>>> credentials_file = io.StringIO("""\ |
| 334 |
... [1] |
| 335 |
... consumer_key: aardvark |
| 336 |
... #consumer_secret: badger |
| 337 |
... access_token: caribou |
| 338 |
... access_secret: dingo |
| 339 |
... """) |
| 340 |
>>> credentials.load(credentials_file) |
| 341 |
Traceback (most recent call last): |
| 342 |
... |
| 343 |
configparser.NoOptionError: No option 'consumer_secret' in section: '1' |
| 344 |
|
| 345 |
>>> credentials_file = io.StringIO("""\ |
| 346 |
... [1] |
| 347 |
... consumer_key: aardvark |
| 348 |
... consumer_secret: badger |
| 349 |
... #access_token: caribou |
| 350 |
... access_secret: dingo |
| 351 |
... """) |
| 352 |
>>> credentials.load(credentials_file) |
| 353 |
Traceback (most recent call last): |
| 354 |
... |
| 355 |
configparser.NoOptionError: No option 'access_token' in section: '1' |
| 356 |
|
| 357 |
>>> credentials_file = io.StringIO("""\ |
| 358 |
... [1] |
| 359 |
... consumer_key: aardvark |
| 360 |
... consumer_secret: badger |
| 361 |
... access_token: caribou |
| 362 |
... #access_secret: dingo |
| 363 |
... """) |
| 364 |
>>> credentials.load(credentials_file) |
| 365 |
Traceback (most recent call last): |
| 366 |
... |
| 367 |
configparser.NoOptionError: No option 'access_secret' in section: '1' |
| 368 |
|
| 369 |
|
| 370 |
Bad credentials |
| 371 |
=============== |
| 372 |
|
| 373 |
The application is not allowed to access Launchpad with a bad access token. |
| 374 |
|
| 375 |
>>> access_token = AccessToken('bad', 'no-secret') |
| 376 |
>>> credentials = Credentials( |
| 377 |
... consumer_name=consumer.key, consumer_secret=consumer.secret, |
| 378 |
... access_token=access_token) |
| 379 |
>>> launchpad = Launchpad(credentials=credentials) |
| 380 |
Traceback (most recent call last): |
| 381 |
... |
| 382 |
lazr.restfulclient.errors.Unauthorized: HTTP Error 401: Unauthorized |
| 383 |
... |
| 384 |
|
| 385 |
The application is not allowed to access Launchpad with a consumer |
| 386 |
name that doesn't match the credentials. |
| 387 |
|
| 388 |
>>> access_token = AccessToken('salgado-change-anything', 'test') |
| 389 |
>>> credentials = Credentials( |
| 390 |
... consumer_name='not-the-launchpad-library', |
| 391 |
... access_token=access_token) |
| 392 |
>>> launchpad = Launchpad(credentials=credentials) |
| 393 |
Traceback (most recent call last): |
| 394 |
... |
| 395 |
lazr.restfulclient.errors.Unauthorized: HTTP Error 401: Unauthorized |
| 396 |
... |
| 397 |
|
| 398 |
The application is not allowed to access Launchpad with a bad access secret. |
| 399 |
|
| 400 |
>>> access_token = AccessToken('hgm2VK35vXD6rLg5pxWw', 'bad-secret') |
| 401 |
>>> credentials = Credentials( |
| 402 |
... consumer_name=consumer.key, consumer_secret=consumer.secret, |
| 403 |
... access_token=access_token) |
| 404 |
>>> launchpad = Launchpad(credentials=credentials) |
| 405 |
Traceback (most recent call last): |
| 406 |
... |
| 407 |
lazr.restfulclient.errors.Unauthorized: HTTP Error 401: Unauthorized |
| 408 |
... |
| 409 |
|
| 410 |
Clean up |
| 411 |
======== |
| 412 |
|
| 413 |
>>> os.remove(path) |
| 414 |
>>> home_fixture.cleanUp() |
| 415 |
>>> tempdir_fixture.cleanUp() |