Skip to content

Commit d5c93e4

Browse files
ServiceApplicationClient: Add extra_jwt_headers
1 parent eddb461 commit d5c93e4

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

oauthlib/oauth2/rfc6749/clients/service_application.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def prepare_request_body(self,
6868
audience=None,
6969
expires_at=None,
7070
issued_at=None,
71+
extra_jwt_headers=None,
7172
extra_claims=None,
7273
body='',
7374
scope=None,
@@ -96,7 +97,11 @@ def prepare_request_body(self,
9697
:param issued_at: A unix timestamp of when the JWT was created.
9798
Defaults to now, i.e. ``time.time()``.
9899
99-
:param extra_claims: A dict of additional claims to include in the JWT.
100+
:param extra_jwt_headers: A dict of additional headers to include
101+
in the JWT header.
102+
103+
:param extra_claims: A dict of additional claims to include
104+
in the JWT payload.
100105
101106
:param body: Existing request body (URL encoded string) to embed parameters
102107
into. This may contain extra parameters. Default ''.
@@ -176,7 +181,7 @@ def prepare_request_body(self,
176181

177182
claim.update(extra_claims or {})
178183

179-
assertion = jwt.encode(claim, key, 'RS256')
184+
assertion = jwt.encode(claim, key, 'RS256', extra_jwt_headers)
180185
assertion = to_unicode(assertion)
181186

182187
kwargs['client_id'] = self.client_id

tests/oauth2/rfc6749/clients/test_service_application.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,24 @@ def test_request_body(self, t):
114114
# Optional kwargs
115115
not_before = time() - 3600
116116
jwt_id = '8zd15df4s35f43sd'
117+
extra_jwt_headers = {'extra': 'header'}
118+
extra_claims = {'extra': 'claim'}
117119
body = client.prepare_request_body(issuer=self.issuer,
118120
subject=self.subject,
119121
audience=self.audience,
120122
body=self.body,
121123
not_before=not_before,
124+
extra_jwt_headers=extra_jwt_headers,
125+
extra_claims=extra_claims,
122126
jwt_id=jwt_id)
123127

124128
r = Request('https://a.b', body=body)
125129
self.assertEqual(r.isnot, 'empty')
126130
self.assertEqual(r.grant_type, ServiceApplicationClient.grant_type)
127131

128-
claim = jwt.decode(r.assertion, self.public_key, audience=self.audience, algorithms=['RS256'])
132+
token = jwt.api_jwt.decode_complete(r.assertion, self.public_key, audience=self.audience, algorithms=['RS256'])
133+
header = token['header']
134+
claim = token['payload']
129135

130136
self.assertEqual(claim['iss'], self.issuer)
131137
# audience verification is handled during decode now
@@ -134,6 +140,9 @@ def test_request_body(self, t):
134140
self.assertEqual(claim['nbf'], not_before)
135141
self.assertEqual(claim['jti'], jwt_id)
136142

143+
self.assertLessEqual(extra_jwt_headers.items(), header.items())
144+
self.assertLessEqual(extra_claims.items(), claim.items())
145+
137146
@patch('time.time')
138147
def test_request_body_no_initial_private_key(self, t):
139148
t.return_value = time()

0 commit comments

Comments
 (0)