diff options
author | NeroBurner <pyro4hell@gmail.com> | 2021-09-23 19:40:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-23 23:10:51 +0530 |
commit | 49fa4d9af725d006e4722b0e169afe99290232d7 (patch) | |
tree | ffe19b82938d077f68cc5005a77aca95d57ef808 /yt_dlp/utils.py | |
parent | ee2b3563f367bf6644f58e16212262af8a664f6f (diff) | |
download | hypervideo-pre-49fa4d9af725d006e4722b0e169afe99290232d7.tar.lz hypervideo-pre-49fa4d9af725d006e4722b0e169afe99290232d7.tar.xz hypervideo-pre-49fa4d9af725d006e4722b0e169afe99290232d7.zip |
[atv.at] Use jwt for API (#1012)
The jwt token is implemented according to RFC7519
Closes #988
Authored by: NeroBurner
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r-- | yt_dlp/utils.py | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 9eb47fccb..141d2c9cc 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -16,6 +16,8 @@ import email.header import errno import functools import gzip +import hashlib +import hmac import imp import io import itertools @@ -3290,6 +3292,14 @@ def platform_name(): return res +def get_windows_version(): + ''' Get Windows version. None if it's not running on Windows ''' + if compat_os_name == 'nt': + return version_tuple(platform.win32_ver()[1]) + else: + return None + + def _windows_write_string(s, out): """ Returns True if the string was written using special methods, False if it has yet to be written out.""" @@ -6375,9 +6385,20 @@ def variadic(x, allowed_types=(str, bytes)): return x if isinstance(x, collections.abc.Iterable) and not isinstance(x, allowed_types) else (x,) -def get_windows_version(): - ''' Get Windows version. None if it's not running on Windows ''' - if compat_os_name == 'nt': - return version_tuple(platform.win32_ver()[1]) - else: - return None +# create a JSON Web Signature (jws) with HS256 algorithm +# the resulting format is in JWS Compact Serialization +# implemented following JWT https://www.rfc-editor.org/rfc/rfc7519.html +# implemented following JWS https://www.rfc-editor.org/rfc/rfc7515.html +def jwt_encode_hs256(payload_data, key, headers={}): + header_data = { + 'alg': 'HS256', + 'typ': 'JWT', + } + if headers: + header_data.update(headers) + header_b64 = base64.b64encode(json.dumps(header_data).encode('utf-8')) + payload_b64 = base64.b64encode(json.dumps(payload_data).encode('utf-8')) + h = hmac.new(key.encode('utf-8'), header_b64 + b'.' + payload_b64, hashlib.sha256) + signature_b64 = base64.b64encode(h.digest()) + token = header_b64 + b'.' + payload_b64 + b'.' + signature_b64 + return token |