Skip to content

Commit

Permalink
Adding username and password authentication, failes for some reason a…
Browse files Browse the repository at this point in the history
…nd added an ticket at TMDB API http://tmdb.lighthouseapp.com/projects/83077-api/tickets/358-improve-authentication-api#ticket-358-8.

I suspect the api_key has to be ignored from the request as the request matches the earlier discussion in this ticket,
but this would require extra work and actually doesn't make sense as all other authentication requests do require the api_key.
  • Loading branch information
wtfzdotnet committed Mar 3, 2014
1 parent b676689 commit cc0ec44
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/phpunit
/public_html
/cache.properties
/tmdb.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 <[email protected]>
* @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);

$client->setLogging(true, '/www/dev/php-tmdb-api/tmdb.log');

$requestToken = new \Tmdb\RequestToken('db13650c7740b364efdc5413bd8781cb700efc64');

$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client);

$sessionToken = $authenticationRepository->getUsernamePasswordToken(
$requestToken,
TMDB_USERNAME,
TMDB_PASSWORD
);

var_dump($sessionToken);
35 changes: 35 additions & 0 deletions lib/Tmdb/Api/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Tmdb\Api;

use Tmdb\Exception\UnauthorizedRequestTokenException;
use Tmdb\RequestToken;

/**
* Class Authentication
Expand Down Expand Up @@ -63,6 +64,10 @@ public function authenticateRequestToken($token)
*/
public function getNewSession($requestToken)
{
if ($requestToken instanceof RequestToken) {
$requestToken = $requestToken->getToken();
}

try {
return $this->get('authentication/session/new', array('request_token' => $requestToken));
//@codeCoverageIgnoreStart
Expand All @@ -74,6 +79,36 @@ public function getNewSession($requestToken)
}
}

/**
* 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.
*
* @param string $requestToken
* @param string $username
* @param string $password
* @throws UnauthorizedRequestTokenException
* @return mixed
*/
public function getUsernamePasswordToken($requestToken, $username, $password)
{
if ($requestToken instanceof RequestToken) {
$requestToken = $requestToken->getToken();
}

try {
return $this->get('authenticate/'. $requestToken .'/validate_with_login', array(
'username' => $username,
'password' => $password
));
//@codeCoverageIgnoreStart
} catch (\Exception $e) {
if ($e->getCode() == 401) {
throw new UnauthorizedRequestTokenException("The request token has not been validated yet.");
}
//@codeCoverageIgnoreEnd
}
}

/**
* This method is used to generate a guest session id.
*
Expand Down
22 changes: 22 additions & 0 deletions lib/Tmdb/Repository/AuthenticationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
namespace Tmdb\Repository;

use Tmdb\Exception\UnauthorizedRequestTokenException;
use Tmdb\Factory\AuthenticationFactory;
use Tmdb\RequestToken;

Expand Down Expand Up @@ -52,6 +53,27 @@ public function getSessionToken(RequestToken $requestToken)
return $this->getFactory()->createSessionToken($data);
}

/**
* 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.
*
* @param RequestToken $requestToken
* @param string $username
* @param string $password
* @throws UnauthorizedRequestTokenException
* @return mixed
*/
public function getUsernamePasswordToken(RequestToken $requestToken, $username, $password)
{
$data = $this->getApi()->getUsernamePasswordToken(
$requestToken->getToken(),
$username,
$password
);

return $this->getFactory()->createSessionToken($data);
}

/**
* This method is used to generate a guest session id.
*
Expand Down

0 comments on commit cc0ec44

Please sign in to comment.