Skip to content

Commit 2c2ca5d

Browse files
committed
fix: remove duplicate code
1 parent 454a55e commit 2c2ca5d

1 file changed

Lines changed: 20 additions & 32 deletions

File tree

src/joserfc/_rfc7518/jws_algs.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,15 @@ def verify(self, msg: bytes, sig: bytes, key: OctKey) -> bool:
7070
return hmac.compare_digest(sig, v_sig)
7171

7272

73-
class RSAAlgorithm(JWSAlgModel):
74-
"""RSA using SHA algorithms for JWS. Available algorithms:
75-
76-
- RS256: RSASSA-PKCS1-v1_5 using SHA-256
77-
- RS384: RSASSA-PKCS1-v1_5 using SHA-384
78-
- RS512: RSASSA-PKCS1-v1_5 using SHA-512
79-
"""
80-
73+
class _RSAAlgModel(JWSAlgModel):
8174
key_type = "RSA"
8275

8376
SHA256 = hashes.SHA256
8477
SHA384 = hashes.SHA384
8578
SHA512 = hashes.SHA512
86-
padding = padding.PKCS1v15()
79+
padding: padding.AsymmetricPadding
8780

8881
def __init__(self, sha_type: t.Literal[256, 384, 512], recommended: bool = False):
89-
self.name = f"RS{sha_type}"
90-
self.description = f"RSASSA-PKCS1-v1_5 using SHA-{sha_type}"
9182
self.recommended = recommended
9283
self.hash_alg = getattr(self, f"SHA{sha_type}")
9384
self.algorithm_security = sha_type
@@ -105,6 +96,22 @@ def verify(self, msg: bytes, sig: bytes, key: RSAKey) -> bool:
10596
return False
10697

10798

99+
class RSAAlgorithm(_RSAAlgModel):
100+
"""RSA using SHA algorithms for JWS. Available algorithms:
101+
102+
- RS256: RSASSA-PKCS1-v1_5 using SHA-256
103+
- RS384: RSASSA-PKCS1-v1_5 using SHA-384
104+
- RS512: RSASSA-PKCS1-v1_5 using SHA-512
105+
"""
106+
107+
padding = padding.PKCS1v15()
108+
109+
def __init__(self, sha_type: t.Literal[256, 384, 512], recommended: bool = False):
110+
super().__init__(sha_type, recommended)
111+
self.name = f"RS{sha_type}"
112+
self.description = f"RSASSA-PKCS1-v1_5 using SHA-{sha_type}"
113+
114+
108115
class ESAlgorithm(JWSAlgModel):
109116
"""ECDSA using SHA algorithms for JWS. Available algorithms:
110117
@@ -158,38 +165,19 @@ def verify(self, msg: bytes, sig: bytes, key: ECKey) -> bool:
158165
return False
159166

160167

161-
class RSAPSSAlgorithm(JWSAlgModel):
168+
class RSAPSSAlgorithm(_RSAAlgModel):
162169
"""RSASSA-PSS using SHA algorithms for JWS. Available algorithms:
163170
164171
- PS256: RSASSA-PSS using SHA-256 and MGF1 with SHA-256
165172
- PS384: RSASSA-PSS using SHA-384 and MGF1 with SHA-384
166173
- PS512: RSASSA-PSS using SHA-512 and MGF1 with SHA-512
167174
"""
168175

169-
key_type = "RSA"
170-
171-
SHA256 = hashes.SHA256
172-
SHA384 = hashes.SHA384
173-
SHA512 = hashes.SHA512
174-
175176
def __init__(self, sha_type: t.Literal[256, 384, 512]):
177+
super().__init__(sha_type, False)
176178
self.name = f"PS{sha_type}"
177179
self.description = f"RSASSA-PSS using SHA-{sha_type} and MGF1 with SHA-{sha_type}"
178-
self.hash_alg = getattr(self, f"SHA{sha_type}")
179180
self.padding = padding.PSS(mgf=padding.MGF1(self.hash_alg()), salt_length=self.hash_alg.digest_size)
180-
self.algorithm_security = sha_type
181-
182-
def sign(self, msg: bytes, key: RSAKey) -> bytes:
183-
op_key = key.get_op_key("sign")
184-
return op_key.sign(msg, self.padding, self.hash_alg())
185-
186-
def verify(self, msg: bytes, sig: bytes, key: RSAKey) -> bool:
187-
op_key = key.get_op_key("verify")
188-
try:
189-
op_key.verify(sig, msg, self.padding, self.hash_alg())
190-
return True
191-
except InvalidSignature:
192-
return False
193181

194182

195183
JWS_ALGORITHMS: list[JWSAlgModel] = [

0 commit comments

Comments
 (0)