Skip to content

Commit e95e057

Browse files
authored
Merge pull request #85 from ousid/fix/cross-origin-authorization
Fix: Cross Origin Authorization Error.
2 parents e129c9d + 136a4df commit e95e057

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/MsGraph.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,32 @@ class MsGraph
2828
{
2929
public function contacts(): Contacts
3030
{
31-
return new Contacts();
31+
return new Contacts;
3232
}
3333

3434
public function emails(): Emails
3535
{
36-
return new Emails();
36+
return new Emails;
3737
}
3838

3939
public function files(): Files
4040
{
41-
return new Files();
41+
return new Files;
4242
}
4343

4444
public function sites(): Sites
4545
{
46-
return new Sites();
46+
return new Sites;
4747
}
4848

4949
public function tasklists(): TaskLists
5050
{
51-
return new TaskLists();
51+
return new TaskLists;
5252
}
5353

5454
public function tasks(): Tasks
5555
{
56-
return new Tasks();
56+
return new Tasks;
5757
}
5858

5959
protected static string $baseUrl = 'https://graph.microsoft.com/v1.0/';
@@ -83,7 +83,7 @@ public static function setUserModel(string $model): static
8383
{
8484
self::$userModel = $model;
8585

86-
return new static();
86+
return new static;
8787
}
8888

8989
/**
@@ -116,7 +116,18 @@ public function connect(?string $id = null): Redirector|RedirectResponse
116116

117117
if (request()->has('code')) {
118118

119-
$accessToken = $provider->getAccessToken('authorization_code', ['code' => request('code')]);
119+
try {
120+
$accessToken = $provider->getAccessToken('authorization_code', ['code' => request('code')]);
121+
} catch (IdentityProviderException $e) {
122+
123+
$response = $e->getResponseBody();
124+
125+
$errorMessage = "{$response['error']} {$response['error_description']}\n".
126+
'Error Code: '.($response['error_codes'][0] ?? 'N/A')."\n".
127+
'More Info: '.($response['error_uri'] ?? 'N/A');
128+
129+
throw new Exception($errorMessage);
130+
}
120131

121132
if (auth()->check()) {
122133
$this->storeToken(
@@ -334,6 +345,9 @@ protected function getUserId(?string $id = null): ?string
334345
protected function getProvider(): GenericProvider
335346
{
336347
app()->singleton(GenericProvider::class, function () {
348+
349+
$codeVerifier = bin2hex(random_bytes(32));
350+
337351
return new GenericProvider([
338352
'clientId' => config('msgraph.clientId'),
339353
'clientSecret' => config('msgraph.clientSecret'),
@@ -342,6 +356,10 @@ protected function getProvider(): GenericProvider
342356
'urlAccessToken' => config('msgraph.urlAccessToken'),
343357
'urlResourceOwnerDetails' => config('msgraph.urlResourceOwnerDetails'),
344358
'scopes' => config('msgraph.scopes'),
359+
'code_challenge_method' => 'S256',
360+
'code_challenge' => rtrim(
361+
strtr(base64_encode(hash('sha256', $codeVerifier, true)), '+/', '-_'), '='
362+
),
345363
]);
346364
});
347365

tests/MsGraphTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
MsGraphFacade::connect();
7171

72-
})->throws(IdentityProviderException::class);
72+
})->throws(Exception::class);
7373

7474
test('can connect with valid code', function () {
7575

0 commit comments

Comments
 (0)