Skip to content

Commit d28b67f

Browse files
committed
add header access token
1 parent f4c6b87 commit d28b67f

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/Andreoli/ApiProxy/Managers/RequestManager.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class RequestManager {
2323
private $callMode = null;
2424
private $clientSecrets = null;
2525
private $cookieManager = null;
26+
private $useHeader = false;
2627

2728
public function __construct($uri, $method, $clientSecrets, $callMode, $cookieManager) {
2829
$this->uri = $uri;
@@ -32,6 +33,10 @@ public function __construct($uri, $method, $clientSecrets, $callMode, $cookieMan
3233
$this->cookieManager = $cookieManager;
3334
}
3435

36+
public function enableHeader() {
37+
$this->useHeader = true;
38+
}
39+
3540
/**
3641
* @param $inputs
3742
* @param $parsedCookie
@@ -80,23 +85,33 @@ public function executeRequest($inputs, $parsedCookie) {
8085
private function refreshToken($proxyResponse, $inputs, $parsedCookie) {
8186
$cookie = null;
8287
if ($proxyResponse->getStatusCode() != 200 && array_key_exists(ProxyAux::REFRESH_TOKEN, $parsedCookie)) {
88+
$this->callMode = ProxyAux::MODE_REFRESH;
89+
90+
//TODO: remove and save additional params
91+
8392
//Get a new access token from refresh token
8493
$inputs = $this->removeTokenExtraParams($inputs);
8594
$inputs = $this->addRefreshExtraParams($inputs, $parsedCookie);
8695
$proxyResponse = $this->replicateRequest($parsedCookie[ProxyAux::COOKIE_METHOD], $parsedCookie[ProxyAux::COOKIE_URI], $inputs);
8796

8897
$content = $proxyResponse->getContent();
8998
if ($proxyResponse->getStatusCode() === 200 && array_key_exists(ProxyAux::ACCESS_TOKEN, $content)) {
99+
$this->callMode = ProxyAux::MODE_TOKEN;
90100
$parsedCookie[ProxyAux::ACCESS_TOKEN] = $content[ProxyAux::ACCESS_TOKEN];
91101
$parsedCookie[ProxyAux::REFRESH_TOKEN] = $content[ProxyAux::REFRESH_TOKEN];
92102

103+
//TODO: add additional saved params
104+
93105
$inputs = $this->removeRefreshTokenExtraParams($inputs);
94106
$inputs = $this->addTokenExtraParams($inputs, $parsedCookie);
95107
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs);
96108

97109
//Set a new cookie with updated access token and refresh token
98110
$cookie = $this->cookieManager->createCookie($parsedCookie);
99111
}
112+
else {
113+
$cookie = $this->cookieManager->destroyCookie();
114+
}
100115
}
101116

102117
return array(
@@ -143,12 +158,20 @@ private function getResponseContent($response) {
143158
private function sendGuzzleRequest($method, $uriVal, $inputs) {
144159
$options = array();
145160
$client = new Client();
161+
162+
if ($this->callMode === ProxyAux::MODE_TOKEN && $this->useHeader === true) {
163+
$accessToken = ProxyAux::getQueryValue($inputs, ProxyAux::ACCESS_TOKEN);
164+
$inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::ACCESS_TOKEN);
165+
$options = array_add($options, 'headers', [ ProxyAux::HEADER_AUTH => 'Bearer ' . $accessToken ]);
166+
}
167+
146168
if ($method === 'GET') {
147169
$options = array_add($options, 'query', $inputs);
148170
}
149171
else {
150172
$options = array_add($options, 'body', $inputs);
151173
}
174+
152175
$request = $client->createRequest($method, $uriVal, $options);
153176

154177
try {

src/Andreoli/ApiProxy/Proxy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class Proxy {
2525
private $redirectUri = null;
2626
private $clientSecrets = null;
2727
private $cookieManager = null;
28+
private $useHeader = false;
2829

2930
public function __construct($params) {
3031
$this->uriParam = $params['uri_param'];
3132
$this->skipParam = $params['skip_param'];
3233
$this->redirectUri = $params['redirect_login'];
3334
$this->clientSecrets = $params['client_secrets'];
35+
$this->useHeader = $params['use_header'];
3436
$this->cookieManager = new CookieManager($params['cookie_info']);
3537
}
3638

@@ -66,6 +68,9 @@ public function makeRequest($method, Array $inputs) {
6668

6769
//Create the new request
6870
$requestManager = new RequestManager($this->uri, $method, $this->clientSecrets, $this->callMode, $this->cookieManager);
71+
if ($this->useHeader) {
72+
$requestManager->enableHeader();
73+
}
6974
$proxyResponse = $requestManager->executeRequest($inputs, $parsedCookie);
7075

7176
return $this->setApiResponse($proxyResponse['response'], $proxyResponse['cookie']);

src/Andreoli/ApiProxy/ProxyAux.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ class ProxyAux {
2222
const COOKIE_URI = 'uri';
2323
const COOKIE_METHOD = 'method';
2424
const PASSWORD_GRANT = 'password';
25+
const HEADER_AUTH = "Authorization";
2526
const MODE_SKIP = '0';
2627
const MODE_LOGIN = '1';
2728
const MODE_TOKEN = '2';
29+
const MODE_REFRESH = '3';
2830

2931
/**
3032
* @param $array

src/config/proxy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@
5757
'time' => 5
5858
],
5959

60+
/*
61+
|--------------------------------------------------------------------------
62+
| Access token send into header
63+
|--------------------------------------------------------------------------
64+
|
65+
| If it is true the access_token was sent to oauth server into request's header.
66+
|
67+
*/
68+
'use_header' => false,
69+
6070
/*
6171
|--------------------------------------------------------------------------
6272
| List of client secret

0 commit comments

Comments
 (0)