Calling user/token and system/connect through CORS returns anonymous user I have two Drupal sites set up. On site A, I'd like to authenticate myself as the currently logged-in user on site B (the remote site) so that I can view and edit nodes on the remote site using the Services module.

The two sites are on different domains, so I'm using the CORS module on the remote site. My CORS config is as follows:

*|http://MY-SITE-URL|*|Content-Type,X-CSRF-Token,Authentication, Origin, Cookie|true


Calling user/token against the remote site gets me a valid X-CSRF-Token. However, when I send the X-CSRF-Token with system/connect, the remote server returns the anonymous user. 

I'm trying to authenticate against the remote server with the following code:

$base_url_api = variable_get('tnb_sync_base_url'); //URL of the remote server


            $options = array(
              'headers' =&gt; array(
                'Content-Type' =&gt; 'application/json',
              ),
              'method' =&gt; 'POST',
            );
            //Step 1: grab the authentication token from the remote server
            $response = drupal_http_request($base_url_api . '/user/token', $options);
            $data = json_decode($response-&gt;data);

            // Check if login was successful
            if ($response-&gt;code == 200) {
              // Add the token to the request headers:
              $options['headers']['X-CSRF-Token'] = $data-&gt;token;

              //Step 2: pass along the token and check who we're logged in as
              $response = drupal_http_request($base_url_api . '/system/connect', $options);
              $data = json_decode($response-&gt;data);

              kpr($response);

              return $options;

            }

            else {
                return NULL;
            }


The response for the system/connect call looks like:

stdClass Object
(
    [request] =&gt; POST /sync/system/connect HTTP/1.0
Content-Type: application/json
X-CSRF-Token: 74xWvd5I5l1ad1WmrIjqZoACo9r7f30YtJx_uCFot_g
X-NewRelic-ID: VQQAU1JbChACUldTBgUFVw==
X-NewRelic-Transaction: PxQBVFRUXQJUUVQGVQkFBVIEFB8EBw8RVU4aVA0KA1cCAQxUVQMLUAcAAkNKQQgKVVAEUgEEFTs=
User-Agent: Drupal (+http://drupal.org/)
Host: dev.tnb.plus
Content-Length: 0


    [data] =&gt; {"sessid":"7u8CZ98Gvb1P3ZuKSnK85g2FMkyu8OlqbOp8mnsy7OM","session_name":"SSESS4be586c0dc3085a3ca4f49c7acb4e7d1","user":{"uid":0,"hostname":"2001:4801:7827:0104:be76:4eff:fe11:ccd8","roles":{"1":"anonymous user"},"cache":0,"timestamp":1492095786}}
    [protocol] =&gt; HTTP/1.1
    [status_message] =&gt; OK
    [headers] =&gt; Array
        (
            [date] =&gt; Thu, 13 Apr 2017 15:03:06 GMT
            [content-type] =&gt; application/json
            [connection] =&gt; close
            [set-cookie] =&gt; __cfduid=dc62becfa48db03b7f5e40b426d0f587c1492095786; expires=Fri, 13-Apr-18 15:03:06 GMT; path=/; domain=.tnb.plus; HttpOnly
            [access-control-allow-credentials] =&gt; true
            [access-control-allow-origin] =&gt; http://dan-northern-portal.pantheonsite.io
            [cache-control] =&gt; no-cache, must-revalidate
            [expires] =&gt; Sun, 19 Nov 1978 05:00:00 GMT
            [surrogate-key] =&gt; AuKS06yStxtoXZf2mZvq kFG3DJ+3z7Lu2WK6wNkS
            [surrogate-key-raw] =&gt; 
            [vary] =&gt; Accept-Encoding, Accept
            [x-content-type-options] =&gt; nosniff
            [x-drupal-cache] =&gt; MISS
            [x-pantheon-endpoint] =&gt; a9463df4-67b3-47b9-ad41-148336613222
            [x-pantheon-styx-hostname] =&gt; styx94699a92
            [x-styx-req-id] =&gt; styx-e58976d158f4a1f2e669efc5205db167
            [x-styx-version] =&gt; StyxGo
            [xkey] =&gt; AuKS06yStxtoXZf2mZvq kFG3DJ+3z7Lu2WK6wNkS
            [x-varnish] =&gt; 952125076
            [age] =&gt; 0
            [via] =&gt; 1.1 varnish-v4
            [server] =&gt; cloudflare-nginx
            [cf-ray] =&gt; 34ef4f698acf2513-ORD
        )

    [code] =&gt; 200
)


How can I properly authenticate myself on the remote site using Services and CORS?
You just haven't logged in to the remote site yet; you can do that with something like:

$options['headers']['X-CSRF-Token'] = $data-&gt;token;
$options['method'] = 'POST';

$post_data = ['username' =&gt; 'foo', 'password' =&gt; 'bar'];
$options['data'] = drupal_http_build_query($post_data);

$response = drupal_http_request($base_url_api . '/user/login', $options);
$data = json_decode($response-&gt;data);


Assuming a successful login, subsequent requests within the session timeout will be made in the context of that user.