f2a384d0 by Jeff Balicki

fixed google auth

1 parent 0fdce8be
...@@ -154,8 +154,8 @@ class ApiController extends Controller ...@@ -154,8 +154,8 @@ class ApiController extends Controller
154 $pdf = $pdf->pdf($pdfId); 154 $pdf = $pdf->pdf($pdfId);
155 $response = $pdfId; 155 $response = $pdfId;
156 $statusCode = 200; 156 $statusCode = 200;
157 //$UploaqdPdf = new GoogledriveuploadpdfController(); 157 $UploaqdPdf = new GoogledriveuploadpdfController();
158 //$UploaqdPdf->google_drive_upload($folder, 'new_'.$pdfLocation,$pdfId,$cust_id); 158 $UploaqdPdf->google_drive_upload($folder, 'new_'.$pdfLocation,$pdfId,$cust_id);
159 159
160 return Response::json( $response, $statusCode); 160 return Response::json( $response, $statusCode);
161 161
......
...@@ -200,181 +200,125 @@ public function isGoogleFolderCreated($cust_id) ...@@ -200,181 +200,125 @@ public function isGoogleFolderCreated($cust_id)
200 200
201 public function GoogleToken() 201 public function GoogleToken()
202 { 202 {
203 session_start(); 203
204 require_once realpath(dirname(__FILE__) . '/../autoload.php'); 204 session_start();
205 $oath = new pdfModel();
206 $oath = $oath->getGoogleUserOath(2)->get();
207 //var_dump($oath);
208
205 209
206 210
207 $client = new \Google_Client(); 211 $client = new \Google_Client();
208 $client->setScopes(array('https://www.googleapis.com/auth/drive.file')); 212 $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
209 $client->setClientId('206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com'); 213 $client->setClientId($oath[0]->google_client_id);
210 $client->setClientSecret('ZzEE02Dqz7AKJLSklmL30LNd'); 214 $client->setClientSecret($oath[0]->google_client_secret);
211 $client->setRedirectUri('https://pdf-customizer.synapsus.co/auth/google/tokenCallback'); 215 $client->setRedirectUri($oath[0]->redirect_uri);
216
212 217
213 $client->setAccessType('offline'); 218 $client->setAccessType('offline');
214 219
215 $client->setApprovalPrompt('force'); 220 $client->setApprovalPrompt('force');
216 221
217 if (isset($_REQUEST['logout'])) { 222 if (isset($_GET['code'])) {
218 unset($_SESSION['access_token']);
219 $client->revokeToken();
220 }
221
222 /************************************************
223 If we have a code back from the OAuth 2.0 flow,
224 we need to exchange that with the authenticate()
225 function. We store the resultant access token
226 bundle in the session, and redirect to ourself.
227 ************************************************/
228 if (isset($_GET['code'])) {
229 $client->authenticate($_GET['code']); 223 $client->authenticate($_GET['code']);
230 $_SESSION['access_token'] = $client->getAccessToken(); 224 $_SESSION['token'] = $client->getAccessToken();
231 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 225 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
232 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); 226 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
233 } 227 return;
228 }
234 229
235 /************************************************ 230 if (isset($_SESSION['token'])) {
236 If we have an access token, we can make 231 $client->setAccessToken($_SESSION['token']);
237 requests, else we generate an authentication URL. 232 }
238 ************************************************/
239 if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
240 $client->setAccessToken($_SESSION['access_token']);
241 } else {
242 $authUrl = $client->createAuthUrl();
243 }
244 233
245 /************************************************ 234 if (isset($_REQUEST['logout'])) {
246 If we're signed in we can go ahead and retrieve 235 unset($_SESSION['token']);
247 the ID token, which is part of the bundle of 236 $client->revokeToken();
248 data that is exchange in the authenticate step 237 }
249 - we only need to do a network call if we have 238 ?>
250 to retrieve the Google certificate to verify it, 239 <!doctype html>
251 and that can be cached. 240 <html>
252 ************************************************/ 241 <head>
253 if ($client->getAccessToken()) { 242 <meta charset="utf-8">
254 $_SESSION['access_token'] = $client->getAccessToken(); 243 </head>
255 $token_data = $client->verifyIdToken()->getAttributes(); 244 <body>
256 } 245 <header><h1>Get Token</h1></header>
246 <?php
247 if ($client->getAccessToken()) {
248 $_SESSION['token'] = $client->getAccessToken();
249 $token = json_decode($_SESSION['token']);
250 echo "Access Token = " . $token->access_token . '<br/>';
251 echo "Refresh Token = " . $token->refresh_token . '<br/>';
252 echo "Token type = " . $token->token_type . '<br/>';
253 echo "Expires in = " . $token->expires_in . '<br/>';
254 //echo "ID Token = " . $token->id_token . '<br/>';
255 echo "Created = " . $token->created . '<br/>';
257 256
258 echo pageHeader("User Query - Retrieving An Id Token");
259 if (
260 $client_id == '206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com'
261 || $client_secret == 'ZzEE02Dqz7AKJLSklmL30LNd'
262 || $redirect_uri == 'http://localhost:8888/pdf-customizer/public/auth/google/tokenCallback') {
263 echo missingClientSecretsWarning();
264 }
265 ?>
266 <div class="box">
267 <div class="request">
268 <?php
269 if (isset($authUrl)) {
270 echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
271 } else {
272 echo "<a class='logout' href='?logout'>Logout</a>"; 257 echo "<a class='logout' href='?logout'>Logout</a>";
273 } 258 } else {
274 ?> 259 $authUrl = $client->createAuthUrl();
275 </div> 260 print "<a class='login' href='$authUrl'>Connect Me!</a><br/>";
276 261 echo "<a class='logout' href='?logout'>Logout</a>";
277 <div class="data"> 262 }
278 <?php
279 if (isset($token_data)) {
280 var_dump($token_data);
281 }
282 ?>
283 </div>
284 </div>
285 <?php
286 echo pageFooter(__FILE__);
287
288 263
289 } 264 }
290 265
291 266
292 public function GoogleTokenCode() 267 public function GoogleTokenCode()
293 { 268 {
269
294 session_start(); 270 session_start();
295 require_once realpath(dirname(__FILE__) . '/../autoload.php'); 271 $oath = new pdfModel();
272 $oath = $oath->getGoogleUserOath(2)->get();
273 $google_client_id = $oath[0]->google_client_id;
274
296 275
297 276
298 $client = new \Google_Client(); 277 $client = new \Google_Client();
299 $client->setScopes(array('https://www.googleapis.com/auth/drive.file')); 278 $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
300 $client->setClientId('206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com'); 279 $client->setClientId($google_client_id);
301 $client->setClientSecret('ZzEE02Dqz7AKJLSklmL30LNd'); 280 $client->setClientSecret($oath[0]->google_client_secret);
302 $client->setRedirectUri('https://pdf-customizer.synapsus.co/auth/google/tokenCallback'); 281 $client->setRedirectUri($oath[0]->redirect_uri);
303 282
304 $client->setAccessType('offline'); 283 $client->setAccessType('offline');
305 284
306 $client->setApprovalPrompt('force'); 285 $client->setApprovalPrompt('force');
307 286
308 if (isset($_REQUEST['logout'])) {
309 unset($_SESSION['access_token']);
310 $client->revokeToken();
311 }
312 287
313 /************************************************
314 If we have a code back from the OAuth 2.0 flow,
315 we need to exchange that with the authenticate()
316 function. We store the resultant access token
317 bundle in the session, and redirect to ourself.
318 ************************************************/
319 if (isset($_GET['code'])) {
320 $client->authenticate($_GET['code']); 288 $client->authenticate($_GET['code']);
321 $_SESSION['access_token'] = $client->getAccessToken(); 289 $_SESSION['token'] = $client->getAccessToken();
322 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
323 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
324 }
325 290
326 /************************************************
327 If we have an access token, we can make
328 requests, else we generate an authentication URL.
329 ************************************************/
330 if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
331 $client->setAccessToken($_SESSION['access_token']);
332 } else {
333 $authUrl = $client->createAuthUrl();
334 }
335 291
336 /************************************************ 292 if (isset($_SESSION['token'])) {
337 If we're signed in we can go ahead and retrieve 293 $client->setAccessToken($_SESSION['token']);
338 the ID token, which is part of the bundle of 294 }
339 data that is exchange in the authenticate step
340 - we only need to do a network call if we have
341 to retrieve the Google certificate to verify it,
342 and that can be cached.
343 ************************************************/
344 if ($client->getAccessToken()) {
345 $_SESSION['access_token'] = $client->getAccessToken();
346 $token_data = $client->verifyIdToken()->getAttributes();
347 }
348 295
349 echo pageHeader("User Query - Retrieving An Id Token"); 296 if (isset($_REQUEST['logout'])) {
350 if ( 297 unset($_SESSION['token']);
351 $client_id == '206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com' 298 $client->revokeToken();
352 || $client_secret == 'ZzEE02Dqz7AKJLSklmL30LNd' 299 }
353 || $redirect_uri == 'http://localhost:8888/pdf-customizer/public/auth/google/tokenCallback') { 300 ?>
354 echo missingClientSecretsWarning(); 301 <!doctype html>
355 } 302 <html>
356 ?> 303 <head>
357 <div class="box"> 304 <meta charset="utf-8">
358 <div class="request"> 305 </head>
359 <?php 306 <body>
360 if (isset($authUrl)) { 307 <header><h1>Get Token</h1></header>
361 echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; 308 <?php
362 } else { 309
310 $_SESSION['token'] = $client->getAccessToken();
311 $token = json_decode($_SESSION['token']);
312 echo "Access Token = " . $token->access_token . '<br/>';
313 echo "Refresh Token = " . $token->refresh_token . '<br/>';
314 echo "Token type = " . $token->token_type . '<br/>';
315 echo "Expires in = " . $token->expires_in . '<br/>';
316 //echo "ID Token = " . $token->id_token . '<br/>';
317 echo "Created = " . $token->created . '<br/>';
363 echo "<a class='logout' href='?logout'>Logout</a>"; 318 echo "<a class='logout' href='?logout'>Logout</a>";
364 } 319 $oath = new pdfModel();
365 ?> 320 $oath = $oath->updateGoogleOath($token,$google_client_id);
366 </div>
367 321
368 <div class="data">
369 <?php
370 if (isset($token_data)) {
371 var_dump($token_data);
372 }
373 ?>
374 </div>
375 </div>
376 <?php
377 echo pageFooter(__FILE__);
378 322
379 } 323 }
380 324
......
...@@ -46,6 +46,13 @@ class pdfModel extends Model ...@@ -46,6 +46,13 @@ class pdfModel extends Model
46 return $folderId ; 46 return $folderId ;
47 } 47 }
48 48
49 public function updateGoogleOath($token, $google_client_id){
50
51 $folderId = DB::table('googleOuth')->where('google_client_id', $google_client_id)->insertGetId(
52 array('access_token' => $token->access_token, 'refresh_token' => $token->refresh_token)
53 );
54 return 'Saved';
55 }
49 56
50 public function getPdf($id){ 57 public function getPdf($id){
51 58
...@@ -182,7 +189,8 @@ class pdfModel extends Model ...@@ -182,7 +189,8 @@ class pdfModel extends Model
182 189
183 190
184 191
185 $rows = DB::table('googleOuth')->where('googleOuth.user_id','=', $id); 192
193 $rows = DB::table('googleOuth')->select('*')->where('googleOuth.user_id','=', $id);
186 194
187 195
188 return $rows; 196 return $rows;
...@@ -191,7 +199,7 @@ class pdfModel extends Model ...@@ -191,7 +199,7 @@ class pdfModel extends Model
191 199
192 200
193 201
194 $rows = DB::table('users')->select('id')->where('email','=', $id); 202 $rows = DB::table('users')->where('email','=', $id);
195 203
196 204
197 return $rows; 205 return $rows;
......