Skip to content

Commit 063abaa

Browse files
dietercoopmanclaude
andcommitted
Harden OAuth callback and token refresh error handling
Dispatch MicrosoftGraphErrorReceived when the callback has no code or the token exchange fails, and throw a clear exception when refreshing with an invalid refresh token. Release v1.22. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent da00dbf commit 063abaa

3 files changed

Lines changed: 58 additions & 11 deletions

File tree

‎CHANGELOG.md‎

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

33
All notable changes to `microsoftgraph` will be documented in this file.
44

5+
## v1.22 - 2026-11-10
6+
7+
- More robust OAuth callback handling: an error from Microsoft, a missing authorization code, or a code that could not be exchanged for an access token (for example one already redeemed by a link-preview bot) now dispatches `MicrosoftGraphErrorReceived` instead of failing on an empty token response.
8+
- Refreshing an expired access token with an invalid refresh token now throws a clear "please reconnect" exception instead of dispatching an event with empty data.
9+
510
## v1.21 - 2026-10-22
611

712
- Added support for Laravel 13 (and 11, 12) with `orchestra/testbench` 9–11.

‎src/Authenticate.php‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
namespace LLoadout\Microsoftgraph;
44

5-
use Illuminate\Support\Facades\Http;
65
use Laravel\Socialite\Facades\Socialite;
7-
use LLoadout\Microsoftgraph\EventListeners\MicrosoftGraphErrorReceived;
86
use LLoadout\Microsoftgraph\Traits\Authenticate as AuthTrait;
97

108
/**
119
* Class Authenticate
1210
*
1311
* Handles Microsoft Graph API authentication using OAuth2 flow
14-
*
15-
* @package LLoadout\Microsoftgraph
1612
*/
1713
class Authenticate
1814
{
@@ -34,18 +30,21 @@ public function connect()
3430
* Handles the OAuth2 callback from Microsoft
3531
*
3632
* Processes the authentication response, either:
37-
* - Dispatches error event if authentication failed
33+
* - Dispatches error event if authentication failed (error from Microsoft, no code,
34+
* or a code that could not be exchanged for an access token)
3835
* - Retrieves access token and dispatches success event if authentication succeeded
3936
*
4037
* @return \Illuminate\Http\RedirectResponse|void
4138
*/
4239
public function callback()
4340
{
4441
if (request()->has('error')) {
45-
MicrosoftGraphErrorReceived::dispatch(encrypt((object)['error' => request('error'), 'error_description' => request('error_description')]));
46-
} else {
47-
$tokenData = Http::asForm()->post('https://login.microsoftonline.com/' . config('services.microsoft.tenant') . '/oauth2/token', $this->getTokenFields(request('code')))->object();
48-
$this->dispatchCallbackReceived($tokenData);
42+
$this->dispatchErrorReceived(request('error'), request('error_description'));
43+
44+
return;
45+
}
46+
47+
if ($this->handleAuthorizationCode(request('code'))) {
4948
return redirect(config('services.microsoft.redirect_after_callback'));
5049
}
5150
}

‎src/Traits/Authenticate.php‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ trait Authenticate
1313
private function refreshAccessToken($refreshtoken): void
1414
{
1515
$tokenData = Http::asForm()->post('https://login.microsoftonline.com/'.config('services.microsoft.tenant').'/oauth2/token', $this->getRefreshFields($refreshtoken))->object();
16+
17+
if (! $this->hasAccessToken($tokenData)) {
18+
throw new \Exception('Your access data is invalid, please reconnect');
19+
}
20+
1621
$this->dispatchCallbackReceived($tokenData);
1722
}
1823

@@ -22,10 +27,48 @@ private function dispatchCallbackReceived($tokenData): void
2227
MicrosoftGraphCallbackReceived::dispatch(encrypt((object) ['user' => $user, 'expires_on' => $tokenData->expires_on, 'access_token' => $tokenData->access_token, 'refresh_token' => $tokenData->refresh_token]));
2328
}
2429

30+
private function dispatchErrorReceived($error, $errorDescription = null): void
31+
{
32+
MicrosoftGraphErrorReceived::dispatch(encrypt((object) ['error' => $error, 'error_description' => $errorDescription]));
33+
}
34+
2535
public function callback(): void
2636
{
27-
$tokenData = Http::asForm()->post('https://login.microsoftonline.com/' . config('services.microsoft.tenant') . '/oauth2/token', $this->getTokenFields(request('code')))->object();
28-
$this->dispatchCallbackReceived($tokenData);
37+
$this->handleAuthorizationCode(request('code'));
38+
}
39+
40+
/**
41+
* Exchanges the authorization code for access data and dispatches the matching event.
42+
* Returns true when the access data was received.
43+
*/
44+
private function handleAuthorizationCode($code): bool
45+
{
46+
if (blank($code)) {
47+
$this->dispatchErrorReceived('invalid_request', 'No authorization code received');
48+
49+
return false;
50+
}
51+
52+
$tokenData = Http::asForm()->post('https://login.microsoftonline.com/'.config('services.microsoft.tenant').'/oauth2/token', $this->getTokenFields($code))->object();
53+
54+
if (! $this->hasAccessToken($tokenData)) {
55+
$this->dispatchErrorReceived($tokenData->error ?? 'invalid_token_response', $tokenData->error_description ?? null);
56+
57+
return false;
58+
}
59+
60+
$this->dispatchCallbackReceived($tokenData);
61+
62+
return true;
63+
}
64+
65+
/**
66+
* A failed token request (an authorization code that was already redeemed, e.g. by a link preview bot,
67+
* or an expired refresh token) returns an error object instead of an access token.
68+
*/
69+
private function hasAccessToken($tokenData): bool
70+
{
71+
return is_object($tokenData) && is_string($tokenData->access_token ?? null) && $tokenData->access_token !== '';
2972
}
3073

3174
private function getAccessToken()

0 commit comments

Comments
 (0)