-
Notifications
You must be signed in to change notification settings - Fork 2
feat: support custom auth plugins #16
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f8aeaa4
Initial plan for issue
Copilot 1e7fe22
Initial analysis and plan for custom auth plugins support
Copilot 66a90a7
feat: implement custom auth plugins support
Copilot 2d9d52e
revert
GrantBirki f78a1ef
make the message more clear when no auth_plugin_dir is set for the gi…
GrantBirki 20a69e2
rename `handler_dir` to `handler_plugin_dir`
GrantBirki 47edef5
fix: update default paths for handler and auth plugin directories
GrantBirki 6826e4e
move into plugin dir
GrantBirki 652907a
cleanup
GrantBirki 97622f4
refactor: migrate handlers to plugins architecture
GrantBirki d800e16
update docs
GrantBirki e7b996c
feat: add custom auth plugin and related tests
GrantBirki 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Auth Plugins | ||
|
|
||
| This document provides an example of how to implement a custom authentication plugin for a hypothetical system. The plugin checks for a specific authorization header and validates it against a secret stored in an environment variable. | ||
|
|
||
| In your global configuration file (e.g. `hooks.yml`) you would likely set `auth_plugin_dir` to something like `./plugins/auth`. | ||
|
|
||
| Here is an example snippet of how you might configure the global settings in `hooks.yml`: | ||
|
|
||
| ```yaml | ||
| # hooks.yml | ||
| auth_plugin_dir: ./plugins/auth # Directory where custom auth plugins are stored | ||
| ``` | ||
|
|
||
| Then place your custom auth plugin in the `./plugins/auth` directory, for example `./plugins/auth/some_cool_auth_plugin.rb`. | ||
|
|
||
| ```ruby | ||
| # frozen_string_literal: true | ||
| # Example custom auth plugin implementation | ||
| module Hooks | ||
| module Plugins | ||
| module Auth | ||
| class SomeCoolAuthPlugin < Base | ||
| def self.valid?(payload:, headers:, config:) | ||
| # Get the secret from environment variable | ||
| secret = fetch_secret(config) # by default, this will fetch the value of the environment variable specified in the config (e.g. SUPER_COOL_SECRET as defined by `secret_env_key`) | ||
|
|
||
| # Get the authorization header (case-insensitive) | ||
| auth_header = nil | ||
| headers.each do |key, value| | ||
| if key.downcase == "authorization" | ||
| auth_header = value | ||
| break | ||
| end | ||
| end | ||
|
|
||
| # Check if the header matches our expected format | ||
| return false unless auth_header | ||
|
|
||
| # Extract the token from "Bearer <token>" format | ||
| return false unless auth_header.start_with?("Bearer ") | ||
|
|
||
| token = auth_header[7..-1] # Remove "Bearer " prefix | ||
|
|
||
| # Simple token comparison (in practice, this might be more complex) | ||
| token == secret | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| Then you could create a new endpoint configuration that references this plugin: | ||
|
|
||
| ```yaml | ||
| path: /example | ||
| handler: CoolNewHandler | ||
|
|
||
| auth: | ||
| type: some_cool_auth_plugin # using the newly created auth plugin as seen above | ||
| secret_env_key: SUPER_COOL_SECRET # the name of the environment variable containing the shared secret - used by `fetch_secret(config)` in the plugin | ||
| header: Authorization | ||
| ``` |
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
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 was deleted.
Oops, something went wrong.
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,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Hooks | ||
| module Plugins | ||
| module Handlers | ||
| # Base class for all webhook handlers | ||
| # | ||
| # All custom handlers must inherit from this class and implement the #call method | ||
| class Base | ||
| # Process a webhook request | ||
| # | ||
| # @param payload [Hash, String] Parsed request body (JSON Hash) or raw string | ||
| # @param headers [Hash<String, String>] HTTP headers | ||
| # @param config [Hash] Merged endpoint configuration including opts section | ||
| # @return [Hash, String, nil] Response body (will be auto-converted to JSON) | ||
| # @raise [NotImplementedError] if not implemented by subclass | ||
| def call(payload:, headers:, config:) | ||
| raise NotImplementedError, "Handler must implement #call method" | ||
| end | ||
|
|
||
| # Short logger accessor for all subclasses | ||
| # @return [Hooks::Log] Logger instance | ||
| # | ||
| # Provides a convenient way for handlers to log messages without needing | ||
| # to reference the full Hooks::Log namespace. | ||
| # | ||
| # @example Logging an error in an inherited class | ||
| # log.error("oh no an error occured") | ||
| def log | ||
| Hooks::Log.instance | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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
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.