| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2018-2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""OAuth helper functions and classes""" |
| 10 |
|
| 11 |
import hmac |
| 12 |
import time |
| 13 |
import random |
| 14 |
import string |
| 15 |
import hashlib |
| 16 |
import binascii |
| 17 |
import urllib.parse |
| 18 |
|
| 19 |
import requests |
| 20 |
import requests.auth |
| 21 |
|
| 22 |
from . import text |
| 23 |
|
| 24 |
|
| 25 |
def nonce(size, alphabet=string.ascii_letters): |
| 26 |
"""Generate a nonce value with 'size' characters""" |
| 27 |
return "".join(random.choice(alphabet) for _ in range(size)) |
| 28 |
|
| 29 |
|
| 30 |
def quote(value, quote=urllib.parse.quote): |
| 31 |
"""Quote 'value' according to the OAuth1.0 standard""" |
| 32 |
return quote(value, "~") |
| 33 |
|
| 34 |
|
| 35 |
def concat(*args): |
| 36 |
"""Concatenate 'args' as expected by OAuth1.0""" |
| 37 |
return "&".join(quote(item) for item in args) |
| 38 |
|
| 39 |
|
| 40 |
class OAuth1Session(requests.Session): |
| 41 |
"""Extension to requests.Session to support OAuth 1.0""" |
| 42 |
|
| 43 |
def __init__(self, consumer_key, consumer_secret, |
| 44 |
token=None, token_secret=None): |
| 45 |
|
| 46 |
requests.Session.__init__(self) |
| 47 |
self.auth = OAuth1Client( |
| 48 |
consumer_key, consumer_secret, |
| 49 |
token, token_secret, |
| 50 |
) |
| 51 |
|
| 52 |
def rebuild_auth(self, prepared_request, response): |
| 53 |
if "Authorization" in prepared_request.headers: |
| 54 |
del prepared_request.headers["Authorization"] |
| 55 |
prepared_request.prepare_auth(self.auth) |
| 56 |
|
| 57 |
|
| 58 |
class OAuth1Client(requests.auth.AuthBase): |
| 59 |
"""OAuth1.0a authentication""" |
| 60 |
|
| 61 |
def __init__(self, consumer_key, consumer_secret, |
| 62 |
token=None, token_secret=None): |
| 63 |
|
| 64 |
self.consumer_key = consumer_key |
| 65 |
self.consumer_secret = consumer_secret |
| 66 |
self.token = token |
| 67 |
self.token_secret = token_secret |
| 68 |
|
| 69 |
def __call__(self, request): |
| 70 |
oauth_params = [ |
| 71 |
("oauth_consumer_key", self.consumer_key), |
| 72 |
("oauth_nonce", nonce(16)), |
| 73 |
("oauth_signature_method", "HMAC-SHA1"), |
| 74 |
("oauth_timestamp", str(int(time.time()))), |
| 75 |
("oauth_version", "1.0"), |
| 76 |
] |
| 77 |
if self.token: |
| 78 |
oauth_params.append(("oauth_token", self.token)) |
| 79 |
|
| 80 |
signature = self.generate_signature(request, oauth_params) |
| 81 |
oauth_params.append(("oauth_signature", signature)) |
| 82 |
|
| 83 |
request.headers["Authorization"] = "OAuth " + ",".join( |
| 84 |
key + '="' + value + '"' for key, value in oauth_params) |
| 85 |
|
| 86 |
return request |
| 87 |
|
| 88 |
def generate_signature(self, request, params): |
| 89 |
"""Generate 'oauth_signature' value""" |
| 90 |
url, _, query = request.url.partition("?") |
| 91 |
|
| 92 |
params = params.copy() |
| 93 |
for key, value in text.parse_query(query).items(): |
| 94 |
params.append((quote(key), quote(value))) |
| 95 |
params.sort() |
| 96 |
query = "&".join("=".join(item) for item in params) |
| 97 |
|
| 98 |
message = concat(request.method, url, query).encode() |
| 99 |
key = concat(self.consumer_secret, self.token_secret or "").encode() |
| 100 |
signature = hmac.new(key, message, hashlib.sha1).digest() |
| 101 |
|
| 102 |
return quote(binascii.b2a_base64(signature)[:-1].decode()) |
| 103 |
|
| 104 |
|
| 105 |
class OAuth1API(): |
| 106 |
"""Base class for OAuth1.0 based API interfaces""" |
| 107 |
API_KEY = None |
| 108 |
API_SECRET = None |
| 109 |
|
| 110 |
def __init__(self, extractor): |
| 111 |
self.exc = extractor.exc |
| 112 |
self.log = extractor.log |
| 113 |
self.extractor = extractor |
| 114 |
|
| 115 |
api_key = extractor.config("api-key", self.API_KEY) |
| 116 |
api_secret = extractor.config("api-secret", self.API_SECRET) |
| 117 |
token = extractor.config("access-token") |
| 118 |
token_secret = extractor.config("access-token-secret") |
| 119 |
key_type = "default" if api_key == self.API_KEY else "custom" |
| 120 |
|
| 121 |
if token is None or token == "cache": |
| 122 |
token, token_secret = extractor.cache( |
| 123 |
_token_cache, (extractor.category, api_key), _mem=False) |
| 124 |
|
| 125 |
if api_key and api_secret and token and token_secret: |
| 126 |
self.log.debug("Using %s OAuth1.0 authentication", key_type) |
| 127 |
self.session = OAuth1Session( |
| 128 |
api_key, api_secret, token, token_secret) |
| 129 |
self.api_key = None |
| 130 |
else: |
| 131 |
self.log.debug("Using %s api_key authentication", key_type) |
| 132 |
self.session = extractor.session |
| 133 |
self.api_key = api_key |
| 134 |
|
| 135 |
def request(self, url, **kwargs): |
| 136 |
kwargs["fatal"] = None |
| 137 |
kwargs["session"] = self.session |
| 138 |
return self.extractor.request(url, **kwargs) |
| 139 |
|
| 140 |
|
| 141 |
def _token_cache(key): |
| 142 |
return None, None |