Skip to content

Commit 062099b

Browse files
committed
Totp support
1 parent 22bab17 commit 062099b

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace Atomx\Exceptions;
2+
3+
class TotpRequiredException extends ApiException {
4+
protected $response = null;
5+
6+
public function __construct($response)
7+
{
8+
$this->response = $response;
9+
}
10+
11+
public function getResponse()
12+
{
13+
return $this->response;
14+
}
15+
}

Atomx/MemoryAccountStore.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
<?php namespace Atomx;
22

3+
use Atomx\Exceptions\TotpRequiredException;
34
use Atomx\Resources\Login;
45

56
class MemoryAccountStore implements AccountStore {
67
protected $token = null;
7-
protected $username, $password, $apiBase;
8+
protected $username, $password, $totp, $apiBase;
89

910
/**
10-
* @param string $username
11-
* @param string $password
12-
* @param string $apiBase
11+
* @param string|null $username
12+
* @param string|null $password
13+
* @param string|null $totp
14+
* @param string|null $apiBase
1315
*/
14-
public function __construct($username = null, $password = null, $apiBase = null)
16+
public function __construct($username = null, $password = null, $totp = null, $apiBase = null)
1517
{
1618
$this->username = $username;
1719
$this->password = $password;
20+
$this->totp = $totp;
1821
$this->apiBase = $apiBase;
1922

2023
if ($this->apiBase == null)
2124
$this->apiBase = AtomxClient::API_BASE;
2225
}
2326

2427
/**
25-
* @return string|null
28+
* @return null|string
2629
*/
2730
public function getToken()
2831
{
@@ -43,8 +46,12 @@ protected function getTokenFromLogin()
4346
$response = $this->getLoginClient()->login([
4447
'email' => $this->getUsername(),
4548
'password' => $this->getPassword(),
49+
'totp' => $this->getTotp()
4650
]);
4751

52+
if ($response['totp_required'])
53+
throw new TotpRequiredException($response);
54+
4855
return $response['auth_token'];
4956
}
5057

@@ -72,6 +79,11 @@ public function getPassword()
7279
return $this->password;
7380
}
7481

82+
public function getTotp()
83+
{
84+
return $this->totp;
85+
}
86+
7587
/**
7688
* @return string
7789
*/

Atomx/Resources/Login.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function login(array $fields)
2020
$message = $e->getMessage();
2121

2222
if (isset($fields['password']))
23-
$message = str_replace($this->accountStore->getPassword(), '[redacted]', $message);
23+
$message = str_replace($fields['password'], '[redacted]', $message);
2424

2525
throw new ApiException('Unable to login to API! Message: ' . $message);
2626
}

Atomx/Resources/Totp.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace Atomx\Resources;
2+
3+
use Atomx\AtomxClient;
4+
5+
class Totp extends AtomxClient {
6+
protected $endpoint = 'totp';
7+
protected $requiresToken = true;
8+
9+
public function send($totp)
10+
{
11+
return $this->post(compact('totp'));
12+
}
13+
}

tests/ClientTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Atomx\Resources\Advertiser;
88
use Atomx\Resources\Domain;
99
use Atomx\Resources\Login;
10+
use Atomx\Resources\Totp;
1011
use GuzzleHttp\Message\Response;
1112
use GuzzleHttp\Stream\Stream;
1213
use GuzzleHttp\Subscriber\History;
@@ -110,7 +111,7 @@ public function testLogin()
110111
$this->assertArraySubset(['Authorization' => ['Bearer LOGIN_TOKEN']], $history->getLastRequest()->getHeaders());
111112
}
112113

113-
public function testTotp()
114+
public function testTotpAccountStore()
114115
{
115116
$login = new Login(new TestAccountStore);
116117

@@ -132,15 +133,17 @@ public function testTotp()
132133
$advertiser->getClient()->getEmitter()->attach($history);
133134

134135
$totpException = false;
136+
$token = '';
135137

136138
try {
137139
$advertiser->get(['limit' => 1, 'depth' => 0]);
138140
} catch (TotpRequiredException $e) {
139141
$totpException = true;
142+
$token = $e->getResponse()['auth_token'];
140143
}
141144

142145
$this->assertTrue($totpException);
143-
$this->assertEquals('TOTP_TOKEN', $store->getToken());
146+
$this->assertEquals('TOTP_TOKEN', $token);
144147
}
145148

146149
private function getValidEmptyResponse()

0 commit comments

Comments
 (0)