Skip to content

Commit

Permalink
Add token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpeirson committed Jan 11, 2015
1 parent d96394b commit cc8d270
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
23 changes: 4 additions & 19 deletions src/Request/BasicAuthAbstract.php → src/Request/AuthAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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'
];
}

Expand All @@ -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';
}
}
25 changes: 22 additions & 3 deletions src/Request/GetToken.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
<?php
namespace Nickpeirson\Evohome\Request;

class GetToken extends BasicAuthAbstract
class GetToken extends AuthAbstract
{
public function getPath()
protected $username;
protected $password;

protected function getBody()
{
$body = parent::getBody();
$body['grant_type'] = 'password';
$body['Username'] = $this->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;
}
}
26 changes: 26 additions & 0 deletions src/Request/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Nickpeirson\Evohome\Request;
use Nickpeirson\Evohome\Token;

class RefreshToken extends AuthAbstract
{
/**
*
* @var Token
*/
protected $token;

protected function getBody()
{
$body = parent::getBody();
$body['grant_type'] = 'refresh_token';
$body['refresh_token'] = $this->token->getRefreshToken();
return $body;
}

public function setToken(Token $token)
{
$this->token = $token;
return $this;
}
}
14 changes: 13 additions & 1 deletion src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ public function getType()
{
return $this->type;
}
public function getRefreshToken() {
return $this->refreshToken;
}


}

0 comments on commit cc8d270

Please sign in to comment.