我做了一個 SPA,它從 Azure 獲取訪問令牌,以及我需要與之安全通信的 API。我的用戶是 Azure 的,所以我按照這些Microsoft 說明注冊了 API 和相關范圍。
Custom Custom
SPA AzureAD API Graph
──┼───────────┼───────────┼───────────┼──
│ │ │ │
Access │ ────────> │ │ │
Token │ <──────── │ │ │
│ │ │ │
Access some │ ────────────────────> │ │
resource │ │ │ │
│ │ │ │
Check the │ │ <──────── │ │
token │ │ ────────> │ │ ╮ Both things
│ │ │ │ │ I need to do
Get info │ │ │ ────────> │ ╯ with Symfony
about the user │ │ <──────── │
│ │ │ │
Return the │ <──────────────────── │ │
resource │ │ │ │
現在我需要使用 php (Symfony 4) 從 API驗證令牌,并最終代表他使用 Graph獲取有關用戶的一些資訊。
我想那里有很多 Symfony 捆綁包可以幫助我做到這一點,我想避免重新發明輪子,我想我應該使用“代表”流程,對嗎?
所以我的問題是:有人可以指出一些已知的捆綁包和一些例子嗎?我應該做一個驗證器守衛嗎?
uj5u.com熱心網友回復:
thenetworg/oauth2-azure 的 wiki幫助了我
我終于創建了一個身份驗證保護,其中有趣的部分依賴于getCredentials():
public function getCredentials(Request $request)
{
$accessToken = explode(' ', $request->headers->get($this->tokenHeader))[1];
// Gets the latest signature keys from Microsoft
$keys = (new Azure())->getJwtVerificationKeys();
// Validates and returns the decoded token
return (array)JWT::decode($accessToken, $keys, ['RS256']);
}
我的完整身份驗證器可在此要點中找到
該類Azure可以在networg/oauth2-azure包中找到,并且JWT來自firebase/php-jwt(這是前者的依賴項)
Microsoft 的簽名密鑰按照官方建議進行快取,我在相關問題中討論
// First tries to use the standard cached keys, then falls back on a shorter
// cache (if signature check failed) which forces the "long" cache refresh
foreach ([0, 1] as $try) {
$firstTry = $try === 0;
try {
$keys = $firstTry ? $this->getKeys() : $this->getKeysShort();
return (array)JWT::decode($accessToken, $keys);
} catch (SignatureInvalidException $e) {
if ($firstTry) continue;
throw $e;
}
}
檢查gistgetKeys中函式的實作
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507529.html
上一篇:使用SymfonyMailer時如何獲取電子郵件ID?
下一篇:(php8)學說屬性可以繼承嗎?
