|
1 | 1 | import functools
|
2 | 2 | import json
|
3 | 3 | import time
|
| 4 | + |
| 5 | +import six |
| 6 | +from cryptography.hazmat.backends import default_backend |
| 7 | +from cryptography.hazmat.primitives import serialization |
| 8 | + |
4 | 9 | try: # Python 2
|
5 | 10 | from urlparse import urljoin
|
6 | 11 | except: # Python 3
|
@@ -124,6 +129,7 @@ def __init__(
|
124 | 129 | "private_key": "...-----BEGIN PRIVATE KEY-----...",
|
125 | 130 | "thumbprint": "A1B2C3D4E5F6...",
|
126 | 131 | "public_certificate": "...-----BEGIN CERTIFICATE-----..." (Optional. See below.)
|
| 132 | + "passphrase": "Passphrase if the private_key is encrypted (Optional)" |
127 | 133 | }
|
128 | 134 |
|
129 | 135 | *Added in version 0.5.0*:
|
@@ -252,8 +258,21 @@ def _build_client(self, client_credential, authority):
|
252 | 258 | headers = {}
|
253 | 259 | if 'public_certificate' in client_credential:
|
254 | 260 | headers["x5c"] = extract_certs(client_credential['public_certificate'])
|
| 261 | + if not client_credential.get("passphrase"): |
| 262 | + unencrypted_private_key = client_credential['private_key'] |
| 263 | + else: |
| 264 | + if isinstance(client_credential['private_key'], six.text_type): |
| 265 | + private_key = client_credential['private_key'].encode(encoding="utf-8") |
| 266 | + else: |
| 267 | + private_key = client_credential['private_key'] |
| 268 | + if isinstance(client_credential['passphrase'], six.text_type): |
| 269 | + password = client_credential['passphrase'].encode(encoding="utf-8") |
| 270 | + else: |
| 271 | + password = client_credential['passphrase'] |
| 272 | + unencrypted_private_key = serialization.load_pem_private_key( |
| 273 | + private_key, password=password, backend=default_backend()) |
255 | 274 | assertion = JwtAssertionCreator(
|
256 |
| - client_credential["private_key"], algorithm="RS256", |
| 275 | + unencrypted_private_key, algorithm="RS256", |
257 | 276 | sha1_thumbprint=client_credential.get("thumbprint"), headers=headers)
|
258 | 277 | client_assertion = assertion.create_regenerative_assertion(
|
259 | 278 | audience=authority.token_endpoint, issuer=self.client_id,
|
|
0 commit comments