-
Notifications
You must be signed in to change notification settings - Fork 7
Implement Hashing for API Key Storage to Enhance Security #142 #158
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
nishika26
merged 14 commits into
ProjectTech4DevAI:main
from
priyanshu6238:Cryptography
Apr 22, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
01f2985
Enhance API key management with encryption and decryption
priyanshu6238 3c5b9b6
Merge branch 'ProjectTech4DevAI:main' into Cryptography
priyanshu6238 8e23397
Refactor security imports and clean up code formatting
priyanshu6238 9f42ea5
Refactor API key generation to use helper function
priyanshu6238 f8a88f3
Add unit tests for API key encryption and decryption
priyanshu6238 a28c3ef
Refactor test cases for API key encryption and decryption
priyanshu6238 d279edb
Refactor API key generation and enhance validation tests
priyanshu6238 a4aa7de
Clean up whitespace in API key generation and validation tests
priyanshu6238 41bcec3
Refactor API key retrieval to use database session and simplify decry…
priyanshu6238 9a14268
Merge branch 'ProjectTech4DevAI:main' into Cryptography
priyanshu6238 f464314
Refactor API key creation by removing unnecessary comments and cleani…
priyanshu6238 2fd1b8c
Fix whitespace inconsistency in API key creation function for improve…
priyanshu6238 d5fbc9e
Refactor API key handling to encrypt and return raw keys instead of h…
priyanshu6238 30174f3
Enhance test for API key retrieval by adding assertions for organizat…
priyanshu6238 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
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
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
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,121 @@ | ||
| import pytest | ||
| from app.core.security import ( | ||
| get_password_hash, | ||
| verify_password, | ||
| encrypt_api_key, | ||
| decrypt_api_key, | ||
| get_encryption_key, | ||
| ) | ||
|
|
||
|
|
||
| def test_encrypt_decrypt_api_key(): | ||
| """Test that API key encryption and decryption works correctly.""" | ||
| # Test data | ||
| test_key = "ApiKey test123456789" | ||
|
|
||
| # Encrypt the key | ||
| encrypted_key = encrypt_api_key(test_key) | ||
|
|
||
| # Verify encryption worked | ||
| assert encrypted_key is not None | ||
| assert encrypted_key != test_key | ||
| assert isinstance(encrypted_key, str) | ||
|
|
||
| # Decrypt the key | ||
| decrypted_key = decrypt_api_key(encrypted_key) | ||
|
|
||
| # Verify decryption worked | ||
| assert decrypted_key is not None | ||
| assert decrypted_key == test_key | ||
|
|
||
|
|
||
| def test_api_key_format_validation(): | ||
| """Test that API key format is validated correctly.""" | ||
| # Test valid API key format | ||
| valid_key = "ApiKey test123456789" | ||
| encrypted_valid = encrypt_api_key(valid_key) | ||
| assert encrypted_valid is not None | ||
| assert decrypt_api_key(encrypted_valid) == valid_key | ||
|
|
||
| # Test invalid API key format (missing prefix) | ||
| invalid_key = "test123456789" | ||
| encrypted_invalid = encrypt_api_key(invalid_key) | ||
| assert encrypted_invalid is not None | ||
| assert decrypt_api_key(encrypted_invalid) == invalid_key | ||
|
|
||
|
|
||
| def test_encrypt_api_key_edge_cases(): | ||
| """Test edge cases for API key encryption.""" | ||
| # Test empty string | ||
| empty_key = "" | ||
| encrypted_empty = encrypt_api_key(empty_key) | ||
| assert encrypted_empty is not None | ||
| assert decrypt_api_key(encrypted_empty) == empty_key | ||
|
|
||
| # Test whitespace only | ||
| whitespace_key = " " | ||
| encrypted_whitespace = encrypt_api_key(whitespace_key) | ||
| assert encrypted_whitespace is not None | ||
| assert decrypt_api_key(encrypted_whitespace) == whitespace_key | ||
|
|
||
| # Test very long input | ||
| long_key = "ApiKey " + "a" * 1000 | ||
| encrypted_long = encrypt_api_key(long_key) | ||
| assert encrypted_long is not None | ||
| assert decrypt_api_key(encrypted_long) == long_key | ||
|
|
||
|
|
||
| def test_encrypt_api_key_type_validation(): | ||
| """Test type validation for API key encryption.""" | ||
| # Test non-string inputs | ||
| invalid_inputs = [123, [], {}, True] | ||
| for invalid_input in invalid_inputs: | ||
| with pytest.raises(ValueError, match="Failed to encrypt API key"): | ||
| encrypt_api_key(invalid_input) | ||
|
|
||
|
|
||
| def test_encrypt_api_key_security(): | ||
| """Test security properties of API key encryption.""" | ||
| # Test that same input produces different encrypted output | ||
| test_key = "ApiKey test123456789" | ||
| encrypted1 = encrypt_api_key(test_key) | ||
| encrypted2 = encrypt_api_key(test_key) | ||
| assert encrypted1 != encrypted2 # Different encrypted outputs for same input | ||
|
|
||
|
|
||
| def test_encrypt_api_key_error_handling(): | ||
| """Test error handling in encrypt_api_key.""" | ||
| # Test with invalid input | ||
| with pytest.raises(ValueError, match="Failed to encrypt API key"): | ||
| encrypt_api_key(None) | ||
|
|
||
|
|
||
| def test_decrypt_api_key_error_handling(): | ||
| """Test error handling in decrypt_api_key.""" | ||
| # Test with invalid input | ||
| with pytest.raises(ValueError, match="Failed to decrypt API key"): | ||
| decrypt_api_key(None) | ||
|
|
||
| # Test with various invalid encrypted data formats | ||
| invalid_encrypted_data = [ | ||
| "invalid_encrypted_data", # Not base64 | ||
| "not_a_base64_string", # Not base64 | ||
| "a" * 44, # Wrong length | ||
| "!" * 44, # Invalid base64 chars | ||
| "aGVsbG8=", # Valid base64 but not encrypted | ||
| ] | ||
| for invalid_data in invalid_encrypted_data: | ||
| with pytest.raises(ValueError, match="Failed to decrypt API key"): | ||
| decrypt_api_key(invalid_data) | ||
|
|
||
|
|
||
| def test_get_encryption_key(): | ||
| """Test that encryption key generation works correctly.""" | ||
| # Get the encryption key | ||
| key = get_encryption_key() | ||
|
|
||
| # Verify the key | ||
| assert key is not None | ||
| assert isinstance(key, bytes) | ||
| # The key is base64 encoded, so it should be 44 bytes | ||
| assert len(key) == 44 # Base64 encoded Fernet key length is 44 bytes |
Oops, something went wrong.
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.