Skip to content

Commit 7982683

Browse files
authored
feat: use InvalidHeaderValueError in registry header validation (#47)
1 parent cf0ad49 commit 7982683

File tree

10 files changed

+398
-213
lines changed

10 files changed

+398
-213
lines changed

docs/guide/jwt.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Algorithms & Registry
291291

292292
The :meth:`encode` and :meth:`decode` accept an ``algorithms`` parameter for
293293
specifying the allowed algorithms. By default, it only allows your to use
294-
recommended algorithms.
294+
**recommended** algorithms.
295295

296296
You can find out the recommended algorithms at:
297297

docs/guide/registry.rst

+15-15
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ the value type.
7373
>>> jws.serialize_compact({"alg": "HS256", "kid": 123}, "hello", key)
7474
Traceback (most recent call last):
7575
File "<stdin>", line 1, in <module>
76-
File "$/joserfc/jws.py", line 98, in serialize_compact
76+
File ".../joserfc/jws.py", line 111, in serialize_compact
7777
registry.check_header(protected)
78-
File "$/joserfc/rfc7515/registry.py", line 63, in check_header
78+
File ".../joserfc/rfc7515/registry.py", line 68, in check_header
7979
validate_registry_header(self.header_registry, header)
80-
File "$/joserfc/registry.py", line 193, in validate_registry_header
81-
raise ValueError(f'"{key}" in header {error}')
82-
ValueError: "kid" in header must be a str
80+
File ".../joserfc/registry.py", line 194, in validate_registry_header
81+
raise InvalidHeaderValueError(f"'{key}' in header {error}")
82+
joserfc.errors.InvalidHeaderValueError: invalid_header_value: 'kid' in header must be a str
8383
8484
In the above example, ``kid`` MUST be a string instead of an integer. The default
8585
registry validates the ``kid`` before processing the serialization.
@@ -99,13 +99,13 @@ indicating that they must be present. For example:
9999
>>> jws.serialize_compact({"alg": "HS256", "crit": ["kid"]}, "hello", key)
100100
Traceback (most recent call last):
101101
File "<stdin>", line 1, in <module>
102-
File "$/joserfc/jws.py", line 98, in serialize_compact
102+
File ".../joserfc/jws.py", line 111, in serialize_compact
103103
registry.check_header(protected)
104-
File "$/joserfc/rfc7515/registry.py", line 62, in check_header
104+
File ".../joserfc/rfc7515/registry.py", line 67, in check_header
105105
check_crit_header(header)
106-
File "$/joserfc/registry.py", line 195, in check_crit_header
107-
raise ValueError(f'"{k}" is a critical header')
108-
ValueError: "kid" is a critical header
106+
File ".../joserfc/registry.py", line 202, in check_crit_header
107+
raise MissingCritHeaderError(k)
108+
joserfc.errors.MissingCritHeaderError: missing_crit_header: Missing critical 'kid' value in header
109109
110110
Since "kid" is listed as a critical (``crit``) header parameter, it is mandatory
111111
and must be included in the header.
@@ -124,13 +124,13 @@ Any additional header beyond those supported by the algorithm will result in an
124124
>>> jws.serialize_compact({"alg": "HS256", "custom": "hi"}, "hello", key)
125125
Traceback (most recent call last):
126126
File "<stdin>", line 1, in <module>
127-
File "/home/lepture/authlib/joserfc/src/joserfc/jws.py", line 98, in serialize_compact
127+
File ".../joserfc/jws.py", line 111, in serialize_compact
128128
registry.check_header(protected)
129-
File "/home/lepture/authlib/joserfc/src/joserfc/rfc7515/registry.py", line 65, in check_header
129+
File ".../joserfc/rfc7515/registry.py", line 70, in check_header
130130
check_supported_header(self.header_registry, header)
131-
File "/home/lepture/authlib/joserfc/src/joserfc/registry.py", line 175, in check_supported_header
132-
raise ValueError(f'Unsupported "{unsupported_keys} in header')
133-
ValueError: Unsupported {'custom'} in header
131+
File ".../joserfc/registry.py", line 183, in check_supported_header
132+
raise UnsupportedHeaderError(f"Unsupported {unsupported_keys} in header")
133+
joserfc.errors.UnsupportedHeaderError: unsupported_header: Unsupported {'custom'} in header
134134
135135
To resolve this error, you have two options. First, you can register the
136136
additional header parameters with the registry. This allows the registry

docs/locales/zh/LC_MESSAGES/api.po

+79-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: joserfc\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2025-02-28 11:54+0900\n"
11+
"POT-Creation-Date: 2025-04-20 18:35+0900\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language: zh\n"
@@ -43,25 +43,45 @@ msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
4343
#: joserfc.errors.InvalidEncryptedKeyError.error:1
4444
#: joserfc.errors.InvalidEncryptionAlgorithmError.error:1
4545
#: joserfc.errors.InvalidExchangeKeyError.error:1
46+
#: joserfc.errors.InvalidHeaderValueError.error:1
47+
#: joserfc.errors.InvalidKeyIdError.error:1
4648
#: joserfc.errors.InvalidKeyLengthError.error:1
4749
#: joserfc.errors.InvalidKeyTypeError.error:1
4850
#: joserfc.errors.InvalidPayloadError.error:1
4951
#: joserfc.errors.InvalidTokenError.error:1 joserfc.errors.JoseError.error:1
5052
#: joserfc.errors.MissingAlgorithmError.error:1
5153
#: joserfc.errors.MissingClaimError.error:1
54+
#: joserfc.errors.MissingCritHeaderError.error:1
5255
#: joserfc.errors.MissingEncryptionError.error:1
56+
#: joserfc.errors.MissingHeaderError.error:1
57+
#: joserfc.errors.MissingKeyError.error:1
58+
#: joserfc.errors.MissingKeyTypeError.error:1
59+
#: joserfc.errors.UnsupportedAlgorithmError.error:1
60+
#: joserfc.errors.UnsupportedHeaderError.error:1
5361
#: joserfc.errors.UnsupportedKeyAlgorithmError.error:1
5462
#: joserfc.errors.UnsupportedKeyOperationError.error:1
5563
#: joserfc.errors.UnsupportedKeyUseError.error:1 of
5664
msgid "short-string error code"
5765
msgstr "短字符串错误代码"
5866

67+
#: joserfc.errors.DecodeError:1 of
68+
#, fuzzy
69+
msgid ""
70+
"This error is designed for JWS/JWE. It is raised when deserialization and"
71+
" decryption fails."
72+
msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
73+
5974
#: joserfc.errors.ExceededSizeError:1 of
6075
msgid ""
6176
"This error is designed for DEF zip algorithm. It raised when the "
6277
"compressed data exceeds the maximum allowed length."
6378
msgstr "该错误是为 DEF 压缩算法设计的,当压缩数据超过允许的最大长度时触发。"
6479

80+
#: joserfc.errors.ExpiredTokenError:1 of
81+
#, fuzzy
82+
msgid "This error is designed for JWT. It raised when the token is expired."
83+
msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
84+
6585
#: ../../docstring joserfc.errors.ExpiredTokenError.description:1
6686
#: joserfc.errors.InvalidCEKLengthError.description:1
6787
#: joserfc.errors.InvalidEncryptedKeyError.description:1
@@ -73,16 +93,64 @@ msgstr "该错误是为 DEF 压缩算法设计的,当压缩数据超过允许
7393
msgid "long-string to describe this error"
7494
msgstr "描述此错误的长字符串"
7595

96+
#: joserfc.errors.InsecureClaimError:1 of
97+
#, fuzzy
98+
msgid ""
99+
"This error is designed for JWT. It raised when the claim contains "
100+
"sensitive information."
101+
msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
102+
103+
#: joserfc.errors.InvalidClaimError:1 of
104+
#, fuzzy
105+
msgid ""
106+
"This error is designed for JWT. It raised when the claim contains invalid"
107+
" values or types."
108+
msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
109+
76110
#: joserfc.errors.InvalidEncryptionAlgorithmError:1 of
77111
msgid ""
78112
"This error is designed for JWE. It is raised when \"enc\" value does not "
79113
"work together with \"alg\" value."
80114
msgstr "该错误是为 JWE 设计的,当 \"enc\" 值与 \"alg\" 值不兼容时触发。"
81115

116+
#: joserfc.errors.InvalidPayloadError:1 of
117+
#, fuzzy
118+
msgid ""
119+
"This error is designed for JWT. It raised when the payload is not a valid"
120+
" JSON object."
121+
msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
122+
123+
#: joserfc.errors.InvalidTokenError:1 of
124+
#, fuzzy
125+
msgid "This error is designed for JWT. It raised when the token is not valid yet."
126+
msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
127+
82128
#: joserfc.errors.JoseError:1 of
83129
msgid "Base Exception for all errors in joserfc."
84130
msgstr "joserfc 中所有错误的基类异常。"
85131

132+
#: joserfc.errors.MissingClaimError:1 of
133+
#, fuzzy
134+
msgid ""
135+
"This error is designed for JWT. It raised when the required claims are "
136+
"missing."
137+
msgstr "该错误是为 JWS/JWT 设计的,当签名不匹配时触发。"
138+
139+
#: joserfc.errors.MissingCritHeaderError:1 of
140+
msgid "This error happens when the critical header does not exist."
141+
msgstr ""
142+
143+
#: joserfc.errors.MissingEncryptionError:1 of
144+
#, fuzzy
145+
msgid ""
146+
"This error is designed for JWE. It is raised when the 'enc' value in "
147+
"header is missing."
148+
msgstr "该错误是为 JWE 设计的,当 \"enc\" 值与 \"alg\" 值不兼容时触发。"
149+
150+
#: joserfc.errors.MissingHeaderError:1 of
151+
msgid "This error happens when the required header does not exist."
152+
msgstr ""
153+
86154
#: ../../api/index.rst:2
87155
msgid "API References"
88156
msgstr "API 参考"
@@ -729,8 +797,8 @@ msgid ""
729797
"represents digitally signed or MACed content as a JSON object. This "
730798
"representation is neither optimized for compactness nor URL-safe."
731799
msgstr ""
732-
"生成 JWS JSON 序列化(字典形式)。JWS JSON 序列化将数字签名或 MAC 内容表示为 JSON 对象。"
733-
"此表示既不优化紧凑性,也不 URL 安全。"
800+
"生成 JWS JSON 序列化(字典形式)。JWS JSON 序列化将数字签名或 MAC 内容表示为 JSON 对象。此表示既不优化紧凑性,也不 "
801+
"URL 安全。"
734802

735803
#: joserfc.jws.serialize_json:5 of
736804
msgid "A general JWS JSON Serialization contains:"
@@ -876,6 +944,10 @@ msgid "a ``JWSRegistry`` or ``JWERegistry`` to use"
876944
msgstr "要使用的 ``JWSRegistry`` 或 ``JWERegistry``"
877945

878946
#: joserfc.jwt.decode:8 of
947+
msgid "A JSONDecoder subclass to use"
948+
msgstr ""
949+
950+
#: joserfc.jwt.decode:9 of
879951
msgid "BadSignatureError"
880952
msgstr "BadSignatureError"
881953

@@ -895,3 +967,7 @@ msgstr "用来编码的字典形式的 JWT claims 部分"
895967
msgid "key used to sign the signature"
896968
msgstr "用于签名的密钥"
897969

970+
#: joserfc.jwt.encode:8 of
971+
msgid "A JSONEncoder subclass to use"
972+
msgstr ""
973+

0 commit comments

Comments
 (0)