apsig 0.3.2__tar.gz → 0.4.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.
- {apsig-0.3.2 → apsig-0.4.0}/COPYING.md +15 -15
- {apsig-0.3.2 → apsig-0.4.0}/LICENSE +21 -21
- {apsig-0.3.2 → apsig-0.4.0}/PKG-INFO +4 -3
- {apsig-0.3.2 → apsig-0.4.0}/README.md +17 -17
- {apsig-0.3.2 → apsig-0.4.0}/pyproject.toml +10 -3
- {apsig-0.3.2 → apsig-0.4.0}/src/apsig/__init__.py +18 -17
- {apsig-0.3.2 → apsig-0.4.0}/src/apsig/__polyfill/datetime.py +8 -8
- apsig-0.4.0/src/apsig/_version.py +1 -0
- {apsig-0.3.2 → apsig-0.4.0}/src/apsig/actor/keytools.py +129 -130
- apsig-0.4.0/src/apsig/draft/__init__.py +7 -0
- apsig-0.4.0/src/apsig/draft/sign.py +88 -0
- apsig-0.4.0/src/apsig/draft/tools.py +18 -0
- apsig-0.4.0/src/apsig/draft/verify.py +134 -0
- {apsig-0.3.2 → apsig-0.4.0}/src/apsig/exceptions.py +10 -10
- {apsig-0.3.2 → apsig-0.4.0}/src/apsig/ld_signature.py +125 -125
- {apsig-0.3.2 → apsig-0.4.0}/src/apsig/proof/sign.py +96 -96
- {apsig-0.3.2 → apsig-0.4.0}/src/apsig/proof/verify.py +130 -130
- apsig-0.4.0/tests/test_draft.py +135 -0
- {apsig-0.3.2 → apsig-0.4.0}/tests/test_ld.py +54 -54
- {apsig-0.3.2 → apsig-0.4.0}/tests/test_multibase_ed25519.py +35 -35
- {apsig-0.3.2 → apsig-0.4.0}/tests/test_proof.py +94 -94
- apsig-0.3.2/src/apsig/draft/sign.py +0 -60
- apsig-0.3.2/src/apsig/draft/verify.py +0 -91
- apsig-0.3.2/tests/test_draft.py +0 -71
- {apsig-0.3.2 → apsig-0.4.0}/tests/__init__.py +0 -0
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
# COPYRIGHT NOTICE
|
|
2
|
-
This software contains LDSignature code ported from [jointakahe/takahe](https://github.com/jointakahe/takahe). This code was originally provided under the BSD-3-Clause license, but like apsig itself is distributed under the MIT license. (This is subject to change due to requests from copyright holders, etc.)
|
|
3
|
-
|
|
4
|
-
The original code for apsig.LDSignature (from Takahe) was created by this authors:
|
|
5
|
-
|
|
6
|
-
- [Andrew Godwin](https://github.com/andrewgodwin)
|
|
7
|
-
|
|
8
|
-
Code ported (modified) by apsig from Takahe (LDSignature class)
|
|
9
|
-
- [/core/signatures.py](https://github.com/jointakahe/takahe/blob/d45f22c9c2f5e4feec9f9692c36b8385d51ec981/core/signatures.py#L289)
|
|
10
|
-
|
|
11
|
-
The original code is provided under the BSD-3-Clause license and is subject to the following terms and conditions
|
|
12
|
-
|
|
13
|
-
1. redistributions of source code must retain the original copyright notice and this license notice
|
|
14
|
-
Use, duplication, modification, or redistribution of the source code is permitted, provided that the original copyright notice is retained.
|
|
15
|
-
|
|
1
|
+
# COPYRIGHT NOTICE
|
|
2
|
+
This software contains LDSignature code ported from [jointakahe/takahe](https://github.com/jointakahe/takahe). This code was originally provided under the BSD-3-Clause license, but like apsig itself is distributed under the MIT license. (This is subject to change due to requests from copyright holders, etc.)
|
|
3
|
+
|
|
4
|
+
The original code for apsig.LDSignature (from Takahe) was created by this authors:
|
|
5
|
+
|
|
6
|
+
- [Andrew Godwin](https://github.com/andrewgodwin)
|
|
7
|
+
|
|
8
|
+
Code ported (modified) by apsig from Takahe (LDSignature class)
|
|
9
|
+
- [/core/signatures.py](https://github.com/jointakahe/takahe/blob/d45f22c9c2f5e4feec9f9692c36b8385d51ec981/core/signatures.py#L289)
|
|
10
|
+
|
|
11
|
+
The original code is provided under the BSD-3-Clause license and is subject to the following terms and conditions
|
|
12
|
+
|
|
13
|
+
1. redistributions of source code must retain the original copyright notice and this license notice
|
|
14
|
+
Use, duplication, modification, or redistribution of the source code is permitted, provided that the original copyright notice is retained.
|
|
15
|
+
|
|
16
16
|
See the [LICENSE](/LICENSE) file for the license of this entire software.
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 AmaseCocoa
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 AmaseCocoa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: apsig
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Signature implementation used in ActivityPub.
|
|
5
5
|
Author-Email: AmaseCocoa <amase.cocoa@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -20,11 +20,12 @@ Classifier: Natural Language :: English
|
|
|
20
20
|
Project-URL: Source, https://github.com/AmaseCocoa/apsig
|
|
21
21
|
Requires-Python: >=3.10
|
|
22
22
|
Requires-Dist: cryptography>=43.0.1
|
|
23
|
-
Requires-Dist: pynacl>=1.5.0
|
|
24
23
|
Requires-Dist: multiformats>=0.3.1.post4
|
|
25
24
|
Requires-Dist: jcs>=0.2.1
|
|
26
25
|
Requires-Dist: pyld[aiohttp,requests]>=2.0.4
|
|
27
|
-
Requires-Dist: pyfill>=0.1.
|
|
26
|
+
Requires-Dist: pyfill>=0.1.3
|
|
27
|
+
Requires-Dist: typing-extensions>=4.12.2
|
|
28
|
+
Requires-Dist: pytz>=2025.1
|
|
28
29
|
Description-Content-Type: text/markdown
|
|
29
30
|
|
|
30
31
|
# apsig
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
# apsig
|
|
2
|
-

|
|
3
|
-
[](https://github.com/AmaseCocoa/apsig/actions/workflows/github-code-scanning/codeql)
|
|
4
|
-
|
|
5
|
-
Signature implementation used in ActivityPub.
|
|
6
|
-
|
|
7
|
-
This library implements the creation/verification of signatures for HTTP Signatures ([draft-cavage-http-signatures-12](https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-12)), [Linked Data Signatures](https://docs.joinmastodon.org/spec/security/#ld), and Object Integrity Proofs ([FEP-8b32](https://codeberg.org/fediverse/fep/src/branch/main/fep/8b32/fep-8b32.md)).
|
|
8
|
-
## Installation
|
|
9
|
-
apsig is available on PyPI and can be installed with the following command.
|
|
10
|
-
```
|
|
11
|
-
pip install apsig
|
|
12
|
-
```
|
|
13
|
-
## Documents
|
|
14
|
-
The document can be viewed [here](https://github.com/AmaseCocoa/apsig/tree/main/docs).
|
|
15
|
-
## Thanks
|
|
16
|
-
- [Hong Minhee](https://github.com/dahlia) ([Fedify](https://fedify.dev/) Author)
|
|
17
|
-
- [Takahē](https://github.com/jointakahe/takahe) Authors (apsig.LDSignature was ported from Takahē)
|
|
1
|
+
# apsig
|
|
2
|
+

|
|
3
|
+
[](https://github.com/AmaseCocoa/apsig/actions/workflows/github-code-scanning/codeql)
|
|
4
|
+
|
|
5
|
+
Signature implementation used in ActivityPub.
|
|
6
|
+
|
|
7
|
+
This library implements the creation/verification of signatures for HTTP Signatures ([draft-cavage-http-signatures-12](https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-12)), [Linked Data Signatures](https://docs.joinmastodon.org/spec/security/#ld), and Object Integrity Proofs ([FEP-8b32](https://codeberg.org/fediverse/fep/src/branch/main/fep/8b32/fep-8b32.md)).
|
|
8
|
+
## Installation
|
|
9
|
+
apsig is available on PyPI and can be installed with the following command.
|
|
10
|
+
```
|
|
11
|
+
pip install apsig
|
|
12
|
+
```
|
|
13
|
+
## Documents
|
|
14
|
+
The document can be viewed [here](https://github.com/AmaseCocoa/apsig/tree/main/docs).
|
|
15
|
+
## Thanks
|
|
16
|
+
- [Hong Minhee](https://github.com/dahlia) ([Fedify](https://fedify.dev/) Author)
|
|
17
|
+
- [Takahē](https://github.com/jointakahe/takahe) Authors (apsig.LDSignature was ported from Takahē)
|
|
18
18
|
- And All Contributor/Users
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "apsig"
|
|
3
|
-
|
|
3
|
+
dynamic = []
|
|
4
4
|
description = "Signature implementation used in ActivityPub."
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "AmaseCocoa", email = "amase.cocoa@gmail.com" },
|
|
7
7
|
]
|
|
8
8
|
dependencies = [
|
|
9
9
|
"cryptography>=43.0.1",
|
|
10
|
-
"pynacl>=1.5.0",
|
|
11
10
|
"multiformats>=0.3.1.post4",
|
|
12
11
|
"jcs>=0.2.1",
|
|
13
12
|
"pyld[aiohttp,requests]>=2.0.4",
|
|
14
|
-
"pyfill>=0.1.
|
|
13
|
+
"pyfill>=0.1.3",
|
|
14
|
+
"typing-extensions>=4.12.2",
|
|
15
|
+
"pytz>=2025.1",
|
|
15
16
|
]
|
|
16
17
|
classifiers = [
|
|
17
18
|
"Development Status :: 4 - Beta",
|
|
@@ -30,6 +31,7 @@ classifiers = [
|
|
|
30
31
|
]
|
|
31
32
|
requires-python = ">=3.10"
|
|
32
33
|
readme = "README.md"
|
|
34
|
+
version = "0.4.0"
|
|
33
35
|
|
|
34
36
|
[project.license]
|
|
35
37
|
text = "MIT"
|
|
@@ -49,6 +51,11 @@ distribution = true
|
|
|
49
51
|
[tool.pdm.scripts]
|
|
50
52
|
test = "python -m unittest discover -s tests"
|
|
51
53
|
|
|
54
|
+
[tool.pdm.version]
|
|
55
|
+
source = "scm"
|
|
56
|
+
write_to = "apsig/_version.py"
|
|
57
|
+
write_template = "__version__ = '{}'"
|
|
58
|
+
|
|
52
59
|
[dependency-groups]
|
|
53
60
|
e2e = [
|
|
54
61
|
"aiohttp>=3.11.12",
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
from .actor.keytools import KeyUtil
|
|
2
|
-
from .draft.sign import draftSigner
|
|
3
|
-
from .draft.verify import draftVerifier
|
|
4
|
-
from .ld_signature import LDSignature
|
|
5
|
-
from .proof.sign import ProofSigner
|
|
6
|
-
from .proof.verify import ProofVerifier
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
1
|
+
from .actor.keytools import KeyUtil
|
|
2
|
+
from .draft.sign import draftSigner
|
|
3
|
+
from .draft.verify import draftVerifier
|
|
4
|
+
from .ld_signature import LDSignature
|
|
5
|
+
from .proof.sign import ProofSigner
|
|
6
|
+
from .proof.verify import ProofVerifier
|
|
7
|
+
from ._version import __version__ # noqa: F401
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"OIPSigner",
|
|
11
|
+
"OIPVerifier",
|
|
12
|
+
"ProofSigner",
|
|
13
|
+
"ProofVerifier",
|
|
14
|
+
"draftSigner",
|
|
15
|
+
"draftVerifier",
|
|
16
|
+
"LDSignature",
|
|
17
|
+
"KeyUtil"
|
|
18
|
+
]
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
import datetime
|
|
3
|
-
|
|
4
|
-
def utcnow() -> datetime.datetime:
|
|
5
|
-
if sys.version_info < (3, 11):
|
|
6
|
-
return datetime.datetime.utcnow()
|
|
7
|
-
# The line `return datetimedatetime.now(datetime.UTC)` seems to have a typo. It should be
|
|
8
|
-
# corrected to `return datetime.datetime.now(datetime.timezone.utc)`.
|
|
1
|
+
import sys
|
|
2
|
+
import datetime
|
|
3
|
+
|
|
4
|
+
def utcnow() -> datetime.datetime:
|
|
5
|
+
if sys.version_info < (3, 11):
|
|
6
|
+
return datetime.datetime.utcnow()
|
|
7
|
+
# The line `return datetimedatetime.now(datetime.UTC)` seems to have a typo. It should be
|
|
8
|
+
# corrected to `return datetime.datetime.now(datetime.timezone.utc)`.
|
|
9
9
|
return datetime.datetime.now(datetime.UTC)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '0.4.0'
|
|
@@ -1,130 +1,129 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
from cryptography.hazmat.primitives
|
|
4
|
-
from cryptography.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
self.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
ed25519.
|
|
79
|
-
|
|
|
80
|
-
| rsa.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
Exception: _description_
|
|
90
|
-
Exception: _description_
|
|
91
|
-
Exception: _description_
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
codec
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
raise ValueError("Unsupported Codec: {}".format(codec.name))
|
|
1
|
+
|
|
2
|
+
from cryptography.hazmat.primitives.asymmetric import ed25519, rsa
|
|
3
|
+
from cryptography.hazmat.primitives import serialization
|
|
4
|
+
from cryptography.exceptions import InvalidKey
|
|
5
|
+
|
|
6
|
+
from multiformats import multibase, multicodec
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class KeyUtil:
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
public_key: ed25519.Ed25519PublicKey | rsa.RSAPublicKey = None,
|
|
13
|
+
private_key: ed25519.Ed25519PrivateKey | rsa.RSAPrivateKey = None,
|
|
14
|
+
):
|
|
15
|
+
"""KeyUtil
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
public_key (ed25519.Ed25519PublicKey | rsa.RSAPublicKey, optional): Actor's Public Key. usually, auto generated public_key from private_key but, if private_key is none, must be set this.
|
|
19
|
+
private_key (ed25519.Ed25519PrivateKey | rsa.RSAPrivateKey, optional): Actor's Private Key. Defaults to None.
|
|
20
|
+
"""
|
|
21
|
+
if private_key is None:
|
|
22
|
+
if public_key is None:
|
|
23
|
+
raise KeyError("If private_key is None, public_key must be set.")
|
|
24
|
+
else:
|
|
25
|
+
self.public_key = public_key
|
|
26
|
+
else:
|
|
27
|
+
self.private_key = private_key
|
|
28
|
+
self.public_key = private_key.public_key()
|
|
29
|
+
|
|
30
|
+
def encode_multibase(self, private: bool = False) -> str:
|
|
31
|
+
"""multibase encode the key.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
private (bool, optional): If true, decodes/encodes the private key, not the public key. Defaults to False.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
str: multibase encoded string.
|
|
38
|
+
"""
|
|
39
|
+
if not private:
|
|
40
|
+
if isinstance(self.public_key, rsa.RSAPublicKey):
|
|
41
|
+
prefixed = multicodec.wrap(
|
|
42
|
+
"rsa-pub",
|
|
43
|
+
self.public_key.public_bytes(
|
|
44
|
+
encoding=serialization.Encoding.DER,
|
|
45
|
+
format=serialization.PublicFormat.PKCS1,
|
|
46
|
+
).hex(),
|
|
47
|
+
)
|
|
48
|
+
prefixed = multicodec.wrap(
|
|
49
|
+
"ed25519-pub",
|
|
50
|
+
self.public_key.public_bytes(
|
|
51
|
+
encoding=serialization.Encoding.Raw,
|
|
52
|
+
format=serialization.PublicFormat.Raw,
|
|
53
|
+
),
|
|
54
|
+
)
|
|
55
|
+
else:
|
|
56
|
+
if isinstance(self.private_key, rsa.RSAPrivateKey):
|
|
57
|
+
prefixed = multicodec.wrap(
|
|
58
|
+
"rsa-priv",
|
|
59
|
+
self.public_key.public_bytes(
|
|
60
|
+
encoding=serialization.Encoding.DER,
|
|
61
|
+
format=serialization.PublicFormat.PKCS1,
|
|
62
|
+
).hex(),
|
|
63
|
+
)
|
|
64
|
+
else:
|
|
65
|
+
prefixed = multicodec.wrap(
|
|
66
|
+
"ed25519-priv",
|
|
67
|
+
self.public_key.public_bytes(
|
|
68
|
+
encoding=serialization.Encoding.Raw,
|
|
69
|
+
format=serialization.PublicFormat.Raw,
|
|
70
|
+
),
|
|
71
|
+
)
|
|
72
|
+
return multibase.encode(prefixed, "base58btc")
|
|
73
|
+
|
|
74
|
+
def decode_multibase(
|
|
75
|
+
self, data: str
|
|
76
|
+
) -> (
|
|
77
|
+
ed25519.Ed25519PublicKey
|
|
78
|
+
| ed25519.Ed25519PrivateKey
|
|
79
|
+
| rsa.RSAPublicKey
|
|
80
|
+
| rsa.RSAPrivateKey
|
|
81
|
+
):
|
|
82
|
+
"""Get Public/PrivateKey from Multibase.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
data (str): multibase data.
|
|
86
|
+
|
|
87
|
+
Raises:
|
|
88
|
+
Exception: _description_
|
|
89
|
+
Exception: _description_
|
|
90
|
+
Exception: _description_
|
|
91
|
+
Exception: _description_
|
|
92
|
+
ValueError: _description_
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
ed25519.Ed25519PublicKey | ed25519.Ed25519PrivateKey | rsa.RSAPublicKey | rsa.RSAPrivateKey: Loaded Key Object.
|
|
96
|
+
"""
|
|
97
|
+
decoded = multibase.decode(data)
|
|
98
|
+
codec, data = multicodec.unwrap(decoded)
|
|
99
|
+
if codec.name == "ed25519-pub":
|
|
100
|
+
try:
|
|
101
|
+
return ed25519.Ed25519PublicKey.from_public_bytes(data)
|
|
102
|
+
except InvalidKey:
|
|
103
|
+
raise Exception(
|
|
104
|
+
"Invalid ed25519 public key passed."
|
|
105
|
+
) # Tempolary, will replaced apsig's exception
|
|
106
|
+
elif codec.name == "rsa-pub":
|
|
107
|
+
try:
|
|
108
|
+
rsa.RSAPublicKey
|
|
109
|
+
return serialization.load_der_public_key(data)
|
|
110
|
+
except ValueError:
|
|
111
|
+
raise Exception(
|
|
112
|
+
"Invalid rsa public key passed."
|
|
113
|
+
) # Tempolary, will replaced apsig's exception
|
|
114
|
+
if codec.name == "ed25519-priv":
|
|
115
|
+
try:
|
|
116
|
+
return ed25519.Ed25519PrivateKey.from_private_bytes(data)
|
|
117
|
+
except InvalidKey:
|
|
118
|
+
raise Exception(
|
|
119
|
+
"Invalid ed25519 private key passed."
|
|
120
|
+
) # Tempolary, will replaced apsig's exception
|
|
121
|
+
elif codec.name == "rsa-priv":
|
|
122
|
+
try:
|
|
123
|
+
return serialization.load_der_private_key(data)
|
|
124
|
+
except ValueError:
|
|
125
|
+
raise Exception(
|
|
126
|
+
"Invalid rsa private key passed."
|
|
127
|
+
) # Tempolary, will replaced apsig's exception
|
|
128
|
+
else:
|
|
129
|
+
raise ValueError("Unsupported Codec: {}".format(codec.name))
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
from typing_extensions import deprecated
|
|
3
|
+
from cryptography.hazmat.primitives.asymmetric import padding, rsa
|
|
4
|
+
from cryptography.hazmat.primitives import hashes
|
|
5
|
+
|
|
6
|
+
import base64
|
|
7
|
+
from urllib.parse import urlparse, ParseResult
|
|
8
|
+
import email.utils
|
|
9
|
+
|
|
10
|
+
from .tools import calculate_digest, build_string
|
|
11
|
+
|
|
12
|
+
class draftSigner:
|
|
13
|
+
@staticmethod
|
|
14
|
+
@deprecated("apsig.draft.sign.draftSigner is deprecated; use apsig.draft.sign.Signer instead. This will be removed in apsig 1.0.")
|
|
15
|
+
def sign(private_key: rsa.RSAPrivateKey, method: str, url: str, headers: dict, key_id: str, body: bytes=b"") -> dict:
|
|
16
|
+
signer = Signer(headers=headers, private_key=private_key, method=method, url=url, key_id=key_id, body=body)
|
|
17
|
+
return signer.sign()
|
|
18
|
+
|
|
19
|
+
class Signer:
|
|
20
|
+
def __init__(self, headers: dict[Any, Any], private_key: rsa.RSAPrivateKey, method: str, url: str, key_id: str, body: bytes=b"") -> None:
|
|
21
|
+
"""Signs an HTTP request with a digital signature.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
private_key (rsa.RSAPrivateKey): The RSA private key used to sign the request.
|
|
25
|
+
method (str): The HTTP method (e.g., "GET", "POST").
|
|
26
|
+
url (str): The URL of the request.
|
|
27
|
+
headers (dict): A dictionary of HTTP headers that will be signed.
|
|
28
|
+
key_id (str): The key identifier to include in the signature header.
|
|
29
|
+
body (bytes, optional): The request body. Defaults to an empty byte string.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
dict: The HTTP headers with the signature added.
|
|
33
|
+
|
|
34
|
+
Raises:
|
|
35
|
+
ValueError: If the signing process fails due to invalid parameters.
|
|
36
|
+
"""
|
|
37
|
+
if not headers.get("date") and not headers.get("Date"):
|
|
38
|
+
headers["date"] = email.utils.formatdate(usegmt=True)
|
|
39
|
+
self.parsed_url: ParseResult = urlparse(url)
|
|
40
|
+
self.headers = {
|
|
41
|
+
**headers,
|
|
42
|
+
"(request-target)": f"{method.lower()} {self.parsed_url.path}"
|
|
43
|
+
}
|
|
44
|
+
self.private_key = private_key
|
|
45
|
+
self.method = method
|
|
46
|
+
self.url = url
|
|
47
|
+
self.key_id = key_id
|
|
48
|
+
self.body = body
|
|
49
|
+
|
|
50
|
+
if not self.headers.get("Host"):
|
|
51
|
+
self.headers["Host"] = self.parsed_url.netloc
|
|
52
|
+
|
|
53
|
+
self.__generate_digest(self.body)
|
|
54
|
+
|
|
55
|
+
def __generate_sign_header(self, signature: str):
|
|
56
|
+
self.headers["Signature"] = signature
|
|
57
|
+
self.headers["Authorization"] = f"Signature {signature}"
|
|
58
|
+
|
|
59
|
+
def __sign_document(self, document: bytes):
|
|
60
|
+
return base64.standard_b64encode(self.private_key.sign(document, padding.PKCS1v15(), hashes.SHA256())).decode("utf-8")
|
|
61
|
+
|
|
62
|
+
def __generate_digest(self, body: bytes | str):
|
|
63
|
+
if not self.headers.get("digest") and not self.headers.get("Digest"):
|
|
64
|
+
self.headers["digest"] = calculate_digest(body)
|
|
65
|
+
else:
|
|
66
|
+
return self.headers.get("digest")
|
|
67
|
+
|
|
68
|
+
def build_signature(self, key_id: str, signature: str, algorithm: str = "rsa-sha256"):
|
|
69
|
+
if algorithm != "rsa-sha256":
|
|
70
|
+
raise NotImplementedError(f"Unsuppored algorithm: {algorithm}")
|
|
71
|
+
|
|
72
|
+
return ",".join([
|
|
73
|
+
f'keyId="{key_id}"',
|
|
74
|
+
f'algorithm="{algorithm}"',
|
|
75
|
+
f'headers="{" ".join(key.lower() for key in self.headers.keys())}"',
|
|
76
|
+
f'signature="{signature}"'
|
|
77
|
+
])
|
|
78
|
+
|
|
79
|
+
def sign(self) -> dict:
|
|
80
|
+
signature_string = build_string(self.headers).encode("utf-8")
|
|
81
|
+
signature = self.__sign_document(signature_string)
|
|
82
|
+
signed = self.build_signature(self.key_id, signature)
|
|
83
|
+
self.__generate_sign_header(signed)
|
|
84
|
+
|
|
85
|
+
headers = self.headers.copy()
|
|
86
|
+
headers.pop("(request-target)")
|
|
87
|
+
|
|
88
|
+
return headers
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
|
|
5
|
+
def calculate_digest(body: bytes | str):
|
|
6
|
+
digest = hashlib.sha256(body.encode("utf-8") if isinstance(body, str) else body).digest()
|
|
7
|
+
hash_bytes = digest
|
|
8
|
+
return "SHA-256=" + base64.standard_b64encode(hash_bytes).decode("utf-8")
|
|
9
|
+
|
|
10
|
+
def build_string(strings: dict, headers: list = []) -> str:
|
|
11
|
+
if headers:
|
|
12
|
+
header_list = []
|
|
13
|
+
for key in headers:
|
|
14
|
+
header_list.append(f"{key}: {strings[key.lower()]}")
|
|
15
|
+
result = "\n".join(header_list)
|
|
16
|
+
else:
|
|
17
|
+
result = "\n".join(f"{key.lower()}: {value}" for key, value in strings.items())
|
|
18
|
+
return result
|