Skip to content

Commit b29ecd1

Browse files
committed
Move login to the AccountStore
This makes it easy to add 2FA support. MemoryAccountStore could be used as a base class.
1 parent ba46774 commit b29ecd1

File tree

13 files changed

+154
-69
lines changed

13 files changed

+154
-69
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.idea/
22
vendor/
33
atomx.php
4-
Atomx/MemoryAccountStore.php
54
tests/AtomxAccountStore.php
65

Atomx/AccountStore.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33

44
interface AccountStore {
5-
public function getUsername();
6-
public function getPassword();
75
public function getToken();
86
public function storeToken($token);
97

Atomx/AtomxClient.php

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@
22

33
use Exception;
44
use GuzzleHttp\Message\Response;
5-
use GuzzleHttp\Stream\Stream;
65

76
class AtomxClient extends ApiClient {
7+
const API_BASE = 'https://api.atomx.com/v3/';
88
protected $apiBase = null;
99
protected $id = null;
10-
protected $requiresLogin = true;
10+
protected $requiresToken = true;
1111

1212
/**
1313
* @var AccountStore Store the token for the application
1414
*/
15-
private $accountStore;
15+
protected $accountStore = null;
1616

17-
function __construct(AccountStore $accountStore, $idOrFields = null)
17+
/**
18+
* AtomxClient constructor.
19+
* @param AccountStore|null $accountStore
20+
* @param int|array $idOrFields
21+
* @param string $apiBase
22+
*/
23+
function __construct($accountStore = null, $idOrFields = null)
1824
{
19-
$this->apiBase = $accountStore->getApiBase();
25+
if ($accountStore) {
26+
$this->accountStore = $accountStore;
27+
$this->apiBase = $accountStore->getApiBase();
28+
} else {
29+
$this->apiBase = AtomxClient::API_BASE;
30+
}
2031

2132
parent::__construct();
2233

23-
$this->accountStore = $accountStore;
24-
25-
2634
if (is_array($idOrFields))
2735
$this->fields = $idOrFields;
2836
else if (is_numeric($idOrFields))
@@ -54,7 +62,7 @@ protected function handleResponse(Response $response)
5462
return json_decode($response->getBody()->getContents(), true);
5563
}
5664

57-
if ($code == 401) {
65+
if ($code == 401 && $this->requiresToken) {
5866
// Unauthorized, invalidate token
5967
$this->accountStore->storeToken(null);
6068
}
@@ -67,56 +75,14 @@ protected function getDefaultOptions()
6775
{
6876
$options = parent::getDefaultOptions();
6977

70-
if ($this->requiresLogin)
78+
if ($this->requiresToken)
7179
$options['headers'] = ['Authorization' => 'Bearer ' . $this->getToken()];
7280

7381
return $options;
7482
}
7583

76-
public function login()
77-
{
78-
$this->requiresLogin = false;
79-
80-
try {
81-
$response = $this->postUrl('login', [
82-
'json' => [
83-
'email' => $this->accountStore->getUsername(),
84-
'password' => $this->accountStore->getPassword()
85-
]
86-
]);
87-
} catch (ApiException $e) {
88-
$message = str_replace($e->getMessage(), $this->accountStore->getPassword(), '[redacted]');
89-
90-
throw new ApiException('Unable to login to API! Message: ' . $message);
91-
}
92-
93-
$this->requiresLogin = true;
94-
95-
if ($response instanceof Stream) {
96-
$response = json_decode($response->getContents(), true);
97-
}
98-
99-
if ($response['success'] !== true)
100-
throw new ApiException('Unable to login to API!');
101-
102-
103-
$token = $response['auth_token'];
104-
105-
$this->accountStore->storeToken($token);
106-
107-
return $response['user'];
108-
}
109-
11084
private function getToken()
11185
{
112-
$token = $this->accountStore->getToken();
113-
114-
if ($token !== null) {
115-
return $token;
116-
}
117-
118-
$this->login();
119-
12086
return $this->accountStore->getToken();
12187
}
12288

Atomx/MemoryAccountStore.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php namespace Atomx;
2+
3+
use Atomx\Resources\Login;
4+
5+
class MemoryAccountStore implements AccountStore {
6+
protected $token = null;
7+
protected $username, $password, $apiBase;
8+
9+
/**
10+
* @param string $username
11+
* @param string $password
12+
* @param string $apiBase
13+
*/
14+
public function __construct($username = null, $password = null, $apiBase = null)
15+
{
16+
$this->username = $username;
17+
$this->password = $password;
18+
$this->apiBase = $apiBase;
19+
20+
if ($this->apiBase == null)
21+
$this->apiBase = AtomxClient::API_BASE;
22+
}
23+
24+
/**
25+
* @return string|null
26+
*/
27+
public function getToken()
28+
{
29+
if (is_null($this->token)) {
30+
$response = $this->getLoginClient()->login([
31+
'email' => $this->getUsername(),
32+
'password' => $this->getPassword(),
33+
]);
34+
35+
$this->token = $response['auth_token'];
36+
}
37+
38+
return $this->token;
39+
}
40+
41+
protected function getLoginClient()
42+
{
43+
return new Login($this);
44+
}
45+
46+
/**
47+
* @param string|null $token
48+
*/
49+
public function storeToken($token)
50+
{
51+
$this->token = $token;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getUsername()
58+
{
59+
return $this->username;
60+
}
61+
62+
/**
63+
* @return string
64+
*/
65+
public function getPassword()
66+
{
67+
return $this->password;
68+
}
69+
70+
/**
71+
* @return string
72+
*/
73+
public function getApiBase()
74+
{
75+
return $this->apiBase;
76+
}
77+
}

Atomx/Resources/Browsers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
class Browsers extends AtomxClient {
77
protected $endpoint = 'browsers';
8-
protected $requiresLogin = false;
8+
protected $requiresToken = false;
99
}

Atomx/Resources/CategoryList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
class CategoryList extends AtomxClient {
66
protected $endpoint = 'categories';
7-
protected $requiresLogin = false;
7+
protected $requiresToken = false;
88
}

Atomx/Resources/ConnectionTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
class ConnectionTypes extends AtomxClient {
77
protected $endpoint = 'connection-types';
8-
protected $requiresLogin = false;
8+
protected $requiresToken = false;
99
}

Atomx/Resources/DeviceTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
class DeviceTypes extends AtomxClient {
77
protected $endpoint = 'device-types';
8-
protected $requiresLogin = false;
8+
protected $requiresToken = false;
99
}

Atomx/Resources/Domain.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php namespace Atomx\Resources;
22

33
use Atomx\AtomxClient;
4-
use InvalidArgumentException;
54

65
class Domain extends AtomxClient {
76
protected $endpoint = 'domain';
8-
protected $requiresLogin = false;
7+
protected $requiresToken = false;
98

109
public function setLanguage($lan = null)
1110
{

Atomx/Resources/Login.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php namespace Atomx\Resources;
2+
3+
use Atomx\ApiException;
4+
use Atomx\AtomxClient;
5+
6+
class Login extends AtomxClient {
7+
protected $endpoint = 'login';
8+
protected $requiresToken = false;
9+
10+
/**
11+
* @param array $fields contains the username/password for the user
12+
* @return string
13+
* @throws ApiException
14+
*/
15+
public function login(array $fields)
16+
{
17+
try{
18+
$response = $this->post($fields);
19+
} catch (ApiException $e) {
20+
$message = $e->getMessage();
21+
22+
if (isset($fields['password']))
23+
$message = str_replace($this->accountStore->getPassword(), '[redacted]', $message);
24+
25+
throw new ApiException('Unable to login to API! Message: ' . $message);
26+
}
27+
28+
if ($response['success'] !== true)
29+
throw new ApiException('Unable to login to API. Message: ' . $response['message']);
30+
31+
return $response;
32+
}
33+
}

0 commit comments

Comments
 (0)