Skip to content

Commit f130985

Browse files
committed
handle access token revoked outside the application, remove saved tokens from Session on UnAuthorized Access error message.
1 parent b19b618 commit f130985

File tree

6 files changed

+127
-16
lines changed

6 files changed

+127
-16
lines changed

sdk/server/php/public_html/att/Devices.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,33 @@
3434
echo $response;
3535
}
3636
catch(ServiceException $se) {
37-
return_json_error($se->getErrorCode(), $se->getErrorResponse());
37+
switch ($se->getErrorCode()) {
38+
case 400: // invalid_grant. Invalid Refresh token.
39+
case 401: // UnAuthorized Access. Invalid access token.
40+
unset($_SESSION['client_token']);
41+
if (DEBUG) {
42+
Debug::init();
43+
Debug::write("Removed cached client token. Errocode=". $se->getErrorCode() ."\n");
44+
Debug::end();
45+
}
46+
break;
47+
}
48+
return_json_error($se->getErrorCode(), $se->getErrorResponse());
3849
}
3950
catch(Exception $e) {
40-
return_json_error(400, $e->getMessage());
51+
$error = $e->getMessage();
52+
// some operations in the codekit do not throw ServiceException
53+
if (stripos($error, 'UnAuthorized Request') !== false) {
54+
unset($_SESSION['client_token']);
55+
if (DEBUG) {
56+
Debug::init();
57+
Debug::write("token removed.\n");
58+
Debug::end();
59+
}
60+
return_json_error(401, "UnAuthorized Request. Try again to obtain a new access token.");
61+
} else {
62+
return_json_error(400, $error);
63+
}
4164
}
4265

4366
?>

sdk/server/php/public_html/att/mms.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,33 @@
128128
echo $response;
129129
}
130130
catch(ServiceException $se) {
131-
return_json_error($se->getErrorCode(), $se->getErrorResponse());
131+
switch ($se->getErrorCode()) {
132+
case 400: // invalid_grant. Invalid Refresh token.
133+
case 401: // UnAuthorized Access. Invalid access token.
134+
unset($_SESSION['client_token']);
135+
if (DEBUG) {
136+
Debug::init();
137+
Debug::write("Removed cached client token. Errocode=". $se->getErrorCode() ."\n");
138+
Debug::end();
139+
}
140+
break;
141+
}
142+
return_json_error($se->getErrorCode(), $se->getErrorResponse());
132143
}
133144
catch(Exception $e) {
134-
return_json_error(400, $e->getMessage());
145+
$error = $e->getMessage();
146+
// some operations in the codekit do not throw ServiceException
147+
if (stripos($error, 'UnAuthorized Request') !== false) {
148+
unset($_SESSION['client_token']);
149+
if (DEBUG) {
150+
Debug::init();
151+
Debug::write("token removed.\n");
152+
Debug::end();
153+
}
154+
return_json_error(401, "UnAuthorized Request. Try again to obtain a new access token.");
155+
} else {
156+
return_json_error(400, $error);
157+
}
135158
}
136159

137160
?>

sdk/server/php/public_html/att/myMessages.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,35 @@
236236
echo $response;
237237
}
238238
catch(ServiceException $se) {
239-
return_json_error($se->getErrorCode(), $se->getErrorResponse());
239+
switch ($se->getErrorCode()) {
240+
case 400: // invalid_grant. Invalid Refresh token.
241+
case 401: // UnAuthorized Access. Invalid access token.
242+
unset($_SESSION['consent_tokens']['MIM']);
243+
unset($_SESSION['consent_tokens']['IMMN']);
244+
if (DEBUG) {
245+
Debug::init();
246+
Debug::write("Removed cached client token. Errocode=". $se->getErrorCode() ."\n");
247+
Debug::end();
248+
}
249+
break;
250+
}
251+
return_json_error($se->getErrorCode(), $se->getErrorResponse());
240252
}
241253
catch(Exception $e) {
242-
return_json_error(400, $e->getMessage());
254+
$error = $e->getMessage();
255+
// some operations in the codekit do not throw ServiceException
256+
if (stripos($error, 'UnAuthorized Request') !== false) {
257+
unset($_SESSION['consent_tokens']['MIM']);
258+
unset($_SESSION['consent_tokens']['IMMN']);
259+
if (DEBUG) {
260+
Debug::init();
261+
Debug::write("token removed.\n");
262+
Debug::end();
263+
}
264+
return_json_error(401, "UnAuthorized Request. Try again to obtain a new access token.");
265+
} else {
266+
return_json_error(400, $error);
267+
}
243268
}
244269

245270
?>

sdk/server/php/public_html/att/rest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,33 @@
5656
echo $response;
5757
}
5858
catch(ServiceException $se) {
59-
return_json_error($se->getErrorCode(), $se->getErrorResponse());
59+
switch ($se->getErrorCode()) {
60+
case 400: // invalid_grant. Invalid Refresh token.
61+
case 401: // UnAuthorized Access. Invalid access token.
62+
unset($_SESSION['client_token']);
63+
if (DEBUG) {
64+
Debug::init();
65+
Debug::write("Removed cached client token. Errocode=". $se->getErrorCode() ."\n");
66+
Debug::end();
67+
}
68+
break;
69+
}
70+
return_json_error($se->getErrorCode(), $se->getErrorResponse());
6071
}
6172
catch(Exception $e) {
62-
return_json_error(400, $e->getMessage());
73+
$error = $e->getMessage();
74+
// some operations in the codekit do not throw ServiceException
75+
if (stripos($error, 'UnAuthorized Request') !== false) {
76+
unset($_SESSION['client_token']);
77+
if (DEBUG) {
78+
Debug::init();
79+
Debug::write("token removed.\n");
80+
Debug::end();
81+
}
82+
return_json_error(401, "UnAuthorized Request. Try again to obtain a new access token.");
83+
} else {
84+
return_json_error(400, $error);
85+
}
6386
}
6487

6588
?>

sdk/server/php/public_html/att/sms.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,33 @@
8585
echo $response;
8686
}
8787
catch(ServiceException $se) {
88-
return_json_error($se->getErrorCode(), $se->getErrorResponse());
88+
switch ($se->getErrorCode()) {
89+
case 400: // invalid_grant. Invalid Refresh token.
90+
case 401: // UnAuthorized Access. Invalid access token.
91+
unset($_SESSION['client_token']);
92+
if (DEBUG) {
93+
Debug::init();
94+
Debug::write("Removed cached client token. Errocode=". $se->getErrorCode() ."\n");
95+
Debug::end();
96+
}
97+
break;
98+
}
99+
return_json_error($se->getErrorCode(), $se->getErrorResponse());
89100
}
90101
catch(Exception $e) {
91-
return_json_error(400, $e->getMessage());
102+
$error = $e->getMessage();
103+
// some operations in the codekit do not throw ServiceException
104+
if (stripos($error, 'UnAuthorized Request') !== false) {
105+
unset($_SESSION['client_token']);
106+
if (DEBUG) {
107+
Debug::init();
108+
Debug::write("token removed.\n");
109+
Debug::end();
110+
}
111+
return_json_error(401, "UnAuthorized Request. Try again to obtain a new access token.");
112+
} else {
113+
return_json_error(400, $error);
114+
}
92115
}
93116

94117
?>

sdk/server/php/public_html/att/speech.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
switch ($se->getErrorCode()) {
8787
case 400: // invalid_grant. Invalid Refresh token.
8888
case 401: // UnAuthorized Access. Invalid access token.
89-
case 403: // Forbidden - app-key secret might have been chnaged in between
9089
unset($_SESSION['client_token']);
9190
if (DEBUG) {
9291
Debug::init();
@@ -98,11 +97,6 @@
9897
return_json_error($se->getErrorCode(), $se->getErrorResponse());
9998
}
10099
catch(Exception $e) {
101-
if (DEBUG) {
102-
Debug::init();
103-
Debug::write("Error code: ".$e->getCode()." ErrorMessage: ".$e->getMessage());
104-
Debug::end();
105-
}
106100
$error = $e->getMessage();
107101
// some operations in the codekit do not throw ServiceException
108102
if (stripos($error, 'UnAuthorized Request') !== false) {

0 commit comments

Comments
 (0)