usso-jwt 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- usso_jwt-0.1.0/LICENSE.txt +19 -0
- usso_jwt-0.1.0/PKG-INFO +92 -0
- usso_jwt-0.1.0/README.md +41 -0
- usso_jwt-0.1.0/pyproject.toml +51 -0
- usso_jwt-0.1.0/setup.cfg +4 -0
- usso_jwt-0.1.0/src/jwt/__init__.py +0 -0
- usso_jwt-0.1.0/src/jwt/algorithms/__init__.py +37 -0
- usso_jwt-0.1.0/src/jwt/algorithms/base.py +38 -0
- usso_jwt-0.1.0/src/jwt/algorithms/ecdsa.py +131 -0
- usso_jwt-0.1.0/src/jwt/algorithms/eddsa.py +95 -0
- usso_jwt-0.1.0/src/jwt/algorithms/hmac.py +98 -0
- usso_jwt-0.1.0/src/jwt/algorithms/rsa.py +143 -0
- usso_jwt-0.1.0/src/jwt/core.py +26 -0
- usso_jwt-0.1.0/src/jwt/exceptions.py +29 -0
- usso_jwt-0.1.0/src/jwt/jwt.py +86 -0
- usso_jwt-0.1.0/src/jwt/sign.py +82 -0
- usso_jwt-0.1.0/src/jwt/verify.py +76 -0
- usso_jwt-0.1.0/src/usso_jwt.egg-info/PKG-INFO +92 -0
- usso_jwt-0.1.0/src/usso_jwt.egg-info/SOURCES.txt +24 -0
- usso_jwt-0.1.0/src/usso_jwt.egg-info/dependency_links.txt +1 -0
- usso_jwt-0.1.0/src/usso_jwt.egg-info/requires.txt +5 -0
- usso_jwt-0.1.0/src/usso_jwt.egg-info/top_level.txt +1 -0
- usso_jwt-0.1.0/tests/test_ecdsa.py +58 -0
- usso_jwt-0.1.0/tests/test_eddsa.py +43 -0
- usso_jwt-0.1.0/tests/test_hmac.py +58 -0
- usso_jwt-0.1.0/tests/test_rsa.py +58 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2016 The Python Packaging Authority (PyPA)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
the Software without restriction, including without limitation the rights to
|
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
8
|
+
so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
usso_jwt-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: usso-jwt
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple and lightweight Python package for handling JWT (JSON Web Token) operations with USSO (Unified Single Sign-On).
|
|
5
|
+
Author-email: Mahdi Kiani <mahdikiany@gmail.com>
|
|
6
|
+
Maintainer-email: Mahdi Kiani <mahdikiany@gmail.com>
|
|
7
|
+
License: Copyright (c) 2016 The Python Packaging Authority (PyPA)
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
10
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
11
|
+
the Software without restriction, including without limitation the rights to
|
|
12
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
13
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
14
|
+
so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Project-URL: Homepage, https://github.com/ussoio/usso-jwt
|
|
28
|
+
Project-URL: Bug Reports, https://github.com/ussoio/usso-jwt/issues
|
|
29
|
+
Project-URL: Funding, https://github.com/ussoio/usso-jwt
|
|
30
|
+
Project-URL: Say Thanks!, https://saythanks.io/to/mahdikiani
|
|
31
|
+
Project-URL: Source, https://github.com/ussoio/usso-jwt
|
|
32
|
+
Keywords: usso,usso-jwt,sso,authentication,security,jwt,json-web-token,token
|
|
33
|
+
Classifier: Development Status :: 3 - Alpha
|
|
34
|
+
Classifier: Intended Audience :: Developers
|
|
35
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
36
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
37
|
+
Classifier: Programming Language :: Python :: 3
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
42
|
+
Requires-Python: >=3.9
|
|
43
|
+
Description-Content-Type: text/markdown
|
|
44
|
+
License-File: LICENSE.txt
|
|
45
|
+
Requires-Dist: pydantic>=2
|
|
46
|
+
Requires-Dist: json-advanced
|
|
47
|
+
Requires-Dist: httpx
|
|
48
|
+
Requires-Dist: cryptography
|
|
49
|
+
Requires-Dist: cachetools>=5.0.0
|
|
50
|
+
Dynamic: license-file
|
|
51
|
+
|
|
52
|
+
# USSO-Client
|
|
53
|
+
|
|
54
|
+
The USSO-Client provides a universal single sign-on (SSO) integration for microservices, making it easy to add secure, scalable authentication across different frameworks. This client simplifies the process of connecting any microservice to the USSO service.
|
|
55
|
+
|
|
56
|
+
## Features
|
|
57
|
+
|
|
58
|
+
- **Core SSO Integration**: Use the USSO core client for basic SSO functionality across any Python application.
|
|
59
|
+
- **Framework-Specific Modules**:
|
|
60
|
+
- **FastAPI Integration**: Specialized support for FastAPI applications, enabling async authentication mechanisms tailored to FastAPI's event loop.
|
|
61
|
+
- *Django Integration* (Coming soon): Customizable Django authentication backend that integrates seamlessly with Django's user management and middleware architecture.
|
|
62
|
+
|
|
63
|
+
## Installation
|
|
64
|
+
|
|
65
|
+
Install the USSO client using pip:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install usso
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
To add framework-specific support, use the following commands:
|
|
72
|
+
|
|
73
|
+
For FastAPI:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install "usso[fastapi]"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
For Django:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pip install "usso[django]"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Quick Start
|
|
86
|
+
Follow the quick start guides in the documentation to integrate USSO in your application.
|
|
87
|
+
|
|
88
|
+
## Contributing
|
|
89
|
+
Contributions are welcome! See CONTRIBUTING.md for more details on how to get involved.
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
Distributed under the MIT License. See LICENSE for more information.
|
usso_jwt-0.1.0/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# USSO-Client
|
|
2
|
+
|
|
3
|
+
The USSO-Client provides a universal single sign-on (SSO) integration for microservices, making it easy to add secure, scalable authentication across different frameworks. This client simplifies the process of connecting any microservice to the USSO service.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Core SSO Integration**: Use the USSO core client for basic SSO functionality across any Python application.
|
|
8
|
+
- **Framework-Specific Modules**:
|
|
9
|
+
- **FastAPI Integration**: Specialized support for FastAPI applications, enabling async authentication mechanisms tailored to FastAPI's event loop.
|
|
10
|
+
- *Django Integration* (Coming soon): Customizable Django authentication backend that integrates seamlessly with Django's user management and middleware architecture.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Install the USSO client using pip:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install usso
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
To add framework-specific support, use the following commands:
|
|
21
|
+
|
|
22
|
+
For FastAPI:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install "usso[fastapi]"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
For Django:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install "usso[django]"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
Follow the quick start guides in the documentation to integrate USSO in your application.
|
|
36
|
+
|
|
37
|
+
## Contributing
|
|
38
|
+
Contributions are welcome! See CONTRIBUTING.md for more details on how to get involved.
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
Distributed under the MIT License. See LICENSE for more information.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "usso-jwt"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A simple and lightweight Python package for handling JWT (JSON Web Token) operations with USSO (Unified Single Sign-On)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { file = "LICENSE.txt" }
|
|
12
|
+
keywords = [
|
|
13
|
+
"usso",
|
|
14
|
+
"usso-jwt",
|
|
15
|
+
"sso",
|
|
16
|
+
"authentication",
|
|
17
|
+
"security",
|
|
18
|
+
"jwt",
|
|
19
|
+
"json-web-token",
|
|
20
|
+
"token",
|
|
21
|
+
]
|
|
22
|
+
authors = [{ name = "Mahdi Kiani", email = "mahdikiany@gmail.com" }]
|
|
23
|
+
maintainers = [{ name = "Mahdi Kiani", email = "mahdikiany@gmail.com" }]
|
|
24
|
+
classifiers = [
|
|
25
|
+
"Development Status :: 3 - Alpha",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"Topic :: Software Development :: Build Tools",
|
|
28
|
+
"License :: OSI Approved :: MIT License",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3.10",
|
|
31
|
+
"Programming Language :: Python :: 3.11",
|
|
32
|
+
"Programming Language :: Python :: 3.12",
|
|
33
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
34
|
+
]
|
|
35
|
+
dependencies = [
|
|
36
|
+
"pydantic>=2",
|
|
37
|
+
"json-advanced",
|
|
38
|
+
"httpx",
|
|
39
|
+
"cryptography",
|
|
40
|
+
"cachetools>=5.0.0",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
"Homepage" = "https://github.com/ussoio/usso-jwt"
|
|
45
|
+
"Bug Reports" = "https://github.com/ussoio/usso-jwt/issues"
|
|
46
|
+
"Funding" = "https://github.com/ussoio/usso-jwt"
|
|
47
|
+
"Say Thanks!" = "https://saythanks.io/to/mahdikiani"
|
|
48
|
+
"Source" = "https://github.com/ussoio/usso-jwt"
|
|
49
|
+
|
|
50
|
+
[tool.setuptools]
|
|
51
|
+
package-data = { "usso_jwt" = ["*.dat"] }
|
usso_jwt-0.1.0/setup.cfg
ADDED
|
File without changes
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from typing import Type
|
|
2
|
+
|
|
3
|
+
from .base import Algorithm
|
|
4
|
+
from .ecdsa import ECDSAAlgorithm
|
|
5
|
+
from .eddsa import EdDSAAlgorithm
|
|
6
|
+
from .hmac import HMACAlgorithm
|
|
7
|
+
from .rsa import RSAAlgorithm
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_algorithm(alg: str) -> Type[Algorithm]:
|
|
11
|
+
"""
|
|
12
|
+
Get the appropriate algorithm manager for the given algorithm.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
alg: The algorithm name (e.g., "HS256", "RS256", "ES256", "EdDSA")
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
The appropriate algorithm manager class
|
|
19
|
+
|
|
20
|
+
Raises:
|
|
21
|
+
ValueError: If the algorithm is not supported
|
|
22
|
+
"""
|
|
23
|
+
for algo in Algorithm.__subclasses__():
|
|
24
|
+
if alg.upper() in algo.SUPPORTED_ALGORITHMS:
|
|
25
|
+
return algo
|
|
26
|
+
|
|
27
|
+
raise ValueError(f"Unsupported algorithm: {alg}")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"Algorithm",
|
|
32
|
+
"HMACAlgorithm",
|
|
33
|
+
"RSAAlgorithm",
|
|
34
|
+
"ECDSAAlgorithm",
|
|
35
|
+
"EdDSAAlgorithm",
|
|
36
|
+
"get_algorithm",
|
|
37
|
+
]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Algorithm(ABC):
|
|
5
|
+
"""Abstract base class for JWT algorithms."""
|
|
6
|
+
|
|
7
|
+
@property
|
|
8
|
+
@abstractmethod
|
|
9
|
+
def SUPPORTED_ALGORITHMS(self) -> set[str]:
|
|
10
|
+
"""Set of supported algorithms for this implementation."""
|
|
11
|
+
|
|
12
|
+
@staticmethod
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def load_key(key: dict | bytes, password: bytes | None = None):
|
|
15
|
+
"""Load key from JWK dict or raw bytes."""
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def sign(
|
|
20
|
+
cls,
|
|
21
|
+
signing_input: bytes,
|
|
22
|
+
key: dict | bytes,
|
|
23
|
+
alg: str,
|
|
24
|
+
password: bytes | None = None,
|
|
25
|
+
) -> bytes:
|
|
26
|
+
"""Sign data using the specified algorithm."""
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
@abstractmethod
|
|
30
|
+
def verify(
|
|
31
|
+
cls,
|
|
32
|
+
signing_input: bytes,
|
|
33
|
+
signature: bytes,
|
|
34
|
+
key: dict | bytes,
|
|
35
|
+
alg: str,
|
|
36
|
+
password: bytes | None = None,
|
|
37
|
+
) -> bool:
|
|
38
|
+
"""Verify signature using the specified algorithm."""
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
|
|
3
|
+
from cryptography.hazmat.backends import default_backend
|
|
4
|
+
from cryptography.hazmat.primitives import hashes, serialization
|
|
5
|
+
from cryptography.hazmat.primitives.asymmetric import ec, utils
|
|
6
|
+
|
|
7
|
+
from .base import Algorithm
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ECDSAAlgorithm(Algorithm):
|
|
11
|
+
"""ECDSA algorithm implementation (ES256, ES384, ES512)."""
|
|
12
|
+
|
|
13
|
+
SUPPORTED_ALGORITHMS = {
|
|
14
|
+
"ES256": ec.SECP256R1,
|
|
15
|
+
"ES384": ec.SECP384R1,
|
|
16
|
+
"ES512": ec.SECP521R1,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@classmethod
|
|
20
|
+
def load_key(
|
|
21
|
+
cls, key: dict | bytes, alg: str, password: bytes | None = None
|
|
22
|
+
) -> ec.EllipticCurvePrivateKey:
|
|
23
|
+
"""
|
|
24
|
+
Load ECDSA private key from JWK dict or PEM bytes.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
key: Either a JWK dict or PEM-encoded private key bytes
|
|
28
|
+
alg: The signing algorithm (ES256, ES384, ES512)
|
|
29
|
+
password: Optional password for encrypted PEM keys
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
ECDSA private key object
|
|
33
|
+
"""
|
|
34
|
+
if alg not in cls.SUPPORTED_ALGORITHMS:
|
|
35
|
+
raise ValueError(f"Unsupported ECDSA algorithm: {alg}")
|
|
36
|
+
|
|
37
|
+
if isinstance(key, dict):
|
|
38
|
+
# Load from JWK
|
|
39
|
+
curve = cls.SUPPORTED_ALGORITHMS[alg]()
|
|
40
|
+
x = int.from_bytes(base64.urlsafe_b64decode(key["x"]), "big")
|
|
41
|
+
y = int.from_bytes(base64.urlsafe_b64decode(key["y"]), "big")
|
|
42
|
+
d = int.from_bytes(base64.urlsafe_b64decode(key["d"]), "big")
|
|
43
|
+
|
|
44
|
+
return ec.EllipticCurvePrivateNumbers(
|
|
45
|
+
d, ec.EllipticCurvePublicNumbers(x, y, curve)
|
|
46
|
+
).private_key(default_backend())
|
|
47
|
+
else:
|
|
48
|
+
# Load from PEM
|
|
49
|
+
return serialization.load_pem_private_key(
|
|
50
|
+
key, password=password, backend=default_backend()
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
@classmethod
|
|
54
|
+
def sign(
|
|
55
|
+
cls,
|
|
56
|
+
signing_input: bytes,
|
|
57
|
+
key: dict | bytes,
|
|
58
|
+
alg: str,
|
|
59
|
+
password: bytes | None = None,
|
|
60
|
+
) -> bytes:
|
|
61
|
+
"""
|
|
62
|
+
Sign using ECDSA algorithms.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
signing_input: The data to sign
|
|
66
|
+
key: Either a JWK dict or PEM-encoded private key bytes
|
|
67
|
+
alg: The signing algorithm to use (ES256, ES384, ES512)
|
|
68
|
+
password: Optional password for encrypted PEM keys
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
The signature
|
|
72
|
+
"""
|
|
73
|
+
if alg not in cls.SUPPORTED_ALGORITHMS:
|
|
74
|
+
raise ValueError(f"Unsupported ECDSA algorithm: {alg}")
|
|
75
|
+
|
|
76
|
+
privkey = cls.load_key(key, alg, password)
|
|
77
|
+
|
|
78
|
+
# Sign and format signature
|
|
79
|
+
signature = privkey.sign(signing_input, ec.ECDSA(hashes.SHA256()))
|
|
80
|
+
r, s = utils.decode_dss_signature(signature)
|
|
81
|
+
size = (privkey.curve.key_size + 7) // 8
|
|
82
|
+
return r.to_bytes(size, "big") + s.to_bytes(size, "big")
|
|
83
|
+
|
|
84
|
+
@classmethod
|
|
85
|
+
def verify(
|
|
86
|
+
cls,
|
|
87
|
+
signing_input: bytes,
|
|
88
|
+
signature: bytes,
|
|
89
|
+
key: dict | bytes,
|
|
90
|
+
alg: str,
|
|
91
|
+
password: bytes | None = None,
|
|
92
|
+
) -> bool:
|
|
93
|
+
"""
|
|
94
|
+
Verify ECDSA signature.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
signing_input: The data that was signed
|
|
98
|
+
signature: The signature to verify
|
|
99
|
+
key: Either a JWK dict or PEM-encoded public key bytes
|
|
100
|
+
alg: The signing algorithm used (ES256, ES384, ES512)
|
|
101
|
+
password: Optional password for encrypted PEM keys
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
True if signature is valid, False otherwise
|
|
105
|
+
"""
|
|
106
|
+
if alg not in cls.SUPPORTED_ALGORITHMS:
|
|
107
|
+
raise ValueError(f"Unsupported ECDSA algorithm: {alg}")
|
|
108
|
+
|
|
109
|
+
if isinstance(key, dict):
|
|
110
|
+
# Load from JWK
|
|
111
|
+
curve = cls.SUPPORTED_ALGORITHMS[alg]()
|
|
112
|
+
x = int.from_bytes(base64.urlsafe_b64decode(key["x"]), "big")
|
|
113
|
+
y = int.from_bytes(base64.urlsafe_b64decode(key["y"]), "big")
|
|
114
|
+
pubkey = ec.EllipticCurvePublicNumbers(x, y, curve).public_key(
|
|
115
|
+
default_backend()
|
|
116
|
+
)
|
|
117
|
+
else:
|
|
118
|
+
# Load from PEM
|
|
119
|
+
pubkey = serialization.load_pem_public_key(key, backend=default_backend())
|
|
120
|
+
|
|
121
|
+
try:
|
|
122
|
+
# Reconstruct signature from r and s components
|
|
123
|
+
size = (pubkey.curve.key_size + 7) // 8
|
|
124
|
+
r = int.from_bytes(signature[:size], "big")
|
|
125
|
+
s = int.from_bytes(signature[size:], "big")
|
|
126
|
+
sig = utils.encode_dss_signature(r, s)
|
|
127
|
+
|
|
128
|
+
pubkey.verify(sig, signing_input, ec.ECDSA(hashes.SHA256()))
|
|
129
|
+
return True
|
|
130
|
+
except Exception:
|
|
131
|
+
return False
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
|
|
3
|
+
from cryptography.hazmat.primitives.asymmetric import ed25519
|
|
4
|
+
|
|
5
|
+
from .base import Algorithm
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class EdDSAAlgorithm(Algorithm):
|
|
9
|
+
"""EdDSA algorithm implementation (Ed25519)."""
|
|
10
|
+
|
|
11
|
+
SUPPORTED_ALGORITHMS = {"EdDSA"}
|
|
12
|
+
|
|
13
|
+
@staticmethod
|
|
14
|
+
def load_key(
|
|
15
|
+
key: dict | bytes, password: bytes | None = None
|
|
16
|
+
) -> ed25519.Ed25519PrivateKey:
|
|
17
|
+
"""
|
|
18
|
+
Load EdDSA private key from JWK dict or raw bytes.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
key: Either a JWK dict or raw private key bytes
|
|
22
|
+
password: Optional password for encrypted keys
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
EdDSA private key object
|
|
26
|
+
"""
|
|
27
|
+
if isinstance(key, dict):
|
|
28
|
+
return ed25519.Ed25519PrivateKey.from_private_bytes(
|
|
29
|
+
base64.urlsafe_b64decode(key["d"])
|
|
30
|
+
)
|
|
31
|
+
return ed25519.Ed25519PrivateKey.from_private_bytes(key)
|
|
32
|
+
|
|
33
|
+
@classmethod
|
|
34
|
+
def sign(
|
|
35
|
+
cls,
|
|
36
|
+
signing_input: bytes,
|
|
37
|
+
key: dict | bytes,
|
|
38
|
+
alg: str,
|
|
39
|
+
password: bytes | None = None,
|
|
40
|
+
) -> bytes:
|
|
41
|
+
"""
|
|
42
|
+
Sign using EdDSA algorithm.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
signing_input: The data to sign
|
|
46
|
+
key: Either a JWK dict or raw private key bytes
|
|
47
|
+
alg: The signing algorithm to use (must be "EdDSA")
|
|
48
|
+
password: Optional password for encrypted keys
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
The signature
|
|
52
|
+
"""
|
|
53
|
+
if alg not in cls.SUPPORTED_ALGORITHMS:
|
|
54
|
+
raise ValueError(f"Unsupported EdDSA algorithm: {alg}")
|
|
55
|
+
|
|
56
|
+
privkey = cls.load_key(key, password)
|
|
57
|
+
return privkey.sign(signing_input)
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def verify(
|
|
61
|
+
cls,
|
|
62
|
+
signing_input: bytes,
|
|
63
|
+
signature: bytes,
|
|
64
|
+
key: dict | bytes,
|
|
65
|
+
alg: str,
|
|
66
|
+
password: bytes | None = None,
|
|
67
|
+
) -> bool:
|
|
68
|
+
"""
|
|
69
|
+
Verify EdDSA signature.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
signing_input: The data that was signed
|
|
73
|
+
signature: The signature to verify
|
|
74
|
+
key: Either a JWK dict or raw public key bytes
|
|
75
|
+
alg: The signing algorithm used (must be "EdDSA")
|
|
76
|
+
password: Optional password for encrypted keys
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
True if signature is valid, False otherwise
|
|
80
|
+
"""
|
|
81
|
+
if alg not in cls.SUPPORTED_ALGORITHMS:
|
|
82
|
+
raise ValueError(f"Unsupported EdDSA algorithm: {alg}")
|
|
83
|
+
|
|
84
|
+
if isinstance(key, dict):
|
|
85
|
+
pubkey = ed25519.Ed25519PublicKey.from_public_bytes(
|
|
86
|
+
base64.urlsafe_b64decode(key["x"])
|
|
87
|
+
)
|
|
88
|
+
else:
|
|
89
|
+
pubkey = ed25519.Ed25519PublicKey.from_public_bytes(key)
|
|
90
|
+
|
|
91
|
+
try:
|
|
92
|
+
pubkey.verify(signature, signing_input)
|
|
93
|
+
return True
|
|
94
|
+
except Exception:
|
|
95
|
+
return False
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
|
|
3
|
+
from cryptography.hazmat.backends import default_backend
|
|
4
|
+
from cryptography.hazmat.primitives import hashes, hmac
|
|
5
|
+
|
|
6
|
+
from .base import Algorithm
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class HMACAlgorithm(Algorithm):
|
|
10
|
+
"""HMAC algorithm implementation (HS256, HS384, HS512)."""
|
|
11
|
+
|
|
12
|
+
SUPPORTED_ALGORITHMS = {
|
|
13
|
+
"HS256": hashes.SHA256,
|
|
14
|
+
"HS384": hashes.SHA384,
|
|
15
|
+
"HS512": hashes.SHA512,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def load_key(key: dict | bytes, password: bytes | None = None) -> bytes:
|
|
20
|
+
"""
|
|
21
|
+
Load HMAC key from JWK dict or raw bytes.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
key: Either a JWK dict or raw key bytes
|
|
25
|
+
password: Optional password for encrypted keys
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
HMAC key bytes
|
|
29
|
+
"""
|
|
30
|
+
if isinstance(key, dict):
|
|
31
|
+
return base64.urlsafe_b64decode(key["k"])
|
|
32
|
+
return key
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def sign(
|
|
36
|
+
cls,
|
|
37
|
+
signing_input: bytes,
|
|
38
|
+
key: dict | bytes,
|
|
39
|
+
alg: str,
|
|
40
|
+
password: bytes | None = None,
|
|
41
|
+
) -> bytes:
|
|
42
|
+
"""
|
|
43
|
+
Sign using HMAC algorithms.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
signing_input: The data to sign
|
|
47
|
+
key: Either a JWK dict or raw key bytes
|
|
48
|
+
alg: The signing algorithm to use (HS256, HS384, HS512)
|
|
49
|
+
password: Optional password for encrypted keys
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
The signature
|
|
53
|
+
"""
|
|
54
|
+
if alg not in cls.SUPPORTED_ALGORITHMS:
|
|
55
|
+
raise ValueError(f"Unsupported HMAC algorithm: {alg}")
|
|
56
|
+
|
|
57
|
+
key_bytes = cls.load_key(key, password)
|
|
58
|
+
h = hmac.HMAC(
|
|
59
|
+
key_bytes, cls.SUPPORTED_ALGORITHMS[alg](), backend=default_backend()
|
|
60
|
+
)
|
|
61
|
+
h.update(signing_input)
|
|
62
|
+
return h.finalize()
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def verify(
|
|
66
|
+
cls,
|
|
67
|
+
signing_input: bytes,
|
|
68
|
+
signature: bytes,
|
|
69
|
+
key: dict | bytes,
|
|
70
|
+
alg: str,
|
|
71
|
+
password: bytes | None = None,
|
|
72
|
+
) -> bool:
|
|
73
|
+
"""
|
|
74
|
+
Verify HMAC signature.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
signing_input: The data that was signed
|
|
78
|
+
signature: The signature to verify
|
|
79
|
+
key: Either a JWK dict or raw key bytes
|
|
80
|
+
alg: The signing algorithm used (HS256, HS384, HS512)
|
|
81
|
+
password: Optional password for encrypted keys
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
True if signature is valid, False otherwise
|
|
85
|
+
"""
|
|
86
|
+
if alg not in cls.SUPPORTED_ALGORITHMS:
|
|
87
|
+
raise ValueError(f"Unsupported HMAC algorithm: {alg}")
|
|
88
|
+
|
|
89
|
+
key_bytes = cls.load_key(key, password)
|
|
90
|
+
h = hmac.HMAC(
|
|
91
|
+
key_bytes, cls.SUPPORTED_ALGORITHMS[alg](), backend=default_backend()
|
|
92
|
+
)
|
|
93
|
+
h.update(signing_input)
|
|
94
|
+
try:
|
|
95
|
+
h.verify(signature)
|
|
96
|
+
return True
|
|
97
|
+
except Exception:
|
|
98
|
+
return False
|