@@ -119,7 +119,29 @@ def _verify_signature(message, signature, certs):
119
119
raise AppIdentityError ('Invalid token signature' )
120
120
121
121
122
- def verify_signed_jwt_with_certs (jwt , certs , audience ):
122
+ def _check_audience (payload_dict , audience ):
123
+ """Checks audience field from a JWT payload.
124
+
125
+ Does nothing if the passed in ``audience`` is null.
126
+
127
+ Args:
128
+ payload_dict: dict, A dictionary containing a JWT payload.
129
+ audience: string or NoneType, an audience to check for in
130
+ the JWT payload.
131
+ """
132
+ if audience is None :
133
+ return
134
+
135
+ audience_in_payload = payload_dict .get ('aud' )
136
+ if audience_in_payload is None :
137
+ raise AppIdentityError ('No aud field in token: %s' %
138
+ (payload_dict ,))
139
+ if audience_in_payload != audience :
140
+ raise AppIdentityError ('Wrong recipient, %s != %s: %s' %
141
+ (audience_in_payload , audience , payload_dict ))
142
+
143
+
144
+ def verify_signed_jwt_with_certs (jwt , certs , audience = None ):
123
145
"""Verify a JWT against public certs.
124
146
125
147
See http://self-issued.info/docs/draft-jones-json-web-token.html.
@@ -180,13 +202,6 @@ def verify_signed_jwt_with_certs(jwt, certs, audience):
180
202
(now , latest , payload_bytes ))
181
203
182
204
# Check audience.
183
- if audience is not None :
184
- aud = payload_dict .get ('aud' )
185
- if aud is None :
186
- raise AppIdentityError ('No aud field in token: %s' %
187
- (payload_bytes ,))
188
- if aud != audience :
189
- raise AppIdentityError ('Wrong recipient, %s != %s: %s' %
190
- (aud , audience , payload_bytes ))
205
+ _check_audience (payload_dict , audience )
191
206
192
207
return payload_dict
0 commit comments