@@ -23,6 +23,7 @@ class RequestManager {
23
23
private $ callMode = null ;
24
24
private $ clientSecrets = null ;
25
25
private $ cookieManager = null ;
26
+ private $ useHeader = false ;
26
27
27
28
public function __construct ($ uri , $ method , $ clientSecrets , $ callMode , $ cookieManager ) {
28
29
$ this ->uri = $ uri ;
@@ -32,6 +33,10 @@ public function __construct($uri, $method, $clientSecrets, $callMode, $cookieMan
32
33
$ this ->cookieManager = $ cookieManager ;
33
34
}
34
35
36
+ public function enableHeader () {
37
+ $ this ->useHeader = true ;
38
+ }
39
+
35
40
/**
36
41
* @param $inputs
37
42
* @param $parsedCookie
@@ -80,23 +85,33 @@ public function executeRequest($inputs, $parsedCookie) {
80
85
private function refreshToken ($ proxyResponse , $ inputs , $ parsedCookie ) {
81
86
$ cookie = null ;
82
87
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
+
83
92
//Get a new access token from refresh token
84
93
$ inputs = $ this ->removeTokenExtraParams ($ inputs );
85
94
$ inputs = $ this ->addRefreshExtraParams ($ inputs , $ parsedCookie );
86
95
$ proxyResponse = $ this ->replicateRequest ($ parsedCookie [ProxyAux::COOKIE_METHOD ], $ parsedCookie [ProxyAux::COOKIE_URI ], $ inputs );
87
96
88
97
$ content = $ proxyResponse ->getContent ();
89
98
if ($ proxyResponse ->getStatusCode () === 200 && array_key_exists (ProxyAux::ACCESS_TOKEN , $ content )) {
99
+ $ this ->callMode = ProxyAux::MODE_TOKEN ;
90
100
$ parsedCookie [ProxyAux::ACCESS_TOKEN ] = $ content [ProxyAux::ACCESS_TOKEN ];
91
101
$ parsedCookie [ProxyAux::REFRESH_TOKEN ] = $ content [ProxyAux::REFRESH_TOKEN ];
92
102
103
+ //TODO: add additional saved params
104
+
93
105
$ inputs = $ this ->removeRefreshTokenExtraParams ($ inputs );
94
106
$ inputs = $ this ->addTokenExtraParams ($ inputs , $ parsedCookie );
95
107
$ proxyResponse = $ this ->replicateRequest ($ this ->method , $ this ->uri , $ inputs );
96
108
97
109
//Set a new cookie with updated access token and refresh token
98
110
$ cookie = $ this ->cookieManager ->createCookie ($ parsedCookie );
99
111
}
112
+ else {
113
+ $ cookie = $ this ->cookieManager ->destroyCookie ();
114
+ }
100
115
}
101
116
102
117
return array (
@@ -143,12 +158,20 @@ private function getResponseContent($response) {
143
158
private function sendGuzzleRequest ($ method , $ uriVal , $ inputs ) {
144
159
$ options = array ();
145
160
$ 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
+
146
168
if ($ method === 'GET ' ) {
147
169
$ options = array_add ($ options , 'query ' , $ inputs );
148
170
}
149
171
else {
150
172
$ options = array_add ($ options , 'body ' , $ inputs );
151
173
}
174
+
152
175
$ request = $ client ->createRequest ($ method , $ uriVal , $ options );
153
176
154
177
try {
0 commit comments