Unauthorized and forbidden error
- Dominant language
- Python
- Stars
- 11.1k
- Forks
- 1k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 2
Description
The [documentation](https://chalice.readthedocs.io/en/latest/topics/authorizers.html#custom-authorizers) says that in order to return a `401 Unauthorized` we have to:
```python
# By specifying an empty list of routes,
# we're saying this user is not authorized
# for any URLs, which will result in an
# Unauthorized response.
return AuthResponse(routes=[], principal_id='user')
```
But this is not true because in this way the Authorizer will produce:
```http
HTTP/1.1 403 Forbidden
Connection: keep-alive
Content-Length: 60
Content-Type: application/json
Date: Tue, 23 Oct 2018 16:56:01 GMT
Via: *** *********************************************** ************
X-Amz-Cf-Id: ********************************************************
X-Cache: Error from cloudfront
x-amz-apigw-id: ****************
x-amzn-ErrorType: AccessDeniedException
x-amzn-RequestId: ************************************
{
"Message": "User is not authorized to access this resource"
}
```
```bash
API-Gateway-Execution-Logs_**********/dev ******************************** 2018-10-23T16:56:01.286Z (************************************) Successfully completed authorizer execution
API-Gateway-Execution-Logs_**********/dev ******************************** 2018-10-23T16:56:01.287Z (************************************) The client is not authorized to perform this operation.
```
If instead I try to use the `UnauthorizedError` it will produce:
```http
HTTP/1.1 500 Internal Server Error
Connection: keep-alive
Content-Length: 16
Content-Type: application/json
Date: Tue, 23 Oct 2018 16:53:08 GMT
Via: *** *********************************************** ************
X-Amz-Cf-Id: ********************************************************
X-Cache: Error from cloudfront
x-amz-apigw-id: ****************
x-amzn-ErrorType: AuthorizerConfigurationException
x-amzn-RequestId: ************************************
{
"message": null
}
```
```bash
API-Gateway-Execution-Logs_**********/dev ******************************** 2018-10-23T16:53:08.992Z (************************************) Execution failed due to configuration error: Authorizer function failed with response body: {"errorMessage": "UnauthorizedError: Authorization failed", "errorType": "UnauthorizedError", "stackTrace": [["/var/task/chalice/app.py", 789, "__call__", "result = self.func(auth_request)"], ["/var/task/app.py", 82, "with_profiling", "ret = fn(*args, **kwargs)"], ["/var/task/app.py", 440, "user_auth", "raise UnauthorizedError('Authorization failed')"]]}
```
The right way to produce a `401 Unauthorized` is to `raise Exception('Unauthorized')`:
```http
HTTP/1.1 401 Unauthorized
Connection: keep-alive
Content-Length: 26
Content-Type: application/json
Date: Tue, 23 Oct 2018 16:51:02 GMT
Via: *** *********************************************** ************
X-Amz-Cf-Id: ********************************************************
X-Cache: Error from cloudfront
x-amz-apigw-id: ****************
x-amzn-ErrorType: UnauthorizedException
x-amzn-RequestId: ************************************
{
"message": "Unauthorized"
}
```
```bash
API-Gateway-Execution-Logs_**********/dev ******************************** 2018-10-23T16:51:02.286Z (d31c7d2e-d6e3-11e8-9223-552ce5a2e72f) Unauthorized request: ************************************
```
So I think that the documentation and the examples are misleading.
Contributor guide
Research direction
Start with the Custom Authorizers documentation at the linked authorizers.html section and compare its 401 example with the observed API Gateway responses. Update the explanation and examples so the documented behavior matches the reported 401, 403, and 500 outcomes, then verify that the examples no longer claim an empty route list produces 401.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100