This adds the ability for Phabricator's OAuth server implementation to use HTTP basic auth for the client ID and secret and brings it in line with the OAuth 2.0 specification in this respect.
Fixes T11794
Differential D16763
oauthserver: get client ID/secret from HTTP auth wrl on Oct 28 2016, 5:29 AM. Authored by Tags None Referenced Files
Details
This adds the ability for Phabricator's OAuth server implementation to use HTTP basic auth for the client ID and secret and brings it in line with the OAuth 2.0 specification in this respect. Fixes T11794 Fixes my use case. Shouldn't impact other use-cases.
Diff Detail
Event TimelineComment Actions This allows weird mixes of client_secret + AUTH_USER -- let's do something like this instead (provided the code actually works)? $client_phid = $request->getStr('client_id'); $client_secret = $request->getStr('client_secret'); if (!strlen($client_phid) && !strlen($client_secret)) { $client_phid = idx($_SERVER, 'PHP_AUTH_USER'); $client_secret = idx($_SERVER, 'PHP_AUTH_PW'); } That is:
Comment Actions Sure, works for me, but I'm going to invert your logic (prefer AUTH header, fallback to request parameters). According to the spec (https://tools.ietf.org/html/rfc6749#section-3.2.1):
So, client_id will always be present as a request param. Comment Actions I explicitly want to prefer the parameters, not the auth header. A scenario I can imagine where both are present might be something like this:
This specific scenario may never occur, but I can't imagine any scenario where both are present and the "Authorization" header is explicit while the parameters are implicit. In contrast, I can easily imagine scenarios were the "Authorization" header is implicit (via a mechanism like ~/.netrc). Preferring parameters guarantees I'll never end up debugging this kind of config mess. The use of strlen() is also important. In PHP, the string "0" is falsey: $ php -r 'echo "0" ? "truthy\n" : "falsey\n";' falsey This means that ?client_id=0&client_secret=0 will be incorrectly ignored without the strlen() tests. Although these are probably not valid IDs/secrets, they can lead to a misleading error message (e.g., "client_secret not present", when the correct error is "client_secret present but not valid"). (Similar issues have occurred in the wild in the past with user @0, who is a real user, also has user account https://github.com/0, and has contributed to Phabricator.) If the Go API sends client_id as both a request parameter and an "Authorization" header, we should require that they agree. Something in this vein: $client_id_parameter = $request->getStr('client_id'); $client_id_header = idx($_SERVER, 'PHP_AUTH_USER'); if (strlen($client_id_parameter) && strlen($client_id_header)) { if ($client_id_parameter !== $client_id_header) { throw new Exception( pht( 'Request included a client_id parameter and an "Authorization" header with '. 'a username, but the values ("%s" and "%s") disagree. The values must match.', $client_id_parameter, $client_id_header)); } } $client_secret_parameter = $request->getStr('client_secret'); $client_secret_header = idx($_SERVER, 'PHP_AUTH_PW'); if (strlen($client_secret_parameter)) { // If the `client_secret` parameter is present, prefer parameters. $client_id = $client_id_parameter; $client_secret = $client_secret_parameter; } else { // Otherwise, read values from the "Authorization" header. $client_id = $client_id_header; $client_secret = $client_secret_header; } Comment Actions Done. Thanks for the strlen() tip as well, been a while since I've written any real amount of PHP. Comment Actions (I adjusted formatting slightly in the pull for project conventions, let me know if I broke anything by accident.) |