-
Notifications
You must be signed in to change notification settings - Fork 208
Thumbprint for certificate made optional #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rayluo
merged 2 commits into
AzureAD:dev
from
vi7us:vitcurda/20250624/cert-thumbprint-made-optional
Nov 1, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,3 +62,6 @@ msal_cache.bin | |
|
|
||
| .env | ||
| .perf.baseline | ||
|
|
||
| *.pfx | ||
| .vscode/settings.json | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| import unittest | ||
| from unittest.mock import Mock, patch | ||
| from msal.application import ConfidentialClientApplication | ||
|
|
||
|
|
||
| @patch('msal.application.Authority') | ||
| @patch('msal.application.JwtAssertionCreator', new_callable=lambda: Mock( | ||
| return_value=Mock(create_regenerative_assertion=Mock(return_value="mock_jwt_assertion")))) | ||
| class TestClientCredentialWithOptionalThumbprint(unittest.TestCase): | ||
| """Test that thumbprint is optional when public_certificate is provided""" | ||
|
|
||
| # Sample test certificate and private key (PEM format) | ||
| # These are minimal valid PEM structures for testing | ||
| test_private_key = """-----BEGIN PRIVATE KEY----- | ||
| MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7VJTUt9Us8cKj | ||
| MzEfYyjiWA4R4/M2bS1+fWIcPm15j7uo6xKvRr4PNx5bKMDFqMdW6/xfqFWX0nZK | ||
| -----END PRIVATE KEY-----""" | ||
|
|
||
| test_certificate = """-----BEGIN CERTIFICATE----- | ||
| MIIC5jCCAc6gAwIBAgIJALdYQVsVsNZHMA0GCSqGSIb3DQEBCwUAMBYxFDASBgNV | ||
| BAMMC0V4YW1wbGUgQ0EwHhcNMjQwMTAxMDAwMDAwWhcNMjUwMTAxMDAwMDAwWjAW | ||
| -----END CERTIFICATE-----""" | ||
|
|
||
| def _setup_mocks(self, mock_authority_class, authority="https://login.microsoftonline.com/common"): | ||
| """Helper to setup Authority mock""" | ||
| # Setup Authority mock | ||
| mock_authority = Mock() | ||
| mock_authority.is_adfs = "adfs" in authority.lower() | ||
|
|
||
| # Extract instance from authority URL | ||
| if mock_authority.is_adfs: | ||
| # For ADFS: https://adfs.contoso.com/adfs -> adfs.contoso.com | ||
| mock_authority.instance = authority.split("//")[1].split("/")[0] | ||
| mock_authority.token_endpoint = f"https://{mock_authority.instance}/adfs/oauth2/token" | ||
| mock_authority.authorization_endpoint = f"https://{mock_authority.instance}/adfs/oauth2/authorize" | ||
| else: | ||
| # For AAD: https://login.microsoftonline.com/common -> login.microsoftonline.com | ||
| mock_authority.instance = authority.split("//")[1].split("/")[0] | ||
| mock_authority.token_endpoint = f"https://{mock_authority.instance}/common/oauth2/v2.0/token" | ||
| mock_authority.authorization_endpoint = f"https://{mock_authority.instance}/common/oauth2/v2.0/authorize" | ||
|
|
||
| mock_authority.device_authorization_endpoint = None | ||
| mock_authority_class.return_value = mock_authority | ||
|
|
||
| return mock_authority | ||
|
|
||
| def _setup_certificate_mocks(self, mock_extract, mock_load_cert): | ||
| """Helper to setup certificate parsing mocks""" | ||
| # Mock certificate loading | ||
| mock_cert = Mock() | ||
| mock_load_cert.return_value = mock_cert | ||
|
|
||
| # Mock _extract_cert_and_thumbprints to return thumbprints | ||
| mock_extract.return_value = ( | ||
| "mock_sha256_thumbprint", # sha256_thumbprint | ||
| "mock_sha1_thumbprint", # sha1_thumbprint | ||
| ["mock_x5c_value"] # x5c | ||
| ) | ||
|
|
||
| def _verify_assertion_params(self, mock_jwt_creator_class, expected_algorithm, | ||
| expected_thumbprint_type, expected_thumbprint_value=None, | ||
| has_x5c=False): | ||
| """Helper to verify JwtAssertionCreator was called with correct params""" | ||
| mock_jwt_creator_class.assert_called_once() | ||
| call_args = mock_jwt_creator_class.call_args | ||
|
|
||
| # Verify algorithm | ||
| self.assertEqual(call_args[1]['algorithm'], expected_algorithm) | ||
|
|
||
| # Verify thumbprint type | ||
| if expected_thumbprint_type == 'sha256': | ||
| self.assertIn('sha256_thumbprint', call_args[1]) | ||
| self.assertNotIn('sha1_thumbprint', call_args[1]) | ||
| elif expected_thumbprint_type == 'sha1': | ||
| self.assertIn('sha1_thumbprint', call_args[1]) | ||
| self.assertNotIn('sha256_thumbprint', call_args[1]) | ||
| if expected_thumbprint_value: | ||
| self.assertEqual(call_args[1]['sha1_thumbprint'], expected_thumbprint_value) | ||
|
|
||
| # Verify x5c header if expected | ||
| if has_x5c: | ||
| self.assertIn('headers', call_args[1]) | ||
| self.assertIn('x5c', call_args[1]['headers']) | ||
|
|
||
| return call_args | ||
|
|
||
| @patch('cryptography.x509.load_pem_x509_certificate') | ||
| @patch('msal.application._extract_cert_and_thumbprints') | ||
| def test_pem_with_certificate_only_uses_sha256( | ||
| self, mock_extract, mock_load_cert, mock_jwt_creator_class, mock_authority_class): | ||
| """Test that providing only public_certificate (no thumbprint) uses SHA-256""" | ||
| authority = "https://login.microsoftonline.com/common" | ||
| self._setup_mocks(mock_authority_class, authority) | ||
| self._setup_certificate_mocks(mock_extract, mock_load_cert) | ||
|
|
||
| # Create app with certificate credential WITHOUT thumbprint | ||
| app = ConfidentialClientApplication( | ||
| client_id="my_client_id", | ||
| client_credential={ | ||
| "private_key": self.test_private_key, | ||
| "public_certificate": self.test_certificate, | ||
| # Note: NO thumbprint provided | ||
| }, | ||
| authority=authority | ||
| ) | ||
|
|
||
| # Verify SHA-256 with PS256 algorithm is used | ||
| self._verify_assertion_params( | ||
| mock_jwt_creator_class, | ||
| expected_algorithm='PS256', | ||
| expected_thumbprint_type='sha256', | ||
| has_x5c=True | ||
| ) | ||
|
|
||
| def test_pem_with_manual_thumbprint_uses_sha1( | ||
| self, mock_jwt_creator_class, mock_authority_class): | ||
| """Test that providing manual thumbprint (no certificate) uses SHA-1""" | ||
| authority = "https://login.microsoftonline.com/common" | ||
| self._setup_mocks(mock_authority_class, authority) | ||
|
|
||
| # Create app with manual thumbprint (legacy approach) | ||
| manual_thumbprint = "A1B2C3D4E5F6" | ||
| app = ConfidentialClientApplication( | ||
| client_id="my_client_id", | ||
| client_credential={ | ||
| "private_key": self.test_private_key, | ||
| "thumbprint": manual_thumbprint, | ||
| # Note: NO public_certificate provided | ||
| }, | ||
| authority=authority | ||
| ) | ||
|
|
||
| # Verify SHA-1 with RS256 algorithm is used | ||
| self._verify_assertion_params( | ||
| mock_jwt_creator_class, | ||
| expected_algorithm='RS256', | ||
| expected_thumbprint_type='sha1', | ||
| expected_thumbprint_value=manual_thumbprint | ||
| ) | ||
|
|
||
| def test_pem_with_both_uses_manual_thumbprint_as_sha1( | ||
| self, mock_jwt_creator_class, mock_authority_class): | ||
| """Test that providing both thumbprint and certificate prefers manual thumbprint (SHA-1)""" | ||
| authority = "https://login.microsoftonline.com/common" | ||
| self._setup_mocks(mock_authority_class, authority) | ||
|
|
||
| # Create app with BOTH thumbprint and certificate | ||
| manual_thumbprint = "A1B2C3D4E5F6" | ||
| app = ConfidentialClientApplication( | ||
| client_id="my_client_id", | ||
| client_credential={ | ||
| "private_key": self.test_private_key, | ||
| "thumbprint": manual_thumbprint, | ||
| "public_certificate": self.test_certificate, | ||
| }, | ||
| authority=authority | ||
| ) | ||
|
|
||
| # Verify manual thumbprint takes precedence (backward compatibility) | ||
| self._verify_assertion_params( | ||
| mock_jwt_creator_class, | ||
| expected_algorithm='RS256', | ||
| expected_thumbprint_type='sha1', | ||
| expected_thumbprint_value=manual_thumbprint, | ||
| has_x5c=True # x5c should still be present | ||
| ) | ||
|
|
||
| @patch('cryptography.x509.load_pem_x509_certificate') | ||
| @patch('msal.application._extract_cert_and_thumbprints') | ||
| def test_pem_with_adfs_uses_sha1( | ||
| self, mock_extract, mock_load_cert, mock_jwt_creator_class, mock_authority_class): | ||
| """Test that ADFS authority uses SHA-1 even with SHA-256 thumbprint""" | ||
| authority = "https://adfs.contoso.com/adfs" | ||
| self._setup_mocks(mock_authority_class, authority) | ||
| self._setup_certificate_mocks(mock_extract, mock_load_cert) | ||
|
|
||
| # Create app with certificate on ADFS | ||
| app = ConfidentialClientApplication( | ||
| client_id="my_client_id", | ||
| client_credential={ | ||
| "private_key": self.test_private_key, | ||
| "public_certificate": self.test_certificate, | ||
| }, | ||
| authority=authority | ||
| ) | ||
|
|
||
| # ADFS should force SHA-1 with RS256 even though SHA-256 would be calculated | ||
| self._verify_assertion_params( | ||
| mock_jwt_creator_class, | ||
| expected_algorithm='RS256', | ||
| expected_thumbprint_type='sha1' | ||
| ) | ||
|
|
||
| def test_pem_with_neither_raises_error(self, mock_jwt_creator_class, mock_authority_class): | ||
| """Test that providing neither thumbprint nor certificate raises ValueError""" | ||
| authority = "https://login.microsoftonline.com/common" | ||
| self._setup_mocks(mock_authority_class, authority) | ||
|
|
||
| # Should raise ValueError when neither thumbprint nor certificate provided | ||
| with self.assertRaises(ValueError) as context: | ||
| app = ConfidentialClientApplication( | ||
| client_id="my_client_id", | ||
| client_credential={ | ||
| "private_key": self.test_private_key, | ||
| # Note: NO thumbprint and NO public_certificate | ||
| }, | ||
| authority=authority | ||
| ) | ||
|
|
||
| self.assertIn("thumbprint", str(context.exception).lower()) | ||
| self.assertIn("public_certificate", str(context.exception).lower()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.