-
Notifications
You must be signed in to change notification settings - Fork 31
Implement API key policy #389
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
Draft
thivindu
wants to merge
23
commits into
wso2:main
Choose a base branch
from
thivindu:api-key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3ed4bb9
Implement API key generation
thivindu 6106410
Implement gateway API key validation
thivindu 96d5962
Add in memory map for api key caching
thivindu 744ddfd
Fix CodeRabbit comments
thivindu 333a0b2
Minor fixes
thivindu d697b8a
Move validation of API keys generated from different services to policy
thivindu 1aa4e6a
Implement API key policy
thivindu 5849fba
Update api key policy implementation
thivindu c3ac7e1
Make type assertions safe
thivindu 2c837e8
Policy definition fixes
thivindu cacca8f
Update API key policy
thivindu 176fd56
Revamp API key generation
thivindu 9b044bc
Remove validate api key internal api
thivindu 141be28
Add sdk method for api key validation
thivindu 9ebe484
Add logging for api key policy
thivindu 6e99376
Update APIkey policy handleAuthFailure
thivindu fc7a5fc
Add API key generated user to the API key
thivindu 1e45e4d
Add default API key expiry
thivindu 59d1032
Implement API key revocation
thivindu 10e8801
Call the policy engine sdk to validate api key
thivindu cec6794
Update auto generated files
thivindu 3bbba19
Set default expiration of api key to infinity
thivindu 9368a3a
Minor openapi definition fixes
thivindu 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,115 @@ | ||
| Introduce **APIKeyAuthentication Policy (v1.0.0)** featuring: | ||
| - API key validation from headers or query parameters | ||
| - Configurable key extraction with optional prefix stripping | ||
| - Flexible authentication source configuration (header/query) | ||
| - Pre-shared key validation against configured key lists | ||
| - Request context enrichment with authentication metadata | ||
|
|
||
| ## Configuration Schema | ||
|
|
||
| ```yaml | ||
| name: APIKeyAuthentication | ||
| version: v1.0.0 | ||
| description: | | ||
| Implements API Key Authentication to protect APIs with pre-shared API keys. | ||
| Validates API keys from request headers or query parameters against a configured | ||
| list of valid keys and sets authentication metadata in the request context. | ||
|
|
||
| parameters: | ||
| type: object | ||
| additionalProperties: false | ||
| properties: | ||
| key: | ||
| type: string | ||
| required: true | ||
| description: | | ||
| The name of the header or query parameter that contains the API key. | ||
| For headers: case-insensitive matching is used (e.g., "X-API-Key", "Authorization") | ||
| For query parameters: exact name matching is used (e.g., "api_key", "token") | ||
| validation: | ||
| minLength: 1 | ||
| maxLength: 128 | ||
|
|
||
| in: | ||
| type: string | ||
| required: true | ||
| description: | | ||
| Specifies where to look for the API key. | ||
| Must be either "header" or "query". | ||
| enum: | ||
| - "header" | ||
| - "query" | ||
|
|
||
| value-prefix: | ||
| type: string | ||
| required: false | ||
| description: | | ||
| Optional prefix that should be stripped from the API key value before validation. | ||
| Case-insensitive matching and removal. Common use case is "Bearer " for Authorization headers. | ||
| If specified, the prefix will be removed from the extracted value. | ||
| validation: | ||
| minLength: 1 | ||
| maxLength: 64 | ||
|
|
||
| required: | ||
| - key | ||
| - in | ||
|
|
||
| initParameters: | ||
| type: object | ||
| properties: {} | ||
| ``` | ||
|
|
||
| ## Configuration Notes | ||
|
|
||
| The APIKeyAuthentication policy configuration only requires user provided parameters when attaching to an API or API resource. The policy does not define system-level initialization parameters (`initParameters` is empty), meaning all configuration is done by the API developer who attaches this policy to an API or API resource. | ||
|
|
||
| Valid API keys are generated by the gateway, management portal, or developer portal. When a request is received, the API key sent in the request will be validated against the keys generated by these services. The policy handles the extraction and validation logic, while the actual key generation and management is handled by the platform's key management system. | ||
|
|
||
| ## Example API/Per-Route Configurations | ||
|
|
||
| ### Header-based API Key Authentication | ||
|
|
||
| ```yaml | ||
| # API key in custom header | ||
| name: APIKeyAuthentication | ||
| version: v1.0.0 | ||
| params: | ||
| key: X-API-Key | ||
| in: header | ||
| ``` | ||
|
|
||
| ### Authorization Header with Bearer Prefix | ||
|
|
||
| ```yaml | ||
| # API key in Authorization header with Bearer prefix | ||
| name: APIKeyAuthentication | ||
| version: v1.0.0 | ||
| params: | ||
| key: Authorization | ||
| in: header | ||
| value-prefix: "Bearer " | ||
| ``` | ||
|
|
||
| ### Query Parameter Authentication | ||
|
|
||
| ```yaml | ||
| # API key as query parameter | ||
| name: APIKeyAuthentication | ||
| version: v1.0.0 | ||
| params: | ||
| key: api_key | ||
| in: query | ||
| ``` | ||
|
|
||
| ### Custom Header with Prefix | ||
|
|
||
| ```yaml | ||
| # API key in custom header with custom prefix | ||
| name: APIKeyAuthentication | ||
| version: v1.0.0 | ||
| params: | ||
| key: X-Custom-Auth | ||
| in: header | ||
| value-prefix: "ApiKey " | ||
| ``` | ||
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 |
|---|---|---|
|
|
@@ -39,7 +39,6 @@ help: ## Show this help message | |
| generate: ## Generate API server code from OpenAPI spec | ||
| @echo "Generating API server code from OpenAPI spec..." | ||
| @go run github.com/oapi-codegen/oapi-codegen/v2/cmd/[email protected] --config=oapi-codegen.yaml api/openapi.yaml | ||
|
|
||
| test: ## Run unit and integration tests | ||
| @echo "Running tests..." | ||
| @go test -v ./... -cover | ||
|
|
||
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.
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.