From 7f732a83146ddab01972ad243f002632e9dc1c05 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Sat, 5 Apr 2014 17:32:57 +0200 Subject: [PATCH] Complying with the API. --- ...ssion_token_with_username_and_password.php | 29 ++++++++++++++++ ...ssion_token_with_username_and_password.php | 4 +-- lib/Tmdb/Api/Authentication.php | 34 ++++++++++++++++--- .../Repository/AuthenticationRepository.php | 27 +++++++++++++-- 4 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 examples/authentication/api/session_token_with_username_and_password.php 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 index 2e7fae0a..63d3f911 100644 --- a/examples/authentication/model/session_token_with_username_and_password.php +++ b/examples/authentication/model/session_token_with_username_and_password.php @@ -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 diff --git a/lib/Tmdb/Api/Authentication.php b/lib/Tmdb/Api/Authentication.php index a93d130e..2078326d 100644 --- a/lib/Tmdb/Api/Authentication.php +++ b/lib/Tmdb/Api/Authentication.php @@ -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) { @@ -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. @@ -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) { diff --git a/lib/Tmdb/Repository/AuthenticationRepository.php b/lib/Tmdb/Repository/AuthenticationRepository.php index 4c35ad31..43819581 100644 --- a/lib/Tmdb/Repository/AuthenticationRepository.php +++ b/lib/Tmdb/Repository/AuthenticationRepository.php @@ -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. @@ -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 );