fixed google auth
Showing
3 changed files
with
106 additions
and
154 deletions
| ... | @@ -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 | ||
| 222 | if (isset($_GET['code'])) { | ||
| 223 | $client->authenticate($_GET['code']); | ||
| 224 | $_SESSION['token'] = $client->getAccessToken(); | ||
| 225 | $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; | ||
| 226 | header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | |||
| 230 | if (isset($_SESSION['token'])) { | ||
| 231 | $client->setAccessToken($_SESSION['token']); | ||
| 232 | } | ||
| 233 | |||
| 217 | if (isset($_REQUEST['logout'])) { | 234 | if (isset($_REQUEST['logout'])) { |
| 218 | unset($_SESSION['access_token']); | 235 | unset($_SESSION['token']); |
| 219 | $client->revokeToken(); | 236 | $client->revokeToken(); |
| 220 | } | 237 | } |
| 221 | 238 | ?> | |
| 222 | /************************************************ | 239 | <!doctype html> |
| 223 | If we have a code back from the OAuth 2.0 flow, | 240 | <html> |
| 224 | we need to exchange that with the authenticate() | 241 | <head> |
| 225 | function. We store the resultant access token | 242 | <meta charset="utf-8"> |
| 226 | bundle in the session, and redirect to ourself. | 243 | </head> |
| 227 | ************************************************/ | 244 | <body> |
| 228 | if (isset($_GET['code'])) { | 245 | <header><h1>Get Token</h1></header> |
| 229 | $client->authenticate($_GET['code']); | 246 | <?php |
| 230 | $_SESSION['access_token'] = $client->getAccessToken(); | 247 | if ($client->getAccessToken()) { |
| 231 | $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; | 248 | $_SESSION['token'] = $client->getAccessToken(); |
| 232 | header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); | 249 | $token = json_decode($_SESSION['token']); |
| 233 | } | 250 | echo "Access Token = " . $token->access_token . '<br/>'; |
| 234 | 251 | echo "Refresh Token = " . $token->refresh_token . '<br/>'; | |
| 235 | /************************************************ | 252 | echo "Token type = " . $token->token_type . '<br/>'; |
| 236 | If we have an access token, we can make | 253 | echo "Expires in = " . $token->expires_in . '<br/>'; |
| 237 | requests, else we generate an authentication URL. | 254 | //echo "ID Token = " . $token->id_token . '<br/>'; |
| 238 | ************************************************/ | 255 | echo "Created = " . $token->created . '<br/>'; |
| 239 | if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { | 256 | |
| 240 | $client->setAccessToken($_SESSION['access_token']); | 257 | echo "<a class='logout' href='?logout'>Logout</a>"; |
| 241 | } else { | 258 | } else { |
| 242 | $authUrl = $client->createAuthUrl(); | 259 | $authUrl = $client->createAuthUrl(); |
| 243 | } | 260 | print "<a class='login' href='$authUrl'>Connect Me!</a><br/>"; |
| 244 | 261 | echo "<a class='logout' href='?logout'>Logout</a>"; | |
| 245 | /************************************************ | 262 | } |
| 246 | If we're signed in we can go ahead and retrieve | ||
| 247 | the ID token, which is part of the bundle of | ||
| 248 | data that is exchange in the authenticate step | ||
| 249 | - we only need to do a network call if we have | ||
| 250 | to retrieve the Google certificate to verify it, | ||
| 251 | and that can be cached. | ||
| 252 | ************************************************/ | ||
| 253 | if ($client->getAccessToken()) { | ||
| 254 | $_SESSION['access_token'] = $client->getAccessToken(); | ||
| 255 | $token_data = $client->verifyIdToken()->getAttributes(); | ||
| 256 | } | ||
| 257 | |||
| 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>"; | ||
| 273 | } | ||
| 274 | ?> | ||
| 275 | </div> | ||
| 276 | |||
| 277 | <div class="data"> | ||
| 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 | 263 | ||
| 288 | |||
| 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 | ||
| 287 | |||
| 288 | $client->authenticate($_GET['code']); | ||
| 289 | $_SESSION['token'] = $client->getAccessToken(); | ||
| 290 | |||
| 291 | |||
| 292 | if (isset($_SESSION['token'])) { | ||
| 293 | $client->setAccessToken($_SESSION['token']); | ||
| 294 | } | ||
| 295 | |||
| 308 | if (isset($_REQUEST['logout'])) { | 296 | if (isset($_REQUEST['logout'])) { |
| 309 | unset($_SESSION['access_token']); | 297 | unset($_SESSION['token']); |
| 310 | $client->revokeToken(); | 298 | $client->revokeToken(); |
| 311 | } | 299 | } |
| 312 | 300 | ?> | |
| 313 | /************************************************ | 301 | <!doctype html> |
| 314 | If we have a code back from the OAuth 2.0 flow, | 302 | <html> |
| 315 | we need to exchange that with the authenticate() | 303 | <head> |
| 316 | function. We store the resultant access token | 304 | <meta charset="utf-8"> |
| 317 | bundle in the session, and redirect to ourself. | 305 | </head> |
| 318 | ************************************************/ | 306 | <body> |
| 319 | if (isset($_GET['code'])) { | 307 | <header><h1>Get Token</h1></header> |
| 320 | $client->authenticate($_GET['code']); | 308 | <?php |
| 321 | $_SESSION['access_token'] = $client->getAccessToken(); | 309 | |
| 322 | $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; | 310 | $_SESSION['token'] = $client->getAccessToken(); |
| 323 | header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); | 311 | $token = json_decode($_SESSION['token']); |
| 324 | } | 312 | echo "Access Token = " . $token->access_token . '<br/>'; |
| 325 | 313 | echo "Refresh Token = " . $token->refresh_token . '<br/>'; | |
| 326 | /************************************************ | 314 | echo "Token type = " . $token->token_type . '<br/>'; |
| 327 | If we have an access token, we can make | 315 | echo "Expires in = " . $token->expires_in . '<br/>'; |
| 328 | requests, else we generate an authentication URL. | 316 | //echo "ID Token = " . $token->id_token . '<br/>'; |
| 329 | ************************************************/ | 317 | echo "Created = " . $token->created . '<br/>'; |
| 330 | if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { | 318 | echo "<a class='logout' href='?logout'>Logout</a>"; |
| 331 | $client->setAccessToken($_SESSION['access_token']); | 319 | $oath = new pdfModel(); |
| 332 | } else { | 320 | $oath = $oath->updateGoogleOath($token,$google_client_id); |
| 333 | $authUrl = $client->createAuthUrl(); | 321 | |
| 334 | } | ||
| 335 | |||
| 336 | /************************************************ | ||
| 337 | If we're signed in we can go ahead and retrieve | ||
| 338 | the ID token, which is part of the bundle of | ||
| 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 | |||
| 349 | echo pageHeader("User Query - Retrieving An Id Token"); | ||
| 350 | if ( | ||
| 351 | $client_id == '206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com' | ||
| 352 | || $client_secret == 'ZzEE02Dqz7AKJLSklmL30LNd' | ||
| 353 | || $redirect_uri == 'http://localhost:8888/pdf-customizer/public/auth/google/tokenCallback') { | ||
| 354 | echo missingClientSecretsWarning(); | ||
| 355 | } | ||
| 356 | ?> | ||
| 357 | <div class="box"> | ||
| 358 | <div class="request"> | ||
| 359 | <?php | ||
| 360 | if (isset($authUrl)) { | ||
| 361 | echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; | ||
| 362 | } else { | ||
| 363 | echo "<a class='logout' href='?logout'>Logout</a>"; | ||
| 364 | } | ||
| 365 | ?> | ||
| 366 | </div> | ||
| 367 | |||
| 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 | ||
| ... | @@ -181,17 +188,18 @@ class pdfModel extends Model | ... | @@ -181,17 +188,18 @@ class pdfModel extends Model |
| 181 | public function getGoogleUserOath($id){ | 188 | public function getGoogleUserOath($id){ |
| 182 | 189 | ||
| 183 | 190 | ||
| 191 | |||
| 192 | |||
| 193 | $rows = DB::table('googleOuth')->select('*')->where('googleOuth.user_id','=', $id); | ||
| 184 | 194 | ||
| 185 | $rows = DB::table('googleOuth')->where('googleOuth.user_id','=', $id); | 195 | |
| 186 | |||
| 187 | |||
| 188 | return $rows; | 196 | return $rows; |
| 189 | } | 197 | } |
| 190 | public function getUserId($id){ | 198 | public function getUserId($id){ |
| 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; | ... | ... |
-
Please register or sign in to post a comment