access_token from OAuth login: already redeemed
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- 説明が足りない
- 活発さ
- 停滞
- 技術スタック
- php
- 領域
- api, authentication
調査の方向性
まず GenericProvider、AuthorizationCodeContext、GraphServiceClient 周辺の OAuth フローを確認し、次に callback URL を再読み込みして失敗を再現します。1 回目と 2 回目のリクエストを比較し、SDK が交換済みの認可コードを別の方法で処理すべきかどうかを判断します。完了条件には、明確な期待動作または文書化された使用方法を含める必要があります。
索引モデルが issue の本文から書いたものです。
説明
Hello everyone!
I've successfully implemented a proof of concept for an application with Authorization Code Flow.
Calling the code below by browser, shows expected informations, BUT when one reloads the "page" a
"IdentityProviderException" with message "invalid_grant" occures - wich is really missleading IMHO.
Digging deeper one finds this "error_description":
"AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code ..."
The code below (as a proof of concept) is part of a SaaS application and should handle different taks.
In this sceanario above exception is thrown from second try to use the script.
As you can see, the "access_token" is not used directly but it seems the SDK uses it internally.
Is there a clean implementation to avoid such exceptions?
I have already a "brute force solution" by redirecting all new calls to the script
to 'https://login.microsoftonline.com/'.TENANT_ID.'/oauth2/v2.0/authorize'
but this realy, realy ugly and far from a clean solution I think.
What is wrong with this code or is this a "misbehavior" of SDK?
const TENANT_ID = '...';
const CLIENT_ID = '...';
const SECRET = '...';
const REDIRECT_TO_URI = 'https://example.com/GraphApplicationTest.php'; // placeholder!!
const MS_AUTHORIZE_URI = 'https://login.microsoftonline.com/'.TENANT_ID.'/oauth2/v2.0/authorize';
const MS_TOKEN_URI = 'https://login.microsoftonline.com/'.TENANT_ID.'/oauth2/v2.0/token';
const MS_RESSOURCE_URI = 'https://login.microsoftonline.com/'.TENANT_ID;
$basicScopes = [ 'https://graph.microsoft.com/.default' ];
$extendedScopes = [
'openid',
'offline_access',
'User.Read',
'User.Read.All',
'User.ReadBasic.All',
];
$oauthGenProvider = NULL;
$authCode = "";
$accessToken = NULL;
$nextToken = NULL;
$tokenContext = NULL;
$oauthGenProvider = new GenericProvider([
'clientId' => CLIENT_ID,
'clientSecret' => SECRET,
'redirectUri' => REDIRECT_TO_URI,
'urlAuthorize' => MS_AUTHORIZE_URI,
'urlAccessToken' => MS_TOKEN_URI,
'urlResourceOwnerDetails' => MS_RESSOURCE_URI,
'scopes' => $basicScopes,
],
);
// check errors from eventually previous request
if ( isset( $_GET['error'] ) )
{
echo "<pre>";
echo "exiting with error!\n\n";
echo "error: ".$_GET['error']."\n";
echo "description:\n ".$_GET['error_description']."\n";
exit;
}
// if not authenticated goto "login"
if (!isset($_GET['code']))
{
$authorizationUrl = $oauthGenProvider->getAuthorizationUrl();
header('Location: ' . $authorizationUrl);
exit;
}
echo "<pre>";
echo "SDK version is ".GraphConstants::SDK_VERSION."\n";
$authCode = $_GET['code'];
// first step to prepare for "GraphServiceClient": create a context of type "AuthorizationCodeContext"
// Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext
// https://github.com/microsoft/kiota-authentication-phpleague-php/blob/main/src/Oauth/AuthorizationCodeContext.php
$tokenContext = new AuthorizationCodeContext(
TENANT_ID,
CLIENT_ID,
SECRET,
$authCode,
REDIRECT_TO_URI,
);
$graphClient = new GraphServiceClient($tokenContext, $extendedScopes);
try
{
$users = $graphClient->users()->get()->wait();
$userList = $users->getValue(); // returns an array!
// $userList[x] <==> Microsoft\Graph\Generated\Models\User
// https://github.com/microsoftgraph/msgraph-sdk-php/blob/main/src/Generated/Models/User.php
echo " user[0] display name: ".$userList[0]->getDisplayName()."\n";
}
catch ( Exception $e )
{
if ( $e instanceof \League\OAuth2\Client\Provider\Exception\IdentityProviderException )
{
echo " IdentityProviderException\n";
echo $e->getResponseBody()["error_description"];
// AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.
}
else
{
echo " Exception";
}
echo $e->getMessage()."\n";
}
- 主要言語
- PHP
- スター
- 669
- フォーク
- 149
- 平均マージ
- 1日 4時間
- マージ済み PR(30日)
- 5
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoftgraph/msgraph-sdk-php のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
microsoftgraph/msgraph-sdk-php#1794 · コメント 2 件 ·
-
bug: metadata type:bug
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
microsoftgraph/msgraph-sdk-php#1739 · コメント 6 件 ·
-
status:waiting-for-triage type:bug
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
microsoftgraph/msgraph-sdk-php#1643 · コメント 8 件 · リアクション 4 件 ·
-
status:waiting-for-triage
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
microsoftgraph/msgraph-sdk-php#1800 · リアクション 3 件 ·
-
Nowオープン
難易度 5/5 1週間以上 初心者へのやさしさ 1/100
microsoftgraph/msgraph-sdk-php#1799 ·
microsoftgraph/msgraph-sdk-php の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
matomo-org/plugin-CustomAlerts#310 ·
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
domain/checkout Platform(Default) priority/low testing/acceptance
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
pluginsGLPI/addressing#132 ·
-
5.14.0 PIE packages still declare PHP >=8.3, so PIE installs them on PHP 8.3 and the build failsオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100