task: Refactor HMAC authentication plugin for better maintainability and cleaner tests#34
Merged
Conversation
Co-authored-by: GrantBirki <23362539+GrantBirki@users.noreply.github.com>
Co-authored-by: GrantBirki <23362539+GrantBirki@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] task: validate built-in hmac and fix tests
task: Refactor HMAC authentication plugin for better maintainability and cleaner tests
Jun 12, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the HMAC authentication plugin by extracting timestamp logic, simplifying HMAC validation, and cleaning up the test suite.
- Extracts
TimestampValidatorinto its own class to handle parsing and validation - Updates
HMACto delegate timestamp checks toTimestampValidator - Refactors specs with helper methods to reduce duplication and improve clarity
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| spec/unit/lib/hooks/plugins/auth/timestamp_validator_spec.rb | Adds comprehensive specs for TimestampValidator covering valid/invalid inputs |
| spec/unit/lib/hooks/plugins/auth/hmac_spec.rb | Refactors HMAC tests to use new helper methods and removes repetitive code |
| lib/hooks/plugins/auth/timestamp_validator.rb | Introduces TimestampValidator class encapsulating timestamp parsing/validation |
| lib/hooks/plugins/auth/hmac.rb | Delegates timestamp checks to TimestampValidator and cleans up inline logic |
| .bundle/config | Updates bundle path and deployment settings, but may include environment-specifics |
Comments suppressed due to low confidence (3)
spec/unit/lib/hooks/plugins/auth/timestamp_validator_spec.rb:3
- Use
require \"spec_helper\"instead of a deep relative path to leverage standard load paths and simplify requires.
require_relative "../../../../spec_helper"
spec/unit/lib/hooks/plugins/auth/hmac_spec.rb:42
- [nitpick] The helper name
create_version_prefixed_signatureis a bit verbose and unclear. Consider renaming tocreate_versioned_signatureto better express its purpose.
def create_version_prefixed_signature(signing_payload, version = "v0")
.bundle/config:3
- Committing an absolute, CI-specific
BUNDLE_PATHcan break local setups. Consider removing.bundle/configfrom version control or using a relative path.
BUNDLE_PATH: "/home/runner/work/hooks/hooks/vendor/bundle"
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
Refactored the HMAC authentication plugin to follow Ruby best practices and improve maintainability, addressing the unwieldy size and complex timestamp parsing logic identified in the issue.
Key Improvements
1. Extracted TimestampValidator class
TimestampValidatorclass (133 lines) to encapsulate all timestamp-related logicvalid?(timestamp, tolerance)andparse(timestamp)2. Simplified HMAC class
TimestampValidator3. Improved test maintainability
create_signature(payload, algorithm)create_algorithm_prefixed_signature(payload, algorithm)create_timestamped_signature(timestamp, version)TimestampValidator(127 lines)TimestampValidator4. Enhanced code quality
Validation
Total impact: Removed 223 lines of code while significantly improving structure, maintainability, and testability of the timestamp parsing logic.
Fixes #31.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.