Calculates the HMAC-SHA1 OAuth 1.0a signature for the given request. See http://oauth.net/core/1.0a/#signing_process
(
consumer_token: Dict[str, Any],
method: str,
url: str,
parameters: Dict[str, Any] = {},
token: Optional[Dict[str, Any]] = None,
)
| 1184 | |
| 1185 | |
| 1186 | def _oauth10a_signature( |
| 1187 | consumer_token: Dict[str, Any], |
| 1188 | method: str, |
| 1189 | url: str, |
| 1190 | parameters: Dict[str, Any] = {}, |
| 1191 | token: Optional[Dict[str, Any]] = None, |
| 1192 | ) -> bytes: |
| 1193 | """Calculates the HMAC-SHA1 OAuth 1.0a signature for the given request. |
| 1194 | |
| 1195 | See http://oauth.net/core/1.0a/#signing_process |
| 1196 | """ |
| 1197 | parts = urllib.parse.urlparse(url) |
| 1198 | scheme, netloc, path = parts[:3] |
| 1199 | normalized_url = scheme.lower() + "://" + netloc.lower() + path |
| 1200 | |
| 1201 | base_elems = [] |
| 1202 | base_elems.append(method.upper()) |
| 1203 | base_elems.append(normalized_url) |
| 1204 | base_elems.append( |
| 1205 | "&".join(f"{k}={_oauth_escape(str(v))}" for k, v in sorted(parameters.items())) |
| 1206 | ) |
| 1207 | |
| 1208 | base_string = "&".join(_oauth_escape(e) for e in base_elems) |
| 1209 | key_elems = [escape.utf8(urllib.parse.quote(consumer_token["secret"], safe="~"))] |
| 1210 | key_elems.append( |
| 1211 | escape.utf8(urllib.parse.quote(token["secret"], safe="~") if token else "") |
| 1212 | ) |
| 1213 | key = b"&".join(key_elems) |
| 1214 | |
| 1215 | hash = hmac.new(key, escape.utf8(base_string), hashlib.sha1) |
| 1216 | return binascii.b2a_base64(hash.digest())[:-1] |
| 1217 | |
| 1218 | |
| 1219 | def _oauth_escape(val: Union[str, bytes]) -> str: |
no test coverage detected