Implement API key generation, hashing, and validation utilities (issue #540)#544
Merged
Merged
Conversation
…#540) Add 4 new functions to campus/common/utils/secret.py for audit service API key security: - generate_audit_api_key(): Generate 31-char keys with 'audit_v1_' prefix and ~132-bit entropy - hash_api_key(): SHA-256 hashing for secure storage (64-char hex) - verify_api_key_hash(): Constant-time comparison using hmac.compare_digest() to prevent timing attacks - is_valid_audit_api_key_format(): Format validation (prefix, length, base64url charset) Key Format: audit_v1_<22-char-base64url> (31 total characters) - Prefix: audit_v1_ (9 chars) - version identification - Random: 22 base64url chars - URL-safe, case-sensitive - Entropy: ~132 bits - strong security margin - Performance: 723K verifications/second (0.0014ms per verification) Comprehensive unit tests added: - 20 test cases covering generation, hashing, verification, and validation - Security property tests (uniqueness, randomness, distribution) - Edge cases and error conditions - All tests passing Acceptance Criteria Met: 8/9 applicable criteria ✅ 31-char keys with audit_v1_ prefix ✅ base64url encoding for URL-safe characters ✅ ~132-bit entropy with 22 random characters ✅ SHA-256 hashing (salt optional per requirements) ✅ Constant-time comparison for timing attack prevention ✅ Comprehensive edge case handling ✅ Security enforced (no key logging, timing-safe comparison) ✅ 20 security-focused unit tests ✅ Performance benchmarks (723K ops/sec) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
10 tasks
Contributor
Author
|
Integration test failures related to ongoing work in audit middleware, for which this issue is working towards a resolution |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements API key generation, hashing, and validation utilities for the Campus Audit Service as specified in issue #540. This provides the cryptographic foundation for secure API key authentication.
Changes
New Functions in
campus/common/utils/secret.pygenerate_audit_api_key()- Generate 31-character API keys with formataudit_v1_<22-char-base64url>hash_api_key()- SHA-256 hashing for secure storage (64-char hex output)verify_api_key_hash()- Constant-time comparison usinghmac.compare_digest()to prevent timing attacksis_valid_audit_api_key_format()- Format validation (prefix, length, base64url character set)Key Format Specification
+,/, or=padding)audit_v1_for version identificationComprehensive Unit Tests
Added 20 test cases in
tests/unit/common/test_secret.py:Acceptance Criteria
✅ Key generation creates 31-char keys with
audit_v1_prefix✅ Keys use base64url encoding for URL-safe characters
✅ ~132-bit entropy achieved with 22 random characters
✅ SHA-256 hashing (salt optional per requirements)
✅ Constant-time comparison for timing attack prevention
✅ Comprehensive edge case handling
✅ Security enforced (no key logging, timing-safe comparison)
✅ 20 security-focused unit tests
✅ Performance benchmarks (723K ops/sec)
Status: 8/9 applicable criteria fully met
Security Features
Performance
Related Issues
Next Steps
Future issues will implement:
Testing
All 20 tests passing ✅
🤖 Generated with Claude Code