esbenp / lumen-api-oauth

The code for a blog post I wrote about creating web apps using a Lumen API that is authenticated by OAuth2
http://esbenp.github.io/2015/05/26/lumen-web-api-oauth-2-authentication/
61 stars 22 forks source link

Cookies are not set to resposne? #26

Closed ershadow786 closed 7 years ago

ershadow786 commented 7 years ago

i have been trying this for a day but can't able to find out what i am doing wrong?

Here Class proxy function which gives me access token and refresh token after user enter it's username and password by ajax request i send request to controller where proxy controller function is called


    {

        try {
            $config = app()->make('config');

            $data = array_merge([
                'client_id'     => 'xxxxxx',
                'client_secret' => 'xxxxxxxx',
                'grant_type'    => $grantType
                ], $data);
            $client = new Client();
            $guzzleResponse = $client->post(sprintf('%s/api/auth/authorize-client', $config->get('app.url')), [
                'form_params' => $data
                ]);

        } catch(\GuzzleHttp\Exception\BadResponseException $e) {
            $guzzleResponse = $e->getResponse();

        }

        $response = json_decode($guzzleResponse->getBody());

        if (property_exists($response, "access_token")) {
            $cookie = new \Illuminate\Cookie\CookieJar();
            $crypt  = app()->make('encrypter');
            $encryptedToken = $crypt->encrypt($response->refresh_token);

            $cookie->queue('refreshToken',
                $crypt->encrypt($encryptedToken),
                604800, 
                null,
                null,
                true,
                true 
                );

            $response = [
            'accessToken'            => $response->access_token,
            'accessTokenExpiration'  => $response->expires_in
            ];
        }

        $response = response()->json($response);
        $response->setStatusCode($guzzleResponse->getStatusCode());

        $headers = $guzzleResponse->getHeaders();
        // attach headers 

        return $response;
    }

now as you can all see that is i am trying to add cookie to header named as refresh token but it;s not added only laravel seesion cookie is added 

  These are request headers 

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Authorization:Bearer bH41UEoAfXSutAcPm1nsRcQ4i3P0NzGxfu09To93
Cache-Control:no-cache
Connection:keep-alive
Content-Length:48
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
contentType:application/json;charset=utf-8;
Cookie:laravel_session=eyJpdiI6IjRtYUdkZk....................
Host:
Origin:https://
Pragma:no-cache
Referer:https://.....
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
X-CSRF-Token:null
X-Requested-With:XMLHttpRequest

And This is  Response Headers 

Access-Control-Allow-Headers:origin, x-requested-with, content-type, Authorization
Access-Control-Allow-Headers:origin, x-requested-with, content-type, Authorization
Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Allow-Origin:*
Cache-Control:must-revalidate, private
Connection:close
Content-Length:91
Content-Type:application/json
Date:........
ETag:"8........489786a33"
Server:Apache/2.4.6 .....
Strict-Transport-Security:max-age=63072000; includeSubdomains
Strict-Transport-Security:max-age=63072000; includeSubdomains
Vary:Authorization
X-Content-Type-Options:nosniff
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-Frame-Options:DENY
X-Powered-By
```:PHP/5.6.30
X-Powered-By:PHP/5.6.30

According to Docs Cookie::queue will automatically add cookies to resposne but i don't get it why it's not added.I am trying this on both postman and web.
Any Help will be appreciated