@@ -338,15 +338,49 @@ public function getSession()
338
338
return $ this ->getVariable ('_session ' );
339
339
}
340
340
341
- public function refreshToken ( $ token )
341
+ public function makeAuthorizationCodeRequest ( $ auth_code )
342
342
{
343
- $ access_token = $ this ->getAccessTokenFromRefreshToken ($ token );
344
- $ session = $ this ->getSessionObject ($ access_token );
343
+ $ access_token = $ this ->getAccessTokenFromAuthorizationCode ($ auth_code );
344
+ return $ this ->validateAndSaveAccessToken ($ access_token );
345
+ }
346
+
347
+ public function makePasswordRequest ($ username , $ password )
348
+ {
349
+ $ access_token = $ this ->getAccessTokenFromPassword ($ username , $ password );
350
+ return $ this ->validateAndSaveAccessToken ($ access_token );
351
+ }
352
+
353
+ public function makeRefreshTokenRequest ($ refresh_token )
354
+ {
355
+ $ access_token = $ this ->getAccessTokenFromRefreshToken ($ refresh_token );
356
+ return $ this ->validateAndSaveAccessToken ($ access_token );
357
+ }
358
+
359
+ public function makeClientCredentialsRequest ()
360
+ {
361
+ $ access_token = $ this ->getAccessTokenFromClientCredentials ();
362
+ return $ this ->validateAndSaveAccessToken ($ access_token );
363
+ }
364
+
365
+ public function makeImplicitRequest ()
366
+ {
367
+ // not implemented
368
+ }
369
+
370
+ public function validateAndSaveAccessToken ($ token )
371
+ {
372
+ $ session = $ this ->getSessionObject ($ token );
345
373
$ session = $ this ->validateSessionObject ($ session );
346
374
$ this ->setSession ($ session , true );
347
375
return $ session ;
348
376
}
349
377
378
+ public function refreshToken ($ refresh_token )
379
+ {
380
+ $ access_token = $ this ->getAccessTokenFromRefreshToken ($ refresh_token );
381
+ return $ this ->validateAndSaveAccessToken ($ access_token );
382
+ }
383
+
350
384
private function hasPersistenceSupport ()
351
385
{
352
386
$ isPersistent = ($ this instanceof IOAuth2Persistent);
@@ -368,6 +402,39 @@ public function getAccessToken()
368
402
return isset ($ session ['access_token ' ]) ? $ session ['access_token ' ] : NULL ;
369
403
}
370
404
405
+ private function makeTokenRequest ($ params )
406
+ {
407
+ if ($ this ->getVariable ('access_token_uri ' ) && $ this ->getVariable ('client_id ' ) && $ this ->getVariable ('client_secret ' ))
408
+ {
409
+ return json_decode ($ this ->makeRequest (
410
+ $ this ->getVariable ('access_token_uri ' ),
411
+ 'POST ' ,
412
+ $ params
413
+ ), TRUE );
414
+ }
415
+ return NULL ;
416
+ }
417
+
418
+ /**
419
+ * Get access token from OAuth2.0 token endpoint with client credentials.
420
+ *
421
+ * This function will only be activated if both access token URI, client
422
+ * identifier and client secret are setup correctly.
423
+ *
424
+ * @return
425
+ * A valid OAuth2.0 JSON decoded access token in associative array, and
426
+ * NULL if not enough parameters or JSON decode failed.
427
+ */
428
+ private function getAccessTokenFromClientCredentials ()
429
+ {
430
+ $ params = array (
431
+ 'grant_type ' => 'client_credentials ' ,
432
+ 'client_id ' => $ this ->getVariable ('client_id ' ),
433
+ 'client_secret ' => $ this ->getVariable ('client_secret ' ),
434
+ );
435
+ return $ this ->makeTokenRequest ($ params );
436
+ }
437
+
371
438
/**
372
439
* Get access token from OAuth2.0 token endpoint with authorization code.
373
440
*
@@ -384,40 +451,40 @@ public function getAccessToken()
384
451
*/
385
452
private function getAccessTokenFromAuthorizationCode ($ code )
386
453
{
387
- if ($ this ->getVariable ('access_token_uri ' ) && $ this ->getVariable ('client_id ' ) && $ this ->getVariable ('client_secret ' ))
388
- {
389
- return json_decode ($ this ->makeRequest (
390
- $ this ->getVariable ('access_token_uri ' ),
391
- 'POST ' ,
392
- array (
393
- 'grant_type ' => 'authorization_code ' ,
394
- 'client_id ' => $ this ->getVariable ('client_id ' ),
395
- 'client_secret ' => $ this ->getVariable ('client_secret ' ),
396
- 'code ' => $ code ,
397
- 'redirect_uri ' => $ this ->getCurrentUri ()
398
- )
399
- ), TRUE );
400
- }
401
- return NULL ;
454
+ $ params = array (
455
+ 'grant_type ' => 'authorization_code ' ,
456
+ 'client_id ' => $ this ->getVariable ('client_id ' ),
457
+ 'client_secret ' => $ this ->getVariable ('client_secret ' ),
458
+ 'code ' => $ code ,
459
+ 'redirect_uri ' => $ this ->getCurrentUri ()
460
+ );
461
+ return $ this ->makeTokenRequest ($ params );
402
462
}
403
463
464
+ /**
465
+ * Get access token from OAuth2.0 token endpoint with refresh token.
466
+ *
467
+ * This function will only be activated if both access token URI, client
468
+ * identifier and client secret are setup correctly.
469
+ *
470
+ * @param $refresh_token
471
+ * Refresh token issued by authorization server's authorization
472
+ * endpoint.
473
+ *
474
+ * @return
475
+ * A valid OAuth2.0 JSON decoded access token in associative array, and
476
+ * NULL if not enough parameters or JSON decode failed.
477
+ */
404
478
private function getAccessTokenFromRefreshToken ($ refresh_token )
405
479
{
406
- if ($ this ->getVariable ('access_token_uri ' ) && $ this ->getVariable ('client_id ' ) && $ this ->getVariable ('client_secret ' ))
407
- {
408
- return json_decode ($ this ->makeRequest (
409
- $ this ->getVariable ('access_token_uri ' ),
410
- 'POST ' ,
411
- array (
412
- 'grant_type ' => 'refresh_token ' ,
413
- 'client_id ' => $ this ->getVariable ('client_id ' ),
414
- 'client_secret ' => $ this ->getVariable ('client_secret ' ),
415
- 'refresh_token ' => $ refresh_token ,
416
- 'redirect_uri ' => $ this ->getCurrentUri ()
417
- )
418
- ), TRUE );
419
- }
420
- return NULL ;
480
+ $ params = array (
481
+ 'grant_type ' => 'refresh_token ' ,
482
+ 'client_id ' => $ this ->getVariable ('client_id ' ),
483
+ 'client_secret ' => $ this ->getVariable ('client_secret ' ),
484
+ 'refresh_token ' => $ refresh_token ,
485
+ 'redirect_uri ' => $ this ->getCurrentUri ()
486
+ );
487
+ return $ this ->makeTokenRequest ($ params );
421
488
}
422
489
423
490
/**
@@ -438,21 +505,14 @@ private function getAccessTokenFromRefreshToken($refresh_token)
438
505
*/
439
506
private function getAccessTokenFromPassword ($ username , $ password )
440
507
{
441
- if ($ this ->getVariable ('access_token_uri ' ) && $ this ->getVariable ('client_id ' ) && $ this ->getVariable ('client_secret ' ))
442
- {
443
- return json_decode ($ this ->makeRequest (
444
- $ this ->getVariable ('access_token_uri ' ),
445
- 'POST ' ,
446
- array (
447
- 'grant_type ' => 'password ' ,
448
- 'client_id ' => $ this ->getVariable ('client_id ' ),
449
- 'client_secret ' => $ this ->getVariable ('client_secret ' ),
450
- 'username ' => $ username ,
451
- 'password ' => $ password
452
- )
453
- ), TRUE );
454
- }
455
- return NULL ;
508
+ $ params = array (
509
+ 'grant_type ' => 'password ' ,
510
+ 'client_id ' => $ this ->getVariable ('client_id ' ),
511
+ 'client_secret ' => $ this ->getVariable ('client_secret ' ),
512
+ 'username ' => $ username ,
513
+ 'password ' => $ password
514
+ );
515
+ return $ this ->makeTokenRequest ($ params );
456
516
}
457
517
458
518
/**
@@ -504,6 +564,7 @@ protected function makeOAuth2Request($path, $method = 'GET', $params = array())
504
564
*/
505
565
protected function makeRequest ($ path , $ method = 'GET ' , $ params = array (), $ ch = NULL )
506
566
{
567
+ Yii::log ("Making request to $ path through $ method with params " .var_export ($ params , true ));
507
568
if (!$ ch )
508
569
{
509
570
$ ch = curl_init ();
@@ -547,13 +608,6 @@ protected function makeRequest($path, $method = 'GET', $params = array(), $ch =
547
608
curl_setopt_array ($ ch , $ opts );
548
609
$ result = curl_exec ($ ch );
549
610
550
- if (curl_errno ($ ch ) == 60 )
551
- { // CURLE_SSL_CACERT
552
- error_log ('Invalid or no certificate authority found, using bundled information ' );
553
- curl_setopt ($ ch , CURLOPT_CAINFO , dirname (__FILE__ ) . '/fb_ca_chain_bundle.crt ' );
554
- $ result = curl_exec ($ ch );
555
- }
556
-
557
611
if ($ result === FALSE )
558
612
{
559
613
$ e = new OAuth2Exception (array ('code ' => curl_errno ($ ch ),
0 commit comments