Skip to content

Commit

Permalink
Complying with the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Apr 5, 2014
1 parent c78d1c0 commit 7f732a8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 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
Expand Up @@ -18,11 +18,11 @@

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

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

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

$sessionToken = $authenticationRepository->getUsernamePasswordToken(
$sessionToken = $authenticationRepository->getSessionTokenWithLogin(
$requestToken,
TMDB_USERNAME,
TMDB_PASSWORD
Expand Down
34 changes: 30 additions & 4 deletions lib/Tmdb/Api/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function getNewSession($requestToken)

try {
return $this->get('authentication/session/new', array('request_token' => $requestToken));

//@codeCoverageIgnoreStart
} catch (\Exception $e) {
if ($e->getCode() == 401) {
Expand All @@ -79,6 +80,30 @@ public function getNewSession($requestToken)
}
}

/**
* 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.
Expand All @@ -89,16 +114,17 @@ public function getNewSession($requestToken)
* @throws UnauthorizedRequestTokenException
* @return mixed
*/
public function getUsernamePasswordToken($requestToken, $username, $password)
public function validateRequestTokenWithLogin($requestToken, $username, $password)
{
if ($requestToken instanceof RequestToken) {
$requestToken = $requestToken->getToken();
}

try {
return $this->get('authenticate/'. $requestToken .'/validate_with_login', array(
'username' => $username,
'password' => $password
return $this->get('authentication/token/validate_with_login', array(
'username' => $username,
'password' => $password,
'request_token' => $requestToken
));
//@codeCoverageIgnoreStart
} catch (\Exception $e) {
Expand Down
27 changes: 24 additions & 3 deletions lib/Tmdb/Repository/AuthenticationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ 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.
Expand All @@ -63,10 +84,10 @@ public function getSessionToken(RequestToken $requestToken)
* @throws UnauthorizedRequestTokenException
* @return mixed
*/
public function getUsernamePasswordToken(RequestToken $requestToken, $username, $password)
public function getSessionTokenWithLogin(RequestToken $requestToken, $username, $password)
{
$data = $this->getApi()->getUsernamePasswordToken(
$requestToken->getToken(),
$data = $this->getApi()->getSessionTokenWithLogin(
$requestToken,
$username,
$password
);
Expand Down

0 comments on commit 7f732a8

Please sign in to comment.