Add configurable error scenarios for /authorize endpoint #10
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.
Enables testing of authentication failure flows by allowing the mock OAuth2 server to return error responses instead of auto-approving all authorization requests.
Changes
API
/configendpoint now acceptserror_scenarioobject withenabled,endpoint,error, anderror_descriptionfieldsauthorizeendpoint, returns error redirect instead of authorization codeError Types
Added support for all OAuth 2.0 RFC 6749 error codes:
access_denied,invalid_request,unauthorized_clientunsupported_response_type,invalid_scopeserver_error,temporarily_unavailableImplementation
AuthorizeHandlerto check for configured error scenarios before generating auth codesErrorScenariotype withEnabledfield to toggle scenarios on/offMemoryStore.GetErrorScenario()to only return enabled scenariosExample Usage
Set
enabled: falseto restore auto-approval behavior.Original prompt
This section details on the original issue you should resolve
<issue_title>[FEATURE] Implement error scenarios</issue_title>
<issue_description># Mock OAuth Error Scenarios - Feature Request
Problem
I have a project that uses the Goloang Mock Oauth2 server for testing. In this project I have a test "should redirect to login when accessing dashboard without authentication" which is currently skipped because the mock OAuth server auto-authenticates all requests, making it impossible to test unauthenticated access scenarios.
Current Behavior
When a test tries to access a page that requires authentication without cookies in a fresh browser context:
/auth/login/authorizeThis auto-approval behavior is great for testing happy paths, but prevents testing authentication failure scenarios.
Proposed Solution
Add an
error_scenarioconfiguration option to the mock OAuth server that allows tests to simulate authentication failures or rejections.API Design
Endpoint:
POST http://localhost:9090/configRequest Body:
{ "error_scenario": { "enabled": true, "error": "access_denied", "error_description": "User denied access" } }Supported Error Types
Following OAuth 2.0 spec (RFC 6749):
access_denied- User or authorization server denied the request{ "error": "access_denied", "error_description": "User denied access" }invalid_request- Request missing required parameter{ "error": "invalid_request", "error_description": "Missing client_id parameter" }unauthorized_client- Client not authorized to use this method{ "error": "unauthorized_client", "error_description": "Client not authorized" }unsupported_response_type- Server doesn't support obtaining auth code{ "error": "unsupported_response_type", "error_description": "Response type not supported" }invalid_scope- Requested scope invalid or unknown{ "error": "invalid_scope", "error_description": "Scope 'admin' is not available" }server_error- Server encountered unexpected error{ "error": "server_error", "error_description": "Internal server error" }temporarily_unavailable- Server temporarily unavailable{ "error": "temporarily_unavailable", "error_description": "Server is under maintenance" }Behavior
When
error_scenario.enabled = true:/authorizeendpoint: Returns error redirect to callback URL with error parameters/tokenendpoint: Returns HTTP 400 with error JSON{ "error": "invalid_grant", "error_description": "Authorization code is invalid" }When
error_scenario.enabled = false(default):Disabling Error Scenario
Reset to normal behavior:
{ "error_scenario": { "enabled": false } }Test Implementation
Updated Test
Test Isolation
Use
test.beforeEachandtest.afterEachto ensure error scenarios don't leak: