nextcloud / server

☁️ Nextcloud server, a safe home for all your data
https://nextcloud.com
GNU Affero General Public License v3.0
26.26k stars 3.96k forks source link

[Bug]: "CORS requires basic auth" error when authenticating with OIDC token #44365

Open akhil1508 opened 4 months ago

akhil1508 commented 4 months ago

⚠️ This issue respects the following points: ⚠️

Bug description

Steps to reproduce

  1. Install https://github.com/pulsejet/nextcloud-oidc-login/ on your nextcloud server
  2. Configure the app with your OIDC provider
  3. Apply the following patch using patch -u custom_apps/oidc_login/lib/AppInfo/Application.php -i oidc_api.patch

    
    --- Application.php 2024-03-20 23:26:05.315639574 +0530
    +++ Application-new.php 2024-03-20 23:27:12.099748051 +0530
    @@ -20,6 +20,7 @@
    use OCP\IURLGenerator;
    use OCP\IUserSession;
    use OCP\Util;
    +use OCA\OIDCLogin\WebDAV\BearerAuthBackend;
    
    class Application extends App implements IBootstrap
    {
    @@ -68,6 +69,15 @@
    
         /** @var IRequest */
         $request = $container->get(IRequest::class);
    +        $bearerAuthBackend = $container->query(BearerAuthBackend::class);
    +
    +        // If it is an OCS request, try to authenticate with bearer token
    +        if ($request->getHeader('OCS-APIREQUEST') === 'true' &&
    +            $request->getHeader('OIDC-LOGIN-WITH-TOKEN') === 'true' &&
    +            str_starts_with($request->getHeader('Authorization'), 'Bearer ')) {
    +            $this->loginWithBearerToken($request, $bearerAuthBackend);
    +        }
    +
    
         // Check if automatic redirection is enabled
         $useLoginRedirect = $this->config->getSystemValue('oidc_login_auto_redirect', false);
    @@ -157,4 +167,12 @@
             }
         }
     }
    +    private function loginWithBearerToken(IRequest $request, BearerAuthBackend $bearerAuthBackend) {
    +        $authHeader = $request->getHeader('Authorization');
    +       $bearerToken = substr($authHeader, 7);
    +        if (empty($bearerToken)) {
    +            return;
    +        }
    +       $bearerAuthBackend->validateBearerToken($bearerToken);
    +    }
    }

4. Get an access token from OIDC provider
5. Perform an API call to `/index.php/apps/notes/api/v0.2/notes` with Bearer authentication set using the above access token and header `OIDC-LOGIN-WITH-TOKEN` set to `true`  
7. Notice the "CORS requires basic auth" message

### Expected behavior

- Notes API should return results correctly

### Installation method

Community Docker image

### Nextcloud Server version

26

### Operating system

Debian/Ubuntu

### PHP engine version

PHP 8.1

### Web server

Nginx

### Database engine version

MariaDB

### Is this bug present after an update or on a fresh install?

Fresh Nextcloud Server install

### Are you using the Nextcloud Server Encryption module?

Encryption is Enabled

### What user-backends are you using?

- [ ] Default user-backend _(database)_
- [X] LDAP/ Active Directory
- [ ] SSO - SAML
- [ ] Other

### Configuration report

_No response_

### List of activated Apps

_No response_

### Nextcloud Signing status

_No response_

### Nextcloud Logs

_No response_

### Additional info

_No response_
akhil1508 commented 3 months ago

Is it okay to get CSRF token using /csrftoken route and then re-use the session associated with this along with BEARER token authentication? My use case is accessing notes API route from a mobile client using BEARER token authentication with SSO.

There is a @NoCSRFRequired annotation for these routes though

akhil1508 commented 3 months ago