From cc8d270f307016499c81577d92f868c241937777 Mon Sep 17 00:00:00 2001 From: Nick Peirson Date: Sun, 11 Jan 2015 18:21:31 +0000 Subject: [PATCH] Add token refresh --- ...BasicAuthAbstract.php => AuthAbstract.php} | 23 +++------------- src/Request/GetToken.php | 25 +++++++++++++++--- src/Request/RefreshToken.php | 26 +++++++++++++++++++ src/Service.php | 14 +++++++++- src/Token.php | 4 +++ 5 files changed, 69 insertions(+), 23 deletions(-) rename src/Request/{BasicAuthAbstract.php => AuthAbstract.php} (58%) create mode 100644 src/Request/RefreshToken.php diff --git a/src/Request/BasicAuthAbstract.php b/src/Request/AuthAbstract.php similarity index 58% rename from src/Request/BasicAuthAbstract.php rename to src/Request/AuthAbstract.php index 0598c29..4ba0dd4 100644 --- a/src/Request/BasicAuthAbstract.php +++ b/src/Request/AuthAbstract.php @@ -4,13 +4,10 @@ use Nickpeirson\Evohome\RequestInterface; use Nickpeirson\Evohome\Service; -abstract class BasicAuthAbstract implements RequestInterface +abstract class AuthAbstract implements RequestInterface { const APP_PASS = 'test'; - protected $username; - protected $password; - public function getOptions() { return [ @@ -30,10 +27,7 @@ public function getHeaders() protected function getBody() { return [ - 'grant_type' => 'password', - 'scope' => 'EMEA-V1-Basic EMEA-V1-Anonymous EMEA-V1-Get-Current-User-Account', - 'Username' => $this->username, - 'Password' => $this->password + 'scope' => 'EMEA-V1-Basic EMEA-V1-Anonymous EMEA-V1-Get-Current-User-Account' ]; } @@ -42,17 +36,8 @@ public function getMethod() return 'post'; } - abstract public function getPath(); - - public function setUsername($username) - { - $this->username = $username; - return $this; - } - - public function setPassword($password) + public function getPath() { - $this->password = $password; - return $this; + return 'Auth/OAuth/Token'; } } \ No newline at end of file diff --git a/src/Request/GetToken.php b/src/Request/GetToken.php index c623d53..169f5d9 100644 --- a/src/Request/GetToken.php +++ b/src/Request/GetToken.php @@ -1,10 +1,29 @@ username; + $body['Password'] = $this->password; + return $body; + } + + public function setUsername($username) + { + $this->username = $username; + return $this; + } + + public function setPassword($password) { - return 'Auth/OAuth/Token'; + $this->password = $password; + return $this; } } \ No newline at end of file diff --git a/src/Request/RefreshToken.php b/src/Request/RefreshToken.php new file mode 100644 index 0000000..bbbcfbd --- /dev/null +++ b/src/Request/RefreshToken.php @@ -0,0 +1,26 @@ +token->getRefreshToken(); + return $body; + } + + public function setToken(Token $token) + { + $this->token = $token; + return $this; + } +} \ No newline at end of file diff --git a/src/Service.php b/src/Service.php index 9507d26..3f796c8 100644 --- a/src/Service.php +++ b/src/Service.php @@ -10,6 +10,7 @@ use Nickpeirson\Evohome\Request\Gateway; use Nickpeirson\Evohome\Request\LocationInstallationInfo; use Nickpeirson\Evohome\Request\ZoneSchedule; +use Nickpeirson\Evohome\Request\RefreshToken; class Service { @@ -103,12 +104,23 @@ public function sendRequest(TokenAbstract $request) throw new \Exception('Please log in before making requests'); } if ($this->token->isExpired()) { - throw new \Exception('Token has expired. Please log in again.'); + $this->refreshToken(); } $request->setToken($this->token); return $this->client->sendRequest($request); } + public function refreshToken() + { + $request = (new RefreshToken()) + ->setToken($this->token); + $response = $this->client->sendRequest($request); + if (isset($response->error)) { + throw new \Exception('Unable to refresh token'); + } + $this->token = $this->mapResponseToToken($response); + } + protected function mapResponseToToken($response) { return (new Token()) diff --git a/src/Token.php b/src/Token.php index 896175d..93eb738 100644 --- a/src/Token.php +++ b/src/Token.php @@ -48,5 +48,9 @@ public function getType() { return $this->type; } + public function getRefreshToken() { + return $this->refreshToken; + } + } \ No newline at end of file