diff --git a/examples/authentication/api/session_token_with_username_and_password.php b/examples/authentication/api/session_token_with_username_and_password.php new file mode 100644 index 00000000..dd073408 --- /dev/null +++ b/examples/authentication/api/session_token_with_username_and_password.php @@ -0,0 +1,29 @@ + + * @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); diff --git a/examples/authentication/model/session_token_with_username_and_password.php b/examples/authentication/model/session_token_with_username_and_password.php new file mode 100644 index 00000000..63d3f911 --- /dev/null +++ b/examples/authentication/model/session_token_with_username_and_password.php @@ -0,0 +1,31 @@ + + * @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); diff --git a/lib/Tmdb/Api/Authentication.php b/lib/Tmdb/Api/Authentication.php index 58ad244f..2078326d 100644 --- a/lib/Tmdb/Api/Authentication.php +++ b/lib/Tmdb/Api/Authentication.php @@ -13,6 +13,7 @@ namespace Tmdb\Api; use Tmdb\Exception\UnauthorizedRequestTokenException; +use Tmdb\RequestToken; /** * Class Authentication @@ -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) { diff --git a/lib/Tmdb/Repository/AuthenticationRepository.php b/lib/Tmdb/Repository/AuthenticationRepository.php index 809a16b9..43819581 100644 --- a/lib/Tmdb/Repository/AuthenticationRepository.php +++ b/lib/Tmdb/Repository/AuthenticationRepository.php @@ -12,6 +12,7 @@ */ namespace Tmdb\Repository; +use Tmdb\Exception\UnauthorizedRequestTokenException; use Tmdb\Factory\AuthenticationFactory; use Tmdb\RequestToken; @@ -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. *