Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Bearer Token Authorization Support #1483

Conversation

amrita-shrestha
Copy link

Description

With this PR, we are enhancing our HTTP client's capability by introducing support for Bearer token-based authorization.

Related to this PR

Test Added

  • unit tests to ensure the proper functioning of the newly introduced Bearer token authorization feature

@amrita-shrestha amrita-shrestha marked this pull request as ready for review August 17, 2023 10:51
@codecov
Copy link

codecov bot commented Aug 18, 2023

Codecov Report

Merging #1483 (fdc4bc0) into master (406f688) will increase coverage by 0.00%.
Report is 2 commits behind head on master.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##             master    #1483   +/-   ##
=========================================
  Coverage     97.34%   97.34%           
- Complexity     2830     2831    +1     
=========================================
  Files           175      175           
  Lines          9418     9420    +2     
=========================================
+ Hits           9168     9170    +2     
  Misses          250      250           
Files Changed Coverage Δ
lib/DAV/Client.php 92.74% <100.00%> (+0.11%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@phil-davis phil-davis self-requested a review August 18, 2023 02:07
lib/DAV/Client.php Outdated Show resolved Hide resolved
@phil-davis phil-davis self-requested a review August 18, 2023 02:43
phil-davis
phil-davis previously approved these changes Aug 18, 2023
@phil-davis phil-davis requested a review from staabm August 18, 2023 04:38
@phil-davis
Copy link
Contributor

@staabm we are starting to use the Client part of sabre/dav for some new stuff, and it would be nice to be able to pass in an access token directly in the constructor when making a new Client class. It is another optional setting, so no backward-compatibility issues. Access tokens are a common thing these days, so maybe useful for others too.

Please review. I don't want to "just merge" this because I have been involved with Amrita in discussing this.

Comment on lines +129 to +132
if (isset($settings['accessToken'])) {
$this->addCurlSetting(CURLOPT_HTTPHEADER, ["Authorization: Bearer {$settings['accessToken']}"]);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting headers here will get replaced if we do requests with some headers. So, this doesn't work as we expected.
Example:

$webdav = new Client([
    "baseUri" => "https://localhost:9200/remote.php/webdav/",
    "accessToken" => "eJashdksjaausb.asdohowkawrkas.adhdiuqwiduwqgwdi"
]);

// propfind() sets headers internally
$res = $webdav->propfind("", []);  // 401 Unauthorized ('Authorization' header gets removed)

Here, the headers are replaced: https://github.com/sabre-io/http/blob/d7c809503c1cb880622659095fc73d513e9ef23c/lib/Client.php#L409-L411

This will work but can't say if the Http\Client wants to do it.

if ([] !== $nHeaders) {
    $settings[CURLOPT_HTTPHEADER] = array_merge($settings[CURLOPT_HTTPHEADER], $nHeaders);
}

CC @phil-davis

Copy link

@saw-jan saw-jan Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we can do like this. So, I wonder if we want to have it in DAV\Client's settings but would be great if we have.

$webdav = new Client([
    "baseUri" => "https://localhost:9200/remote.php/webdav/"
]);

$webdav->addCurlSetting(CURLOPT_HTTPHEADER, ["Authorization: Bearer $accessToken"]);

Copy link

@saw-jan saw-jan Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to have it then I found some information to set it curl way instead of in header directly

// lib/DAV/Client.php
  ...
  $this->addCurlSetting(CURLOPT_USERPWD, $userName.':'.$password);
+} else if (isset($settings['accessToken'])) {
+    $this->addCurlSetting(CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
+    $this->addCurlSetting(CURLOPT_XOAUTH2_BEARER, isset($settings['accessToken']));
+}

See https://curl.se/libcurl/c/CURLOPT_HTTPAUTH.html, https://curl.se/libcurl/c/CURLOPT_XOAUTH2_BEARER.html
PHP: https://www.php.net/manual/en/function.curl-setopt.php, https://www.php.net/manual/en/curl.constants.php

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using CURLOPT_XOAUTH2_BEARER works without having to fix anything in Http\Client.

@@ -102,6 +102,7 @@ class Client extends HTTP\Client
* * baseUri
* * userName (optional)
* * password (optional)
* * accessToken (optional)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this name is too generic. Couldn't we name it bearerToken?

Alternatively couldn't we add a method to pass any raw http header thru to curl?

@staabm
Copy link
Member

staabm commented Sep 1, 2023

I am not sure why we need this PR.

have a look at the examples of sabre/http client in https://github.com/sabre-io/http/blob/d7c809503c1cb880622659095fc73d513e9ef23c/examples/basicauth.php

and we have a BearerAuth class already

@phil-davis phil-davis dismissed their stale review September 4, 2023 06:13

After discussion, we don't actually need this.

@phil-davis
Copy link
Contributor

After discussion, we have realized that we can achieve this in other ways, without needing to add any feature to dav-client.
See the various comments that @saw-jan has made.
So closing.

@phil-davis phil-davis closed this Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants