Skip to content

Commit

Permalink
Worked up to acquiring sessions, implementation of account related fu…
Browse files Browse the repository at this point in the history
…nctions to follow after we get the plugin done.
  • Loading branch information
wtfzdotnet committed Feb 24, 2014
1 parent a8c2cc4 commit 2cdb1a7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
21 changes: 21 additions & 0 deletions examples/authentication/api/authenticate-token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <michael@wtfz.net>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$requestToken = new \Tmdb\RequestToken(TMDB_REQUEST_TOKEN);

$client->getAuthenticationApi()->authenticateRequestToken($requestToken->getToken());
23 changes: 23 additions & 0 deletions examples/authentication/api/get-session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <michael@wtfz.net>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$requestToken = new \Tmdb\RequestToken(TMDB_REQUEST_TOKEN);

$sessionToken = $client->getAuthenticationApi()->getNewSession($requestToken->getToken());

var_dump($sessionToken);
File renamed without changes.
33 changes: 23 additions & 10 deletions lib/Tmdb/Api/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
*/
namespace Tmdb\Api;

use Tmdb\Client;
use Symfony\Component\Yaml\Exception\RuntimeException;
use Tmdb\Exception\NotImplementedException;
use Tmdb\Exception\UnauthorizedRequestTokenException;

class Authentication
extends AbstractApi
{
const REQUEST_TOKEN_URI = 'https://www.themoviedb.org/authenticate/%s';
const REQUEST_TOKEN_URI = 'https://www.themoviedb.org/authenticate';

/**
* This method is used to generate a valid request token for user based authentication.
Expand All @@ -27,33 +28,45 @@ class Authentication
* You can generate any number of request tokens but they will expire after 60 minutes.
* As soon as a valid session id has been created the token will be destroyed.
*
* @param array $parameters
* @param array $headers
* @return mixed
*/
public function getNewToken($parameters = array(), $headers = array())
public function getNewToken()
{
return $this->get('authentication/token/new', $parameters, $headers);
return $this->get('authentication/token/new');
}

/**
* Redirect the user to authenticate the request token
*
* @param $token
*/
public function authenticateRequestToken($token)
{
header(sprintf(
'Location: %s/%s',
sprintf(self::REQUEST_TOKEN_URI, $token)
self::REQUEST_TOKEN_URI,
$token
));
}

/**
* This method is used to generate a session id for user based authentication.
* A session id is required in order to use any of the write methods.
*
* @throws NotImplementedException
* @param string $requestToken
* @throws UnauthorizedRequestTokenException
* @return mixed
*/
public function getNewSession()
public function getNewSession($requestToken)
{
throw new NotImplementedException(__METHOD__ . ' has not been implemented yet.');
try {
return $this->get('authentication/session/new', array('request_token' => $requestToken));
}
catch(\Exception $e) {
if ($e->getCode() == 401) {
throw new UnauthorizedRequestTokenException("The request token has not been validated yet.");
}
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions lib/Tmdb/Exception/UnauthorizedRequestTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <michael@wtfz.net>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Exception;

class UnauthorizedRequestTokenException extends \RuntimeException {

}

0 comments on commit 2cdb1a7

Please sign in to comment.