Skip to content

Commit

Permalink
Adapt auths
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- committed Sep 13, 2014
1 parent 2c6d708 commit c3dcdf7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
6 changes: 1 addition & 5 deletions lib/Nekland/YoutubeApi/Http/Auth/PublicApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public function setOptions(array $options)
public function auth(RequestEvent $event)
{
$request = $event->getRequest();
$url = $request->getPath();

$url .= (false === strpos($url, '?') ? '?' : '&');
$url .= 'key=' . $this->options['key'];

$request->setPath($url);
$request->setParameter('key', $this->options['key']);
}
}
26 changes: 17 additions & 9 deletions lib/Nekland/YoutubeApi/Http/Auth/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

namespace Nekland\YoutubeApi\Http\Auth;


use Guzzle\Http\Client;
use Namshi\JOSE\JWS;
use Nekland\BaseApi\Http\AbstractHttpClient;
use Nekland\BaseApi\Http\Auth\AuthInterface;
use Nekland\BaseApi\Http\Auth\AuthStrategyInterface;
use Nekland\BaseApi\Http\Event\RequestEvent;
use Nekland\BaseApi\Http\Request;
use Nekland\YoutubeApi\Exception\AuthException;
use Nekland\YoutubeApi\Exception\MissingOptionException;

Expand All @@ -28,14 +29,21 @@ class Service implements AuthStrategyInterface
private $options;

/**
* @param RequestEvent $request
* @var AbstractHttpClient
*/
private $client;

/**
* @param RequestEvent $requestEvent
*/
public function auth(RequestEvent $requestEvent)
{
$request = $requestEvent->getRequest();

if (!$request->hasHeader('Authorization')) {
$this->client = $requestEvent->getClient();
$token = $this->getToken();
$request->addHeader('Authorization', 'Bearer '.$token);
$request->setHeader('Authorization', 'Bearer '.$token);
}
}

Expand All @@ -55,17 +63,17 @@ private function getToken()

$jws->sign($this->getPrivateKey());

$response = $this->client->post(
$response = $this->client->send(new Request(
'post',
'https://accounts.google.com/o/oauth2/token',
['Content-Type' => 'application/x-www-form-urlencoded'],
[
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jws->getTokenString()
],
['debug' => true]
);
['Content-Type' => 'application/x-www-form-urlencoded']
), false);

$finalArray = json_decode((string) $response->send()->getBody(), true);
$finalArray = json_decode($response, true);

return $finalArray['access_token'];
}
Expand Down Expand Up @@ -110,7 +118,7 @@ private function getOption($option, $default=null)

/**
* @param array $options
* @return AuthInterface
* @return self
*/
public function setOptions(array $options)
{
Expand Down

0 comments on commit c3dcdf7

Please sign in to comment.