Problem/Motivation

According to https://www.drupal.org/node/2835573#comment-11829914 we need better error messages for 401, 403 errors, etc.

Proposed resolution

Return meaningful HTTP status codes per the RFC.

Remaining tasks

Change record.

User interface changes

None

API changes

Invalid Bearer tokens now throw an error, where previously it meant the anonymous user would be used, instead.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

e0ipso created an issue. See original summary.

mediabounds’s picture

Here's a basic pass at resolving this issue. The underlying OAuth library does have decent error messages (and varying status codes) for the various cases when an access or refresh token is not valid. Instead of eating the exception and only logging to watchdog, this rethrows as a symphony HttpException which allows the underlying error to be passed all the way back to the response.

e0ipso’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: descriptive-exceptions-2840205-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mediabounds’s picture

Reapplies patch against 8.x-3.x branch and fixes formatting issues.

mediabounds’s picture

Status: Needs work » Needs review
e0ipso’s picture

Status: Needs review » Closed (outdated)

I'm closing this as part of issue queue cleanup. Please reopen if necessary.

mediabounds’s picture

Status: Closed (outdated) » Needs review

IMHO, I think this is still applicable--I still use this patch anywhere I'm using Simple OAuth.

I want for the client application(s) of our service to be able to understand why a request was rejected. Without this patch, the server responds with a generic access denied status code. With this patch, the response body will indicate if the request was rejected due to an access token or refresh token being expired or revoked.

mediabounds’s picture

Adjusting the patch to avoid sending the exception headers--it seems that has an unintended side effect of changing the response format.

bradjones1’s picture

Title: Add more descriptive error messages » Error messages/codes should be more helpful & match spec.
Version: 8.x-2.x-dev » 8.x-3.x-dev

To put a bit of a finer point on it, certain client implementations (e.g., https://github.com/shoutem/fetch-token-intercept) use the HTTP status code to more directly map the error to the spec and its recovery/refresh strategy.

bradjones1’s picture

Version: 8.x-3.x-dev » 8.x-2.x-dev
Issue summary: View changes
bradjones1’s picture

Adding related issue that pertains to 401 responses prompting a retry on the client-side and a Drupal core bug poisoning the route matching cache.

bradjones1’s picture

Version: 8.x-2.x-dev » 8.x-4.x-dev
FileSize
1.6 KB
1.79 KB

Found another spot where this needed to be applied; also updated the previous patch to pass along the previous exception and break up the lines to under 80 chars.

bradjones1’s picture

Issue tags: +Needs tests coverage
bradjones1’s picture

Actually... the test failures point to an interesting angle, which should have been more clear to me earlier on: this is a change in behaviour in so far as the authentication provider throws an error when the provided Bearer token is invalid; the tests rely on the previous behaviour, where the provider returned NULL, meaning no user was found and thus the anonymous user would be loaded. (More on why this is bad, below.)

For the reasons already explored above, this is undesirable in the context of OAuth. I don't think the current behaviour is strictly inconsistent with RFC 6750, but it also makes a SHOULD recommendation regarding invalid access tokens:

invalid_token
         The access token provided is expired, revoked, malformed, or
         invalid for other reasons.  The resource SHOULD respond with
         the HTTP 401 (Unauthorized) status code.  The client MAY
         request a new access token and retry the protected resource
         request.

Sending a 401/other compliant response helps clients choose what to do next; a 200 response with the anonymous user's access reflected in the content is indecipherable from a failed authentication via Bearer token.

Updated patch enclosed with the tests updated to reflect this; as it's a change in behaviour, I think it also requires a change record. I'm happy to draft one up.

bradjones1’s picture

Telling Guzzle not to throw an exception on errors.

e0ipso’s picture

I believe the proposed changes make sense.

Thanks for contributing @mediabounds and @bradjones1! This module is better and more useful thanks to you. Open source maintainers need contributions to keep up. ❤️

If you think it's worth it, I would appreciate if contacted your employer about sponsoring some of my contribution time.

e0ipso’s picture

Status: Needs review » Fixed

  • e0ipso committed 509a7e6 on 8.x-4.x authored by bradjones1
    Issue #2840205 by bradjones1, mediabounds, e0ipso: Error messages/codes...
bradjones1’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.