9
9
namespace ActiveCollab \SDK \Authenticator ;
10
10
11
11
use ActiveCollab \SDK \Connector ;
12
+ use ActiveCollab \SDK \Exceptions \Authentication ;
12
13
use ActiveCollab \SDK \Exceptions \IssueTokenException ;
13
14
use ActiveCollab \SDK \Exceptions \ListAccounts ;
15
+ use ActiveCollab \SDK \ResponseInterface ;
14
16
use ActiveCollab \SDK \Token ;
15
17
use InvalidArgumentException ;
16
18
@@ -68,6 +70,8 @@ public function getAllAccounts()
68
70
private $ user ;
69
71
70
72
/**
73
+ * Return user information (first name, last name and avatar URL).
74
+ *
71
75
* @return array
72
76
*/
73
77
public function getUser ()
@@ -107,28 +111,27 @@ public function issueToken(...$arguments)
107
111
108
112
$ intent = $ this ->getIntent ();
109
113
110
- $ account_id = $ arguments [0 ];
114
+ $ account_id = ( integer ) $ arguments [0 ];
111
115
112
116
if (empty ($ this ->accounts [$ account_id ])) {
113
117
throw new InvalidArgumentException ("Account # {$ account_id } not loaded " );
114
118
} 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 , [
117
120
'client_vendor ' => $ this ->getYourOrgName (),
118
121
'client_name ' => $ this ->getYourAppName (),
119
122
'intent ' => $ intent ,
120
123
]);
121
124
122
- if ($ response ->isJson ()) {
125
+ if ($ response instanceof ResponseInterface && $ response ->isJson ()) {
123
126
$ result = $ response ->getJson ();
124
127
125
128
if (empty ($ result ['is_ok ' ]) || empty ($ result ['token ' ])) {
126
- throw new IssueTokenException ( 0 );
129
+ throw new Authentication ( ' Authentication rejected ' );
127
130
} else {
128
131
return new Token ($ result ['token ' ], $ this ->accounts [$ account_id ]['url ' ]);
129
132
}
130
133
} else {
131
- throw new IssueTokenException ( 0 );
134
+ throw new Authentication ( ' Invalid response ' );
132
135
}
133
136
}
134
137
}
@@ -148,13 +151,12 @@ private function loadAccountsAndUser()
148
151
throw new IssueTokenException (0 );
149
152
}
150
153
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 , [
153
155
'email ' => $ this ->getEmailAddress (),
154
156
'password ' => $ this ->getPassword (),
155
157
]);
156
158
157
- if ($ response ->isJson ()) {
159
+ if ($ response instanceof ResponseInterface && $ response ->isJson ()) {
158
160
$ result = $ response ->getJson ();
159
161
160
162
if (empty ($ result ['is_ok ' ])) {
@@ -180,7 +182,6 @@ private function loadAccountsAndUser()
180
182
'name ' => $ account ['display_name ' ],
181
183
'url ' => $ account ['url ' ],
182
184
];
183
- } else {
184
185
}
185
186
}
186
187
}
@@ -190,7 +191,7 @@ private function loadAccountsAndUser()
190
191
$ this ->user = $ result ['user ' ];
191
192
}
192
193
} else {
193
- throw new IssueTokenException ( 0 );
194
+ throw new Authentication ( ' Invalid response ' );
194
195
}
195
196
}
196
197
}
0 commit comments