Right now, if Require authentication is not checked on a resource operation, then all access are anonymous, even if you provide an access_token.
Conversely, if it's checked, every call without a valid token fail.
So basically, we can only have a service operation that is always anonymous or always (correctly) authenticated.
I suggest that we check for a token even if "Require authentication" is not checked. It allows services to be called without any access_token as anonymous & with a (valid) access_token as the corresponding user.
- If "Require authentication" is checked
- Token provided => check token
- Token not present => fail
- If "Require authentication" is not checked
- Token provided => check token
- Token not present => continue as anonymous
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | interdiff.txt | 1.67 KB | pjcdawkins |
| #23 | oauth2_server-service_auth-2340297-23.patch | 2.21 KB | pjcdawkins |
| #21 | oauth2_server-service_auth-2340297-21.patch | 2.05 KB | pjcdawkins |
| #17 | oauth2_server-service_auth-2340297-17.patch | 2.13 KB | pjcdawkins |
| #12 | oauth2_server-service_auth-2340297-12.patch | 1.98 KB | pjcdawkins |
Comments
Comment #1
garphyFirst attempt patch.
Comment #2
morsokPatch looks good to me.
However there is a case with this patch when you do a request to a ressource that does not require auth but you have a invalid token in your request, in that case the call will fail instead of logging you as anonymous. In itself it should not be a big deal because it does not break previous workflows, so if someone else is ok with the patch i think we should mark it RTBC.
Comment #3
dashohoxha commentedThank you so much for this patch. This feature is exactly what I need and the patch works as expected (I am already using it on my production server).
I wished a fix like this long time ago, but I didn't have time to do it myself. Then the patch is there, for such a long time (almost a year), and I missed it. Strange!
Comment #4
dashohoxha commentedComment #5
dbouman commentedI ran in to an issue with this patch when using the Performance configuration "Cache pages for anonymous users" turned on.
Steps to Reproduce:
1. Turn on "Cache pages for anonymous users"
2. Create a Services Resource that doesn't require authentication
3. Access the resource first as an anonymous user
4. Access the resource again as an OAuth2 authenticated user
What I notice is that I incorrectly get the anonymous users response back as an authenticated user.
This appears to be because the patch will bypass the oauth2_server_verify_access() function which specifies "drupal_page_is_cacheable(FALSE)"
Comment #6
dashohoxha commentedThis patch is a bit longer and more complicated, but I hope that it does it right. Can you check it?
Comment #7
dashohoxha commentedComment #8
dbouman commentedThe new patch #6 is working correctly for me. Thanks!
Comment #9
dashohoxha commentedIt is working for me too (but I didn't have that cache problem anyway).
Can you change the status to Reviewed and Tested By The Community?
Comment #10
pjcdawkins commentedI find this comment confusing, it might be better just to remove it.
I think you could have used oauth2_server_check_access() here, instead of changing oauth2_server_verify_access()
Comment #11
dashohoxha commented@pjcdawkins, please suggest your own patch, and let us test it.
Comment #12
pjcdawkins commentedThis patch shows what I mean: there is already a function for checking access without aborting the request.
Comment #13
garphyI find it odd to return NULL instead of FALSE in an access checking callback. Any rationale behind this change ?
Comment #14
pjcdawkins commentedYes there was a bit of rationale. FALSE to me would imply access denied. But this is just saying that this particular access method/plugin didn't authenticate the user, it's like a temporary 401. Other plugins can act later.
And Services doesn't care about the return type.
Comment #15
dashohoxha commentedreturn NULL;andreturn;are equivalent, and if Services doesn't care about the return type, then we should stick with the simplest form.|| $result->getStatusCode() !== 401oauth2_server_verify_access()you make it useless, a dangling function that is called nowhere else. So, this patch should also include the removal of this function.Comment #16
pjcdawkins commentedAuthenticationheader is provided and 'require_authentication' is off. 401 means you haven't provided theAuthenticationheader; it doesn't indicate there should be a fatal error. Services allows multiple authentication methods to act, e.g. cookies. This allows them to co-exist with OAuth2. So... I've added a comment in the patch.Comment #17
pjcdawkins commentedNow I've removed the redundant return statement (!)
Comment #18
dashohoxha commented$resultisinstanceof \OAuth2\Response, created and returned byoauth2_server_check_access(). It should be sent to the client if authentication is required, because it indicates that something went wrong with the authentication. No further checks are needed for this.The function
oauth2_server_verify_access()is not part ofoauth2_server.api.php, and I don't know how somebody could use an internal function like this in another module. Anyway, it doesn't hurt to live it around.Comment #19
pjcdawkins commented"It should be sent to the client if authentication is required" - yes, it is.
Comment #20
dashohoxha commentedI think the extra checking is useless and even may be wrong. Can you show a real case (example) when it can be useful?
Comment #21
pjcdawkins commentedOK I've removed the 401 thing - yes it's a bit exotic to have other authentication methods existing alongside OAuth2.
So this is pretty much your patch above, but without return FALSE, and leveraging the ..._check_access() function.
Comment #22
dashohoxha commentedFor me it is OK.
Comment #23
pjcdawkins commentedI think there should be an error if no
serveris set, regardless of whether authentication is required.Comment #25
pjcdawkins commentedComment #26
pjcdawkins commentedSorry, in the commit I should have credited dbouman for that very important contribution in #5 (I used Drupal's automatic attribution thing, which doesn't give enough credit to issue comments)