Skip to content

Commit 1a28a56

Browse files
committed
Use authentication exception in Cloud authenticator
1 parent 7639720 commit 1a28a56

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/Authenticator/Cloud.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
namespace ActiveCollab\SDK\Authenticator;
1010

1111
use ActiveCollab\SDK\Connector;
12+
use ActiveCollab\SDK\Exceptions\Authentication;
1213
use ActiveCollab\SDK\Exceptions\IssueTokenException;
1314
use ActiveCollab\SDK\Exceptions\ListAccounts;
15+
use ActiveCollab\SDK\ResponseInterface;
1416
use ActiveCollab\SDK\Token;
1517
use InvalidArgumentException;
1618

@@ -68,6 +70,8 @@ public function getAllAccounts()
6870
private $user;
6971

7072
/**
73+
* Return user information (first name, last name and avatar URL).
74+
*
7175
* @return array
7276
*/
7377
public function getUser()
@@ -107,28 +111,27 @@ public function issueToken(...$arguments)
107111

108112
$intent = $this->getIntent();
109113

110-
$account_id = $arguments[0];
114+
$account_id = (integer) $arguments[0];
111115

112116
if (empty($this->accounts[$account_id])) {
113117
throw new InvalidArgumentException("Account #{$account_id} not loaded");
114118
} else {
115-
$connector = new Connector();
116-
$response = $connector->post('https://app.activecollab.com/' . $account_id . '/api/v1/issue-token-intent', null, [
119+
$response = (new Connector())->post('https://app.activecollab.com/' . $account_id . '/api/v1/issue-token-intent', null, [
117120
'client_vendor' => $this->getYourOrgName(),
118121
'client_name' => $this->getYourAppName(),
119122
'intent' => $intent,
120123
]);
121124

122-
if ($response->isJson()) {
125+
if ($response instanceof ResponseInterface && $response->isJson()) {
123126
$result = $response->getJson();
124127

125128
if (empty($result['is_ok']) || empty($result['token'])) {
126-
throw new IssueTokenException(0);
129+
throw new Authentication('Authentication rejected');
127130
} else {
128131
return new Token($result['token'], $this->accounts[$account_id]['url']);
129132
}
130133
} else {
131-
throw new IssueTokenException(0);
134+
throw new Authentication('Invalid response');
132135
}
133136
}
134137
}
@@ -148,13 +151,12 @@ private function loadAccountsAndUser()
148151
throw new IssueTokenException(0);
149152
}
150153

151-
$connector = new Connector();
152-
$response = $connector->post('https://my.activecollab.com/api/v1/external/login', null, [
154+
$response = (new Connector())->post('https://my.activecollab.com/api/v1/external/login', null, [
153155
'email' => $this->getEmailAddress(),
154156
'password' => $this->getPassword(),
155157
]);
156158

157-
if ($response->isJson()) {
159+
if ($response instanceof ResponseInterface && $response->isJson()) {
158160
$result = $response->getJson();
159161

160162
if (empty($result['is_ok'])) {
@@ -180,7 +182,6 @@ private function loadAccountsAndUser()
180182
'name' => $account['display_name'],
181183
'url' => $account['url'],
182184
];
183-
} else {
184185
}
185186
}
186187
}
@@ -190,7 +191,7 @@ private function loadAccountsAndUser()
190191
$this->user = $result['user'];
191192
}
192193
} else {
193-
throw new IssueTokenException(0);
194+
throw new Authentication('Invalid response');
194195
}
195196
}
196197
}

0 commit comments

Comments
 (0)