Skip to content

Commit

Permalink
Adding the username and password authentication method for authentica…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
wtfzdotnet committed Apr 5, 2014
2 parents 78864fd + 7f732a8 commit 1baed86
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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);

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

$validatedRequestToken = $client->getAuthenticationApi()->validateRequestTokenWithLogin(
$requestToken,
TMDB_USERNAME,
TMDB_PASSWORD
);

$sessionToken = $client->getAuthenticationApi()->getNewSession($validatedRequestToken);

var_dump($sessionToken);
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(TMDB_REQUEST_TOKEN);

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

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

var_dump($sessionToken);
61 changes: 61 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,8 +64,68 @@ 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
} catch (\Exception $e) {
if ($e->getCode() == 401) {
throw new UnauthorizedRequestTokenException("The request token has not been validated yet.");
}
//@codeCoverageIgnoreEnd
}
}

/**
* Helper method to validate the request_token and obtain a session_token
*
* @param $requestToken
* @param $username
* @param $password
* @return mixed
* @throws \InvalidArgumentException
*/
public function getSessionTokenWithLogin($requestToken, $username, $password)
{
if ($requestToken instanceof RequestToken) {
$requestToken = $requestToken->getToken();
}

$validatedRequestToken = $this->validateRequestTokenWithLogin($requestToken, $username, $password);

if (!$validatedRequestToken['success']) {
throw new \InvalidArgumentException('Unable to validate the request_token, please check your credentials.');
}

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

try {
return $this->get('authentication/token/validate_with_login', array(
'username' => $username,
'password' => $password,
'request_token' => $requestToken
));
//@codeCoverageIgnoreStart
} catch (\Exception $e) {
if ($e->getCode() == 401) {
Expand Down
43 changes: 43 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,48 @@ public function getSessionToken(RequestToken $requestToken)
return $this->getFactory()->createSessionToken($data);
}

/**
* This method is used to validate a request_token for user based authentication.
* A request_token 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 validateRequestTokenWithLogin(RequestToken $requestToken, $username, $password)
{
$data = $this->getApi()->validateRequestTokenWithLogin(
$requestToken,
$username,
$password
);

return $this->getFactory()->createRequestToken($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 getSessionTokenWithLogin(RequestToken $requestToken, $username, $password)
{
$data = $this->getApi()->getSessionTokenWithLogin(
$requestToken,
$username,
$password
);

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

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

0 comments on commit 1baed86

Please sign in to comment.